PHP-上传时调整图像大小并保持原样

PHP-上传时调整图像大小并保持原样,php,image,laravel,upload,resize,Php,Image,Laravel,Upload,Resize,我正在使用Laravel4创建一个站点,它允许管理员一次上传大量图像。目前,我能够做到这一点,每个图像都有自己独特的ID,并放入自己的文件夹中,命名为相同的ID 问题是,我需要应用程序也上传第二个,调整大小(更小)的图像版本连同原始图像。我了解到必须在客户端调整图像大小,因此我不确定如何保存原始图像以及较小的版本。较小的图像应使用相同的ID命名,并在名称的末尾带有某种标识符,如“-size” 这是我目前的前端 {{ Form::open(array('url' => 'imageU

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

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

这是我目前的前端

    {{ 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() }} 

以下是我在应用程序中解决相同问题的方法:

  • 安装此软件包:
  • 使用此代码:

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

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

    为什么不让服务器调整图像大小?这是我想做的,但我只看到在前端进行了调整大小。如何调整服务器端的大小?您看过imagemagick库吗?请参见,特别是,或者您可以查看干预:
    $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();