Laravel 5.6:创建图像缩略图

7
在我的旧PHP应用程序中,我使用以下类似的函数来创建JPEG图像缩略图。
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

}

问题在于我想在 Laravel 5.6 应用程序中运行类似的功能,因此我创建了一个控制器,其功能与原来的函数完全相同,但是输出不再是图像缩略图,而是一些问号和奇怪的菱形符号,类似于 PHP GD 库 JPEG 图像的编码版本。
我尝试使用 Laravel 文档描述的 return response()->file($pathToFile);,但是它不会将缩略图图像存储在位置上。
有什么想法吗?
提前感谢!

1
我在过去的几个 Laravel 项目中使用了这个库。我知道这并没有解决你的编码问题,但这是一个建议。https://github.com/Intervention/image - Ryan Kozak
@Ryankozak 谢谢,我会去查看这个! - Vasileios Tsakalis
1
这个包 https://github.com/spatie/laravel-medialibrary 是管理图片的一种漂亮而优雅的方式... - Bart
@Bart 谢谢,我会去看看! - Vasileios Tsakalis
2个回答

17

我推荐你使用Intervention这个包,它非常容易安装和使用,并且对编程非常友好。这个包名叫Intervention

Intervention图片处理包

你可以像下面这样轻松创建缩略图:

$img = Image::make('public/foo.jpg')->resize(320, 240)->insert('public/watermark.png');

3

居中裁剪和调整图像大小

无需安装或包含任何软件包。只需在 Laravel/Lumen 中创建一个帮助器,将以下代码放入该帮助器文件中,并在需要时使用:

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

代码已经被多次测试过,并且运行良好。祝您一切顺利,节省时间并享受生活 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接