Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.6: Create image thumbnails

In my old PHP apps i used to run a function like the one bellow to create jpeg image thumbnails.

function imageThumbanail() {

 $image_src = imagecreatefromjpeg('http://examplesite.com/images/sample-image.jpg'); 

 $thumbnail_width = 180; //Desirable thumbnail width size 180px

 $image_width = imagesx($image_src); //Original image width size -> 1080px

 $image_height = imagesy($image_src); //Original image height size -> 1080px

 $thumbnail_height = floor( $image_height * ( $thumb_width / $image_width ) ); //Calculate the right thumbnail height depends on given thumbnail width

 $virtual_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);

 imagecopyresampled($virtual_image, $image_src, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $image_width, $image_height);

 header('Content-Type: image/jpeg');

 imagejpeg($virtual_image, null, 100);

 imagedestroy($virtual_image); //Free up memory

}

The problem is that now i'd like to run a similar function in a laravel 5.6 app, so i created a controller with the exact same function but instead of get as an output the image thumbnail i get question marks and strange diamond icons, something like encoded version of php gd library jpeg image.

I tried to use return response()->file($pathToFile); as laravel documentation describes but i won't store in a location the thumbnail image.

Any ideas?

Thank you in advance!

like image 997
Vasileios Tsakalis Avatar asked May 21 '18 15:05

Vasileios Tsakalis


1 Answers

I recommend you this package is very simple to install and use it and very kind with programming. Its called Intervention

Intervention package to handle images

You can make a thumbnail very simple like the following :

$img = Image::make('public/foo.jpg')->resize(320, 240)->insert('public/watermark.png');
like image 196
Marco Feregrino Avatar answered Sep 19 '22 12:09

Marco Feregrino