php 在 Laravel 5.2 中调整图像大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40358510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:09:33  来源:igfitidea点击:

Resize image in Laravel 5.2

phplaravel-5.2

提问by kunal

Can anyone help me how to implement resizing image in Laravel?

谁能帮助我如何在 Laravel 中实现调整图像大小?

I have this code only:

我只有这个代码:

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

回答by mpalencia

Laravel does not have a default resize of image. But most Laravel developers use 'Image intervention' in handling the image. It is easy to use.

Laravel 没有默认的图像大小调整。但是大多数 Laravel 开发人员使用“图像干预”来处理图像。这个用起来很简单。

To install (Image intervention):

安装(图像干预):

STEP 1Run

步骤 1运行

composer require intervention/image

STEP 2On your config/app.php:

第 2步在您的 config/app.php 上:

In the $providers array, add the following:

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

Intervention\Image\ImageServiceProvider::class

In the $aliases array,add the following:

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

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

If you have problems your GD library is missing, install it

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

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

To use on your controller.

在您的控制器上使用。

STEP 3

第 3 步

On top of your controller

在您的控制器之上

use Intervention\Image\ImageManagerStatic as Image;

use Intervention\Image\ImageManagerStatic as Image;

STEP 4

第四步

On your method (there are several ways but this will give you an idea)

关于你的方法(有几种方法,但这会给你一个想法)

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

}

Reference here.

参考这里

回答by Imran Khan

Although this is an old post but I consider posting a solution which is independent of installing any package.

虽然这是一篇旧帖子,但我考虑发布一个独立于安装任何软件包的解决方案。

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

This will forcely resize your image to 200x200 because of the ! but if you like to keep the aspect ratio then ! need to be removed. This code will save the original uploaded and thumbnail will be generated.

由于 ! 但如果你想保持纵横比那么!需要删除。此代码将保存原始上传并生成缩略图。

回答by Adam

I would recommend Intervention Imagefor this task.

我会为此任务推荐干预图像

Simply install it with composer

只需使用 Composer 安装即可

composer require intervention/image

and just use it like this:

并像这样使用它:

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


Remarks

评论

  • Service ProviderThe intervention image provides a Service Provider for Laravel. You may add the service provider manually as explained here. After that, you may push the configuration file to Laravel. However, the configuration has only one option and that is the image driver. gdis default , so if you don't want to change it, there is no need to use the service provider and configuration.

  • AliasInstead of the service Provider, you could create an alias in config/app:

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

    Then you can use it like this:

    \Image::make('public/foo')->fit(100)->save($path);
    
  • TestingYou can still Laravel's file upload testswhen using intervention image. For details, see How to fake image upload for testing with Intervention image package using Laravel
  • 服务提供者干预图像为 Laravel 提供了一个服务提供者。您可以按照此处的说明手动添加服务提供者。之后,您可以将配置文件推送到 Laravel。但是,配置只有一个选项,那就是映像驱动程序。gd是 default ,所以如果你不想改变它,没有必要使用服务提供者和配置。

  • 别名代替服务提供者,您可以在config/app以下位置创建别名:

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

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

    \Image::make('public/foo')->fit(100)->save($path);
    
  • 测试使用干预图片时,您仍然可以进行 Laravel 的文件上传测试。有关详细信息,请参阅如何使用 Laravel 伪造图像上传以使用干预图像包进行测试