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

时间:2019-02-09 12:45:36

标签: php laravel

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

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

实际上,我想要的只是图像文件的路由系统,例如.jpg.png,其中有查询字符串。

PHP或Laravel中是否可以通过查询字符串获取对图像文件的所有请求并进行处理?


更新: 我测试了@ iftikhar-uddin答案。 它适用于单个请求。例如当我在浏览器中直接example.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获取图像的方法,而不是通过我们在image标签中插入的源获取图像的方法。我认为这种方式必须容易得多。

2 个答案:

答案 0 :(得分:0)

先安装Image intervention

然后在您的控制器中执行以下操作:

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

}

答案 1 :(得分:0)

提示1:

http://image.intervention.io/

  

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

public Intervention\Image\Image 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();
});

提示2:

  

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