Laravel 5.6:创建图像缩略图 [英] Laravel 5.6: Create image thumbnails
问题描述
在我的旧PHP应用程序中,我曾经运行过一个像波纹管这样的功能来创建jpeg图片缩略图.
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
}
问题是,现在我想在laravel 5.6应用程序中运行类似的功能,因此我创建了一个具有完全相同功能的控制器,但是我没有得到图像缩略图作为输出,而是得到了问号和奇怪的菱形图标,类似于php gd库jpeg图像的编码版本.
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.
我试图使用return response()-> file($ pathToFile);如laravel文档所述,但我不会将缩略图存储在某个位置.
I tried to use return response()->file($pathToFile); as laravel documentation describes but i won't store in a location the thumbnail image.
有什么想法吗?
提前谢谢!
推荐答案
我建议您此软件包的安装和使用非常简单,并且非常适合编程. 它称为干预
I recommend you this package is very simple to install and use it and very kind with programming. Its called Intervention
您可以使缩略图非常简单,如下所示:
You can make a thumbnail very simple like the following :
$img = Image::make('public/foo.jpg')->resize(320, 240)->insert('public/watermark.png');
这篇关于Laravel 5.6:创建图像缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!