Last active
June 5, 2021 08:33
-
-
Save ryanscherler/f14976db7f1c0e5103b741a4a3b5858e to your computer and use it in GitHub Desktop.
Add Image Resizing to Jigsaw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Listeners\ResizeImageListener; | |
/** @var $container \Illuminate\Container\Container */ | |
/** @var $events \TightenCo\Jigsaw\Events\EventBus */ | |
/** | |
* A beforeBuild event is fired before any source files have been processed. | |
* This gives you an opportunity to programmatically modify config.php variables, | |
* fetch data from external sources, or modify files in the source folder. | |
*/ | |
$events->beforeBuild([ | |
ResizeImageListener::class, | |
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"tightenco/jigsaw": "^1.2", | |
"intervention/image": "^2.4" | |
}, | |
"autoload": { | |
"psr-4": { | |
"App\\": "app" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends("_layouts.master") | |
@section("content") | |
{{ $page->resizeImage('/assets/images/image.png', ['w' => 100, 'h' => 100, 'dpr' => 2, 'q' => 75]) }} | |
{{-- Will output: /assets/images/[email protected] --}} | |
@endsection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Listeners; | |
use TightenCo\Jigsaw\Jigsaw; | |
use App\Utilities\ResizeImageUtility; | |
class ResizeImageListener | |
{ | |
public function handle(Jigsaw $jigsaw) | |
{ | |
$jigsaw->setConfig('resizeImage', new ResizeImageUtility($jigsaw)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Utilities; | |
use TightenCo\Jigsaw\Jigsaw; | |
use TightenCo\Jigsaw\PageVariable; | |
use Intervention\Image\ImageManagerStatic as Image; | |
class ResizeImageUtility | |
{ | |
protected $jigsaw; | |
public function __construct(Jigsaw $jigsaw) | |
{ | |
$this->jigsaw = $jigsaw; | |
} | |
public function __invoke(PageVariable $page, string $path, array $manipulations) | |
{ | |
// Get manipulations | |
$width = array_get($manipulations, 'w'); | |
$height = array_get($manipulations, 'h'); | |
$dpr = array_get($manipulations, 'dpr'); | |
$quality = array_get($manipulations, 'q'); | |
// Image information | |
$imageSourcePath = $this->jigsaw->getSourcePath() . $path; | |
$imageSourceInfo = pathinfo($imageSourcePath); | |
// Build manipulated image filename | |
$imageFilename = collect([ | |
$imageSourceInfo['filename'], | |
"{$width}x{$height}", | |
$quality, | |
])->filter()->implode('-'); | |
$dprSuffix = $dpr ? '@' . $dpr . 'x' : ''; | |
$imageFile = $imageFilename . $dprSuffix . '.' . $imageSourceInfo['extension']; | |
// Make new Intervention Image instance | |
$image = Image::make($imageSourcePath); | |
// Resize image based on width and/or height | |
if ($width || $height) { | |
$image->resize($width, $height, function ($constraint) use ($width, $height) { | |
if (!$width || !$height) { | |
$constraint->aspectRatio(); | |
} | |
}); | |
} | |
// Widen image based on device pixel resolution | |
if ($dpr) { | |
$image->widen($image->width() * (int) $dpr); | |
} | |
// Get encoded string of manipulated image instance | |
$contents = (string) $image->encode(null, $quality); | |
// Get manipulated image path | |
$imagePath = pathinfo($path, PATHINFO_DIRNAME) . '/' . $imageFile; | |
// Get manipulated image relative build path | |
$imageBuildPath = basename($this->jigsaw->getDestinationPath()) . $imagePath; | |
// Save manipulated image to build output | |
$this->jigsaw->getFilesystem()->putWithDirectories($imageBuildPath, $contents); | |
// Return manipulated image path | |
return $imagePath; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment