Last active
January 6, 2023 19:09
-
-
Save MicrowaveDev/6e648bbfe5c1ce1414771cdf3e852f2b to your computer and use it in GitHub Desktop.
Laravel Intervention Image place to square for instagram(fill background, vertical or horizontal align a image) plus blur option.
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 | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
class AdminController extends Controller | |
{ | |
public function imageFillToSquare(Request $request){ | |
$data = $request->all(); | |
$image = \Image::make(base_path($data['src'])); | |
$image_height = $image->height(); | |
$image_width = $image->width(); | |
if($image_height > $image_width) { | |
$is_vertical = true; | |
} else if($image_height < $image_width) { | |
$is_vertical = false; | |
} else { | |
return $image->response(); | |
} | |
$max_size = $is_vertical ? $image_height : $image_width; | |
$canvas = \Image::canvas($max_size, $max_size); | |
$image->backup(); | |
if(isset($data['blur_fill']) && $data['blur_fill']) | |
#recomended use Imagick driver | |
$canvas->fill($image->blur($data['blur_fill'])); | |
else | |
$canvas->fill('#ffffff'); | |
$image->reset(); | |
$canvas->insert($image->encode('data-url'), 'center'); | |
return $canvas->response(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like this a lot! I had a similar application whereby I had to center an image on a canvas. Now I have a new application that needs exactly this to crop the image as well, so that I always end up with square blocks. Your gist inspired me to publish my gist: https://gist.github.com/eugenevdm/b2b4019014fe3e88f1c5acce6740ab69