Responsive images

Squashed commit of the following:

commit 4f62b48b77b0b34f2f556083e2271f46d5bd023e
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sat Sep 16 11:38:26 2017 +0100

    Update changelog

commit 2c41451b24839dfa9a37e6f92bc542cef999aaa9
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sat Sep 16 11:38:14 2017 +0100

    run migrations in deploy script

commit 2b1c3af725d366479399afcd8059e9abebade9ee
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 23:38:08 2017 +0100

    Modified CSS for note img links

commit fcd6217da3443e28764ed7a810620b54d04b223e
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 23:19:42 2017 +0100

    Add responsive image markup

commit d900d8067dbf36180fd1bdaca7d34421ba85a413
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 23:19:24 2017 +0100

    Fix issues with logic

commit c03d18c5e02c041092ce401f2a814e2a4f8e6fad
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 20:33:39 2017 +0100

    Use new column name

commit 21d40eab48f9f038cf8ea82880b58d68ecdf0549
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 20:33:12 2017 +0100

    Use text column type to give future leeway with how we do this

commit abb3b3b1e14a8de58cac8dffcc06d3b8bb06119d
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 19:36:29 2017 +0100

    Some tests of the new job

commit 0b11093df16a8c0047520322185706bbdc52c0c4
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 19:36:13 2017 +0100

    This job creates smaller resolution images

commit 93449ad2b367bea33e84ec94486125467eaf0394
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 19:35:52 2017 +0100

    Save the media directly to S3, then dispatch the image processing job

commit fff232607c18a6681ea4414b6e54c006614f4e5e
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 19:35:06 2017 +0100

    Store the image library’s preferences

commit 0b908b99a79f8a1294d2c59cd731c18538ffb6ce
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 19:33:28 2017 +0100

    Configure the ligrary to use imagick

commit ed13e55e0ce1c0e94860259bf0b1d97a433c89b1
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Sep 15 19:32:55 2017 +0100

    Add the intervention/image package
This commit is contained in:
Jonny Barnes 2017-09-16 11:39:36 +01:00
parent d1bce2e9e2
commit 39ffb2c225
28 changed files with 308 additions and 6 deletions

View file

@ -2,10 +2,13 @@
namespace App\Http\Controllers;
use Storage;
use Monolog\Logger;
use Ramsey\Uuid\Uuid;
use App\Jobs\ProcessImage;
use App\{Media, Note, Place};
use Monolog\Handler\StreamHandler;
use Intervention\Image\ImageManager;
use Illuminate\Http\{Request, Response};
use App\Exceptions\InvalidTokenException;
use Phaza\LaravelPostgis\Geometries\Point;
@ -399,8 +402,11 @@ class MicropubController extends Controller
'error_description' => 'A problem occured handling your request',
], 500);
}
$size = $request->file('file')->getClientSize();
Storage::disk('local')->put($filename, $request->file('file')->openFile()->fread($size));
try {
$path = $request->file('file')->storeAs('media', $filename, 's3');
Storage::disk('s3')->put('media/' . $filename, $request->file('file')->openFile()->fread($size));
} catch (Exception $e) { // which exception?
return response()->json([
'response' => 'error',
@ -408,12 +414,25 @@ class MicropubController extends Controller
'error_description' => 'Unable to save media to S3',
], 503);
}
$manager = app()->make(ImageManager::class);
try {
$image = $manager->make($request->file('file'));
$width = $image->width();
} catch (\Intervention\Image\Exception\NotReadableException $exception) {
// not an image
$width = null;
}
$media = new Media();
$media->token = $request->bearerToken();
$media->path = $path;
$media->path = 'media/' . $filename;
$media->type = $this->getFileTypeFromMimeType($request->file('file')->getMimeType());
$media->image_widths = $width;
$media->save();
dispatch(new ProcessImage($filename));
return response()->json([
'response' => 'created',
'location' => $media->url,

67
app/Jobs/ProcessImage.php Normal file
View file

@ -0,0 +1,67 @@
<?php
namespace App\Jobs;
use Storage;
use Illuminate\Bus\Queueable;
use Intervention\Image\ImageManager;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Intervention\Image\Exception\NotReadableException;
class ProcessImage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $filename;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(string $filename)
{
$this->filename = $filename;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(ImageManager $manager)
{
//open file
try {
$image = $manager->make(storage_path('app') . '/' . $this->filename);
} catch (NotReadableException $exception) {
// not an image; delete file and end job
unlink(storage_path('app') . '/' . $this->filename);
return;
}
//create smaller versions if necessary
if ($image->width() >= 1000) {
$filenameParts = explode('.', $this->filename);
$extension = array_pop($filenameParts);
// the following acheives this data flow
// foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
$basename = ltrim(array_reduce($filenameParts, function ($carry, $item) {
return $carry . '.' . $item;
}, ''), '.');
$medium = $image->resize(1000, null, function ($constraint) {
$constraint->aspectRatio();
});
Storage::disk('s3')->put('media/'. $basename . '-medium.' . $extension, (string) $medium->encode());
$small = $image->resize(500, null, function ($constraint) {
$constraint->aspectRatio();
});
Storage::disk('s3')->put('media/' . $basename . '-small.' . $extension, (string) $small->encode());
}
// now we can delete the locally saved image
unlink(storage_path('app') . '/' . $this->filename);
}
}

View file

@ -41,4 +41,40 @@ class Media extends Model
return config('filesystems.disks.s3.url') . '/' . $this->path;
}
/**
* Get the URL for the medium size of an S3 image file.
*
* @return string
*/
public function getMediumurlAttribute()
{
$filenameParts = explode('.', $this->path);
$extension = array_pop($filenameParts);
// the following acheives this data flow
// foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
$basename = ltrim(array_reduce($filenameParts, function ($carry, $item) {
return $carry . '.' . $item;
}, ''), '.');
return config('filesystems.disks.s3.url') . '/' . $basename . '-medium.' . $extension;
}
/**
* Get the URL for the small size of an S3 image file.
*
* @return string
*/
public function getSmallurlAttribute()
{
$filenameParts = explode('.', $this->path);
$extension = array_pop($filenameParts);
// the following acheives this data flow
// foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
$basename = ltrim(array_reduce($filenameParts, function ($carry, $item) {
return $carry . '.' . $item;
}, ''), '.');
return config('filesystems.disks.s3.url') . '/' . $basename . '-small.' . $extension;
}
}

View file

@ -52,6 +52,11 @@ class AppServiceProvider extends ServiceProvider
Request::macro('wantsActivityStream', function () {
return str_contains(mb_strtolower($this->header('Accept')), 'application/activity+json');
});
// configure Intervention/Image
$this->app->bind('Intervention\Image\ImageManager', function () {
return new \Intervention\Image\ImageManager(['driver' => config('image.driver')]);
});
}
/**