根据查询字符串调整图像大小



我正在使用Laravel。我想根据查询字符串调整图像大小并创建缩略图。

例如,如果有人请求example.com/1.jpg?width=120example.com/anything/1.jpg?width=120,则原始图像必须更改为新的调整大小的图像。

事实上,我想要的只是像.jpg.png这样的图像文件的路由系统,其中有一个查询字符串。

在PHP或Laravel中有没有任何方法可以获得所有带有查询字符串的图像文件请求并对其进行操作?


更新:我测试了@iftikhar uddin的答案。它适用于单个请求。就像我在浏览器中直接请求这个urlexample.com/anything/1.jpg?width=120一样。

但我想得到所有的图像,并在页面加载时对它们进行操作。

示例:我有多个类似<img src="/anything/1.jpg?width=120">的html标签当页面加载时,我想获取所有图像,并按照查询字符串的大小对它们进行操作。

我以前做过什么目前我为此写了一节课。但问题是,在某些情况下,我找不到图像的原始目录。在我的课堂上:1-我在像这样的<img src="{{class::cache($model->image, 'small')}}">的图像标签中获得图像源和大小

2-然后我根据班上的大小调整图像大小(使用image.intervention.io)。

3-但在某些情况下(比如当我使用lfm包时),镜像和实际目录的路径是不同的。所以当我想要基于源调整图像大小时,我会出错。(目录为'/public/share/image.jpg',但路由为'laravel filemanager/share/image.jpg’)

出于这个原因,我正在寻找一种在加载页面时通过url获取图像的方法,而不是通过我们在图像标签中插入的来源。我想这样肯定容易多了。

提示1:

http://image.intervention.io/

Intervention Image是一个开源的PHP图像处理和操作库。它提供了一种更容易表达的方式创建、编辑和合成图像,并支持目前最多的两种通用图像处理库GD-Library和Imagick。

public InterventionImageImage resize (integer $width, integer $height, [Closure $callback])
Resizes current image based on given width and/or height. 
To constrain the resize command, pass an optional Closure callback as the third parameter.

// create instance
$img = Image::make('public/foo.jpg')
// resize image to fixed size
$img->resize(300, 200);
// resize only the width of the image
$img->resize(300, null);
// resize only the height of the image
$img->resize(null, 200);
// resize the image to a width of 300 and constrain aspect ratio (auto height)
$img->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
});
// resize the image to a height of 200 and constrain aspect ratio (auto width)
$img->resize(null, 200, function ($constraint) {
$constraint->aspectRatio();
});
// prevent possible upsizing
$img->resize(null, 400, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});

Tips2:

http://php.net/manual/en/function.imagecopyresized.php使用PHP函数

首先安装图像干预

然后在你的控制器中做这样的事情:

if($request->hasFile('image')) {
$image       = $request->file('image');
$filename    = $image->getClientOriginalName();
$width = $request->input('width');
$height = $request->input('height');
$image_resize = Image::make($image->getRealPath());              
$image_resize->resize($width, $height );
$image_resize->save(public_path('YourImagesPath' .$filename));
}

最新更新