Laravel 4上传1张图片并保存为多张(3) [英] Laravel 4 upload 1 image and save as multiple (3)

查看:173
本文介绍了Laravel 4上传1张图片并保存为多张(3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make an image upload script with laravel 4. (using Resource Controller) and i'm using the package Intervention Image.

我是什么想要的是:上传图片时将其保存为3张不同的图片(不同尺寸)。

And what i want is: when uploading an image to save it as 3 different images (different sizes).

例如:

1-foo-original.jpg

1-foo-original.jpg

1-foo-thumbnail.jpg

1-foo-thumbnail.jpg

1-foo- resized.jpg

1-foo-resized.jpg

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

This is what i got so far.. it's not working or anything, but this was as far as i could get with it.

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

希望有人可以帮助我,谢谢!

Hopefully someone can help me out, thanks!

推荐答案

你可以试试这个:

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

这篇关于Laravel 4上传1张图片并保存为多张(3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆