Laravel 5.6:创建图像缩略图

Vas*_*lis 6 php gd image thumbnails laravel

在我以前的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

}
Run Code Online (Sandbox Code Playgroud)

问题是,现在我想在laravel 5.6应用程序中运行类似的功能,所以我创建了一个具有完全相同功能的控制器,但是我没有得到图像缩略图作为输出,而是得到了问号和奇怪的菱形图标,像php gd库jpeg图像的编码版本。

我试图使用return response()-> file($ pathToFile); 如laravel文档所述,但我不会将缩略图存储在某个位置。

有任何想法吗?

先感谢您!

Mar*_*ino 8

我建议您此软件包非常易于安装和使用,并且非常适合编程。它叫做干预

干预包以处理图像

您可以使缩略图非常简单,如下所示:

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