Created
October 27, 2015 18:01
-
-
Save aliukevicius/b91b8405ca5ac00e3c92 to your computer and use it in GitHub Desktop.
Image styles for laravel
This file contains 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 | |
class FileController extends Controller | |
{ | |
/** | |
* Create image for the style | |
* | |
* @param $style | |
* @param $imageFile | |
* | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function imageStyle($style, $imageFile) | |
{ | |
$uploadDirectory = config('app.publicUploadPath'); | |
$imageStyleDirectory = config('imageStyles.imageStyleDirectoryPath'); | |
$uploadedFilePath = $uploadDirectory . '/' . $imageFile; | |
$newImagePath = $imageStyleDirectory . '/' .$style . '/' . $imageFile; | |
if (file_exists($uploadedFilePath)) { // check if uploaded image exists | |
$styleConfig = config('imageStyles.styles.' . $style, null); | |
if ($styleConfig != null) { // check if style exists | |
$styleDire = $imageStyleDirectory . '/' .$style; | |
if (!file_exists($styleDire)) { // check if style directory exists | |
mkdir($styleDire, 0777, true); | |
} | |
// from intervention/image package | |
$img = \Image::make($uploadedFilePath); | |
// resize image instance | |
if ($styleConfig['scale'] && $styleConfig['crop']) { | |
$img->fit($styleConfig['width'], $styleConfig['height']); | |
} else if ($styleConfig['crop']) { | |
$img->crop($styleConfig['width'], $styleConfig['height']); | |
} else if ($styleConfig['scale']) { | |
bcscale(2); | |
$styleAspect = bcdiv($styleConfig['width'], $styleConfig['height']); | |
$imageAspect = bcdiv($img->width(), $img->height()); | |
if ($styleAspect >= $imageAspect) { // width and height ratios and decide by which value to scale | |
$img->resize(null, $styleConfig['height'], function ($constraint) { | |
$constraint->aspectRatio(); | |
}); | |
} else { | |
$img->resize($styleConfig['width'], null, function ($constraint) { | |
$constraint->aspectRatio(); | |
}); | |
} | |
} else { | |
$img->resize($styleConfig['width'], $styleConfig['height']); | |
} | |
if ($styleConfig['greyscale']) { | |
$img->greyscale(); | |
} | |
// save image in desired format | |
$img->save($newImagePath); | |
return \Redirect::to(\Request::getUri()); // redirect to image file after we created it.e | |
} | |
} | |
} | |
} |
This file contains 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 | |
// config file in config directory | |
return [ | |
'imageStyleDirectory' => 'images', | |
'imageStyleDirectoryPath' => public_path('images'), | |
'styles' => [ | |
'house-list' => [ | |
'width' => 250, | |
'height' => 250, | |
'scale' => true, | |
'crop' => true, | |
'greyscale' => false, | |
], | |
] | |
]; |
This file contains 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 | |
Route::pattern('imageStyle', '([A-z0-9\-\_]+)'); | |
// add at the end of the file | |
Route::any('/images/{imageStyle}/{imageFile}', 'FileController@imageStyle'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment