【问题标题】:Resize image file laravel 5调整图像文件大小 laravel 5
【发布时间】:2016-02-16 09:14:25
【问题描述】:

我安装了补丁“intervention/image”、“must-master”,以使我的图像大小减少到 300 x 300。 我做了一些表格,在我看来总是同样的错误。

在字符串上调用成员函数 resize()

哪个出错了?

控制器

public function updateProfile() {

    $file = Input::file('imagem');
    $profileData = Input::except('_token');
    $validation = Validator::make($profileData, User::$profileData);
    if ($validation->passes()) {
        if ($file == null) {
            User::where('id', Input::get('id'))->update($profileData);
            Session::flash('message', 'Perfil editado com sucesso');
            return view('backend/perfil.index'); 
        }
        $file = array_get($profileData,'imagem');
        $destinationPath = 'imagens/perfil';
        $extension = $file->getClientOriginalExtension();
        $filename = rand(11111, 99999) . '.' . $extension;
        $reduzir = $filename -> resize (300,300); 
        $profileData['imagem'] = $filename;
        $upload_success = $file->move($destinationPath, $filename);


        User::where('id', Input::get('id'))->update($profileData);
        Session::flash('message', 'Perfil editado com sucesso');
        return Redirect::to('backend/perfil');
    } else {
        return Redirect::to('backend/perfil')->withInput()->withErrors($validation);
    }
}   

【问题讨论】:

    标签: php laravel-5 resize


    【解决方案1】:

    问题可能是因为这些原因

    您是否在 app.php 中添加了此别名

    'aliases' => [
             //add these three at the bottom
            'Form'      => Illuminate\Html\FormFacade::class, 
            'HTML'      => Illuminate\Html\HtmlFacade::class,
            'Image'     => Intervention\Image\Facades\Image::class
    ],
    

    我相信你已经有了表单和 html 助手。

    并在Controller中使用这个功能

    即,只需将图像和大小值作为参数传递给此函数

    在控制器中你只需调用下面的函数

     $resizedImage  =   $this->resize($image, $request->get('image_size'));
    

    下面给出了resize()函数

    private function resize($image, $size)
        {
            try 
            {
                $extension      =   $image->getClientOriginalExtension();
                $imageRealPath  =   $image->getRealPath();
                $thumbName      =   'thumb_'. $image->getClientOriginalName();
    
                //$imageManager = new ImageManager(); // use this if you don't want facade style code
                //$img = $imageManager->make($imageRealPath);
    
                $img = Image::make($imageRealPath); // use this if you want facade style code
                $img->resize(intval($size), null, function($constraint) {
                     $constraint->aspectRatio();
                });
                return $img->save(public_path('images'). '/'. $thumbName);
            }
            catch(Exception $e)
            {
                return false;
            }
    

    【讨论】:

    • 你能帮我吗?我不明白为什么这不起作用。这不是关于那个问题,而是密码。 stackoverflow.com/questions/33716593/…
    • 好的,我去看看,图片的问题呢?
    • 我调用类 Image 并出现 Image undefined
    • 请检查您输入的图像路径!
    • 已经发现。这一行发生冲突 $upload_success = $file->move($destinationPath, $filename);插入只是 Image::make($file)->resize(400, 400)->save($destinationPath.$filename);再次感谢 :) 旅途愉快
    猜你喜欢
    • 1970-01-01
    • 2019-02-25
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 2017-03-14
    相关资源
    最近更新 更多