【问题标题】:Laravel 4 upload 1 image and save as multiple (3)Laravel 4 上传 1 张图片并保存为多张 (3)
【发布时间】:2014-04-02 15:53:24
【问题描述】:

我正在尝试使用 laravel 4 制作图像上传脚本。(使用资源控制器)并且我正在使用包 Intervention Image。

我想要的是:上传图片时将其保存为 3 张不同的图片(不同大小)。

例如:

1-foo-original.jpg

1-foo-thumbnail.jpg

1-foo-resized.jpg

这是我到目前为止所得到的......它不起作用或任何东西,但这是我所能得到的。

if(Input::hasFile('image')) {
     $file             = Input::file('image');
     $fileName         = $file->getClientOriginalName();
     $fileExtension    = $file->getClientOriginalExtension();
     $type = ????;

     $newFileName = '1' . '-' . $fileName . '-' . $type . $fileExtension;

     $img =  Image::make('public/assets/'.$newFileName)->resize(300, null, true);
     $img->save();
}

希望有人能帮帮我,谢谢!

【问题讨论】:

    标签: image upload laravel


    【解决方案1】:

    你可以试试这个:

    $types = array('-original.', '-thumbnail.', '-resized.');
    // Width and height for thumb and resized
    $sizes = array( array('60', '60'), array('200', '200') );
    $targetPath = 'images/';
    
    $file = Input::file('file')[0];
    $fname = $file->getClientOriginalName();
    $ext = $file->getClientOriginalExtension();
    $nameWithOutExt = str_replace('.' . $ext, '', $fname);
    
    $original = $nameWithOutExt . array_shift($types) . $ext;
    $file->move($targetPath, $original); // Move the original one first
    
    foreach ($types as $key => $type) {
        // Copy and move (thumb, resized)
        $newName = $nameWithOutExt . $type . $ext;
        File::copy($targetPath . $original, $targetPath . $newName);
        Image::make($targetPath . $newName)
              ->resize($sizes[$key][0], $sizes[$key][1])
              ->save($targetPath . $newName);
    }
    

    【讨论】:

    • 谢谢。有没有办法将原件也放在 de foreach 中? (我也想以与调整大小/缩略图相同的方式调整原始大小)。当我尝试将它放入 foreach 时,我收到 File::copy() 错误。我该怎么做??
    【解决方案2】:

    试试这个

    $file = Input::file('userfile');
    $fileName = Str::random(4).'.'.$file->getClientOriginalExtension();
    $destinationPath    = 'your upload image folder';
    
    // upload new image
    Image::make($file->getRealPath())
    // original
    ->save($destinationPath.'1-foo-original'.$fileName)
    // thumbnail
    ->grab('100', '100')
    ->save($destinationPath.'1-foo-thumbnail'.$fileName)
    // resize
    ->resize('280', '255', true) // set true if you want proportional image resize
    ->save($destinationPath.'1-foo-resize-'.$fileName)
    ->destroy();
    

    【讨论】:

      猜你喜欢
      • 2022-10-17
      • 1970-01-01
      • 2015-04-14
      • 2015-06-18
      • 1970-01-01
      • 2016-11-22
      • 2019-09-05
      • 2018-04-28
      • 1970-01-01
      相关资源
      最近更新 更多