Laravel 5.6: Create image thumbnails

谁说我不能喝 提交于 2019-12-22 05:12:27

问题


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!


回答1:


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');



回答2:


Resize and crop image by center

No need to install and include any package. Just create a helper in laravel / lumen and put below code in that helper file and use it wherever you want:

function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){
    $imgsize = getimagesize($source_file);
    $width = $imgsize[0];
    $height = $imgsize[1];
    $mime = $imgsize['mime'];

    switch($mime){
        case 'image/gif':
            $image_create = "imagecreatefromgif";
            $image = "imagegif";
            break;

        case 'image/png':
            $image_create = "imagecreatefrompng";
            $image = "imagepng";
            $quality = 7;
            break;

        case 'image/jpeg':
            $image_create = "imagecreatefromjpeg";
            $image = "imagejpeg";
            $quality = 80;
            break;

        default:
            return false;
            break;
    }

    $dst_img = imagecreatetruecolor($max_width, $max_height);
    $src_img = $image_create($source_file);

    $width_new = $height * $max_width / $max_height;
    $height_new = $width * $max_height / $max_width;
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
    if($width_new > $width){
        //cut point by height
        $h_point = (($height - $height_new) / 2);
        //copy image
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
    }else{
        //cut point by width
        $w_point = (($width - $width_new) / 2);
        imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
    }

    $image($dst_img, $dst_dir, $quality);

    if($dst_img)imagedestroy($dst_img);
    if($src_img)imagedestroy($src_img);
}
//usage example
resize_crop_image(100, 100, "test.jpg", "test.jpg");

Code is already tested many times and working well. All the best, save your time and enjoy your life :)



来源:https://stackoverflow.com/questions/50451911/laravel-5-6-create-image-thumbnails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!