在Laravel 5.2中调整图像大小

26

有人能帮我学习如何在Laravel中实现调整图像大小的功能吗?

我目前只有这段代码:

if($request->hasFile('image')){
    if (Input::file('image')->isValid()) {
        $file = Input::file('image');
        $destination = base_path() . '/public/images/ServiceImages';
        $extension = Input::file('image')->getClientOriginalExtension();
        $fileName = rand(111,99999).'.'.$extension;

        if(!empty($data['Image'])){
            unlink($destination.$data['Image']);
        }

        $file->move($destination, $fileName);
        $service->image=$fileName;
    }
}

1
http://image.intervention.io/ - Rwd
3个回答

112

Laravel没有默认的图像大小调整功能。但大多数Laravel开发人员在处理图像时会使用Image intervention库。它易于使用。

安装(Image intervention):

步骤1:运行

composer require intervention/image

步骤 2 在你的 config/app.php 文件中:

在 $providers 数组中,添加以下内容:

Intervention\Image\ImageServiceProvider::class
在$aliases数组中添加以下内容:
'Image' => Intervention\Image\Facades\Image::class

如果您遇到GD库缺失的问题,请安装它。

  • PHP5:sudo apt-get install php5-gd
  • PHP7:sudo apt-get install php7.0-gd

在您的控制器上使用:

步骤3

在您的控制器顶部添加以下代码:

use Intervention\Image\ImageManagerStatic as Image;

步骤4

在您的方法中使用(有几种方式,但这将给您一个想法):

if($request->hasFile('image')) {

    $image       = $request->file('image');
    $filename    = $image->getClientOriginalName();

    $image_resize = Image::make($image->getRealPath());              
    $image_resize->resize(300, 300);
    $image_resize->save(public_path('images/ServiceImages/' .$filename));

}

点击这里 查看参考资料。


1
你得到了我的加一。谢谢。 - MrMarlow
1
这个库的限制是 - 它无法将上传的图像保存在新目录中。 - Sanaulla
调整图像大小会减小它们的尺寸吗? - Youssef Boudaya
@Sanaulla 这个包的主要缺点是会丢失图像的 DPI(每英寸点数)。 - kunal
1
@YoussefBoudaya 是的,它也会减小尺寸。 - kunal
如果您已经使用了 use Intervention\Image\ImageManagerStatic as Image;,那么您就不需要执行第一步和第二步了。 - Adam

0
我会推荐使用 Intervention Image 来完成这个任务。
只需要用 composer 安装即可。
composer require intervention/image

然后就像这样使用它:

\Intervention\Image\ImageManagerStatic::make('public/foo')->fit(100)->save($path);

备注

  • 服务提供者 Intervention Image 为 Laravel 提供了一个服务提供者。您可以按照 这里 的说明手动添加服务提供者。之后,您可以将配置文件推送到 Laravel。但是,该配置仅有一个选项,即图像驱动程序。 gd 是默认值,因此如果您不想更改它,则无需使用服务提供者和配置。

  • 别名 您可以在 config/app 中创建别名,而不是使用服务提供者:

    'Image' => Intervention\Image\ImageManagerStatic::class
    

    然后您可以像这样使用它:

    \Image::make('public/foo')->fit(100)->save($path);
    
  • 测试 当使用 intervention image 时,您仍然可以使用 Laravel 的文件上传测试。有关详细信息,请参见如何使用 Laravel 和 Intervention Image 包模拟图像上传进行测试

0
尽管这是一篇旧帖子,但我认为发表一个不需要安装任何软件包的解决方案。
$image = $request->file('image');

$image_name = rand(111111, 888999)*time() .'.'. $image->getClientOriginalExtension();
$thumb_name = rand(111111, 888999)*time() .'.'. $image->getClientOriginalExtension();
$destinationPath = public_path('/uploads');

$image->move($destinationPath, $image_name);
$orgImgPath = $destinationPath. '/'.$image_name;
$thumbPath = $destinationPath. '/'.$thumb_name;
shell_exec("convert $orgImgPath -resize 200x200\! $thumbPath");

这将强制将您的图像调整为200x200,因为有感叹号,但如果您想保持纵横比,则需要删除感叹号。此代码将保存原始上传的图像,并生成缩略图。


但是它需要您访问命令行,而许多托管提供商不允许这样做。 - marhyno

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