The process followed in this code will:
- Upload image via form, Send to S3
- Finish processing form - Image to DB
- Create queue job
- Process queue
- Download image from S3
- Re-size (a few sizes?)
- Re-upload each to S3
- Save results to DB (mark as processed)
Install Laravel:
$ composer create-project laravel/laravel myproject
Add Composer requirements:
{
"require": {
"laravel/framework": "4.0.*",
"pda/pheanstalk": "dev-master",
"aws/aws-sdk-php-laravel": "1.*",
"imagine/imagine": "0.6.*@dev"
}
}
Followed by:
$ composer update
Setup Autoloading:
Create directory app/Proc
and then autoload it:
{
"autoload": {
"classmap": [
/* ... */
],
"psr-0": {
"Proc": "app"
}
},
}
Then run a $ composer dump-autoload
.
Setup AWS in Laravel:
$ php artisan config:publish aws/aws-sdk-php-laravel
# And then edit app/config/packages/aws/aws-sdk-php-laravel.php
# And then add `Aws\Laravel\AwsServiceProvider` Service Provider
# And then (optionally) add Aws Facade
Setup Beastalkd and server requirements.
Note: some more info on installing Beanstalkd.
$ sudo apt-get update
$ sudo apt-get install beanstalkd supervisor
Create a job to resize the images
<?php namespace Proc\Workers;
class ImageProcessor {
public function fire($job, $data)
{
}
}
Upload a file to S3 and create Queue to process it:
$file = Input::file('file');
$now = new DateTime;
$hash = md5( $file->getClientOriginalName().$now->format('Y-m-d H:i:s') );
$key = $hash.'.'.$file->getClientOriginalExtension();
$s3 = AWS::get('s3');
$s3->putObject(array(
'Bucket' => 'testprocqueue',
'Key' => $key,
'SourceFile' => $file->getRealPath(),
'ContentType' => $file->getClientMimeType(),
// ACL to be public? (Not yet)
));
Queue::push('ImageProcessor', array(
'bucket' => 'testprocqueue',
'hash' => $hash,
'key' => $key,
'ext' => $file->getClientOriginalExtension(),
'mimetype' => $file->getClientMimeType(),
));
Process the Queued item!
<?php namespace Proc\Worker;
use Aws;
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
class ImageProcessor {
protected $width;
protected $height;
protected $image;
public function fire($job, $data)
{
$s3 = Aws::get('s3');
$response = $s3->getObject(array(
'Bucket' => $data['bucket'],
'Key' => $data['key'],
));
$imagine = new Imagine();
$image = $imagine->load( (string)$response->get('Body') );
$size = new Box(100, 100);
$thumb = $image->thumbnail($size);
$s3->putObject(array(
'Bucket' => 'testprocqueue',
'Key' => $data['hash'].'_100x100.'.$data['ext'],
'Body' => $thumb->get($data['ext']),
'ContentType' => $data['mimetype'],
));
// Probaby save file info to a media database here
// If a user-generated or profile image
}
}