php - 调整图像文件 laravel 5

标签 php laravel-5 resize

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

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

哪个出错了?

Controller

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

最佳答案

问题可能是因为这些原因

你在你的 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中使用这个函数

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

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

关于php - 调整图像文件 laravel 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33715619/

相关文章:

php - Laravel 多对多查询关系与 where 子句

php - Laravel 连接外部数据库?

php - 为什么我不能在 PHP 中为类变量定义连接字符串?

php - 我将表单数据从 php 文件回显到 html 表单,现在我想更新对 MYSQL 的更改

php - 拖放保存定位问题

javascript - 如何使用 javascript 和 laravel 将数据传递到数据库

jquery - 如何使用 iframe 正确调整 jQuery.ui 对话框的大小

java - GWT:是否可以在添加下一个子项之前但在 CSS 规则应用于该子项之后检查父项大小?

android - 无法在 Android 中调整 Web View 的大小

php - 使用 Drupal 配置 APC