【问题标题】:PHP - Resize image on upload AND keep originalPHP - 在上传时调整图像大小并保持原始
【发布时间】:2015-02-24 16:26:03
【问题描述】:

我正在使用 Laravel 4 创建一个网站,它允许管理员一次上传大量图片。目前,我能够做到这一点,其中每个图像都有自己的唯一 ID,并放入以相同 ID 命名的自己的文件夹中。

问题是我还需要应用程序上传第二个调整大小(更小)的图像版本以及原始图像。我了解到您必须在客户端调整图像大小,所以我不确定如何保存原始图像以及较小的版本。较小的图像应使用相同的 ID 命名,名称末尾带有某种标识符,例如“-smaller”。

这是我当前的前端;

    {{ Form::open(array('url' => 'imageUpload', 'files' => true, 'method' => 'post'))}}

                    <div class="form-group">
                      <label for="fileToUpload" class="col-sm-2 control-label">Choose All Images</label>
                    </br>
                      {{ Form::file('images[]', ['multiple' => true]) }}
                    </div>

                    <div class="col-sm-offset-2 col-sm-10">
                      {{ Form::submit('Add Photos', ['class' => 'btn btn-large btn-primary openbutton'])}}
                      <!--<button type="submit" class="btn btn-default">Sign in</button> -->
                    </div>

{{ Form::close() }} 

这是我的控制器;

    $files = Input::file('images');

    foreach($files as $file) {
        $rules = array(
           'file' => 'required|mimes:png,gif,jpeg,txt,pdf,doc,rtf|max:9999999999'
        );
        $validator = \Validator::make(array('file'=> $file), $rules);
        if($validator->passes()){

            $id = Str::random(14);
            $id = $race . "-" . $id;

            $destinationPath = 'raceImages/' . $id;
            //$filename = $id;
            $filename = $file->getClientOriginalName();
            $mime_type = $file->getMimeType();
            $extension = $file->getClientOriginalExtension();
            $upload_success = $file->move($destinationPath, $id);


            );
        } else {
            return Redirect::back()->with('error', 'I only accept images.');
        }
    }

【问题讨论】:

标签: php image laravel upload resize


【解决方案1】:

这就是我在应用中解决相同问题的方法:

  1. 安装这个包:https://github.com/Intervention/image
  2. 使用此代码:

    $createnew = new Yourmodelname;
     $avatar = Input::file('pic_path');
    if (isset($avatar)) { //will process the code only if an image was properly pointed in the form
    $image = Input::file('pic_path');
    var_dump($image->getRealPath()); // just for error tracking
    $filename = $image->getClientOriginalName();
    if (Image::make($image->getRealPath())->save('foldername/yourprefix_' . $LastInsertId . '_' . $filename)) {      } // foldername is related to your public folder
    if (Image::make($image->getRealPath())->widen(200)->save('foldername/thumbs/thumb_yourprefix_' . $LastInsertId . '_' . $filename)) {
    
    }
    
    $createnew->pic_path = 'event_poster_' . $LastInsertId . '_' . $filename;
    $createnew->pic_thumb = 'event_poster_thumb_' . $LastInsertId . '_' . $filename;
    $createnew->save();
    

    }

现在您有两个文件:一个原始文件(未更改)和一个按比例缩放到宽度 280 的缩略图。 您可以在干预文档中找到其他调整大小的选项。

【讨论】:

    猜你喜欢
    • 2013-09-19
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 2014-03-14
    • 2023-04-11
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多