2015-10-07 39 views
4

我想上传一些调整大小的图像到S3,但不知何故所有图像都有相同的大小。以不同尺寸在本地存储不会产生任何问题。我错过了什么?调整上传的图像在Laravel和存储在S3不起作用

public function uploadFileToS3(Request $request) { 
    $image = Image::make($request->file('image'))->encode('jpg', 75); 
    $s3 = Storage::disk('s3'); 

    $image_file_name = $this->generateName($request->name) . '.jpg'; 
    $file_path = '/' . config('folder') . '/' . $request->name . '/'; 

    $s3->put($file_path.'original_'.$image_file_name, $image, 'public'); 
    $s3->put($file_path.'medium_'.$image_file_name, $image->fit(300, 300), 'public'); 
    $s3->put($file_path.'thumb_'.$image_file_name, $image->fit(100, 100), 'public'); 

    return json_encode(array(
     'filename' => $image_file_name 
    )); 
} 

所有版本都成功地存储在S3中,只有都在同尺寸

回答

6

我有两个可能的解决方案。

尝试做图像处理完全试图存储在他们面前:

$s3->put($file_path.'original_'.$image_file_name, $image, 'public'); 
$image->fit(300, 300); 
$s3->put($file_path.'medium_'.$image_file_name, $image, 'public'); 
$image->fit(100, 100); 
$s3->put($file_path.'thumb_'.$image_file_name, $image, 'public'); 

尝试将图像转换为字符串,实际的输出文件的内容应该很好地工作:

$s3->put($file_path.'original_'.$image_file_name, (string) $image, 'public'); 
0

...嗯,我设法解决这个问题,首先将调整大小的图像存储到本地tmp文件夹,然后上传它。这是一个解决方案,我真的不喜欢很多,因为我认为它太多的代码应该在Laravel中处理。

$folder = $_SERVER['DOCUMENT_ROOT'] . '/tmp/'; 
    $image_file_name = $this->generateName($request->name) . '.jpg'; 

    // save original 
    $image->save($folder . 'original_' . $image_file_name); 
    $s3->put($file_path.'original_'.$image_file_name, fopen($folder.'original_'.$image_file_name, 'r+'), 'public'); 

    // generate thumb 
    $image = $image->fit(100, 100); 
    $image->save($folder . 'thumb_' . $image_file_name); 
    $s3->put($file_path.'thumb_'.$image_file_name, fopen($folder.'thumb_'.$image_file_name, 'r+'), 'public'); 
相关问题