Skip to content

Instantly share code, notes, and snippets.

@MicrowaveDev
Last active January 6, 2023 19:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MicrowaveDev/6e648bbfe5c1ce1414771cdf3e852f2b to your computer and use it in GitHub Desktop.
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.
<?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();
}
}
@eugenefvdm
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment