Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeperl/9b9e285185ff977e605fac2fbd072d8d to your computer and use it in GitHub Desktop.
Save codeperl/9b9e285185ff977e605fac2fbd072d8d 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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment