【问题标题】:Problem with resizing images after uploading to laravel 5.2上传到 laravel 5.2 后调整图像大小的问题
【发布时间】:2019-06-20 21:37:45
【问题描述】:

晚上好,我想在我的网站上上传图像时添加图像的调整大小功能,我已经安装了必要的依赖项,并且我已经在 config/app 文件中包含了提供程序和别名。但我发现这个错误: production.ERROR: Method Illuminate\Http\UploadedFile::resize does not exist. 我把部分代码放在下面:

    public function imageProfile(Request $request)  
    {
        $user = Auth::user();
        $rules = array(
            'profile-image' => 'required|image|mimes:jpeg,png,jpg,gif|max:8192|dimensions:min_width=160,min_height=160',
        );

        $customMessages = [
            'profile-image.required' => 'E\' richiesta una immagine per cambiare immagine di profilo.',
            'profile-image.image' => 'Devi inserire un immagine valida.',
            'profile-image.mimes' => 'L\'immagine inserita non ha un formato adatto.',
            'profile-image.dimensions' => 'L\'immagine deve essere minimo 160x160.',
        ];

        $validator = Validator::make(Input::all(), $rules, $customMessages);

        if ($validator->fails()) {
            return response()->json(['success' => false, 'error' => $this->validationErrorsToString($validator->errors())]);
        }

        if ($request->hasFile('profile-image')) {
            $number = mt_rand(1,1000000);
            $image = $request->file('profile-image');
            $name = $user->username.'-'.Carbon::now()->toDateString().'-'.$number.'.'.$image->getClientOriginalExtension();
            $destinationPath = 'uploads/profile';
            $imagePath = $destinationPath. "/".  $name;
            $image->move($destinationPath, $name);
            $image->resize(200,200);

            $user->image_profile = $imagePath;
            $user->save();
            $html =  $imagePath;

            return response()->json(['success' => true, 'html' => $html, 'image' => $imagePath]);
        }
    }

谢谢你的帮助,祝你有美好的一天

【问题讨论】:

  • 你在使用干预镜像包吗?
  • 文件上传对象没有resize功能。
  • 是的,完全是@kmg kumar。所以他需要使用图像干预
  • stackoverflow.com/questions/40358510/… 这个链接可以帮到你

标签: php laravel laravel-5


【解决方案1】:

Laravel 没有默认的图片大小调整。但是大多数 laravel 开发人员在处理图像时使用“图像干预”。 (易于使用)

安装(图片干预):

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

}

【讨论】:

    猜你喜欢
    • 2017-03-14
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多