Laravel通过上传Amazon S3降低了图像质量

时间:2019-02-13 14:17:36

标签: laravel amazon-s3

我正在从右边向s3上传图像,并且在上传时图像质量下降。 我想直接保存在我的Amazon S3存储桶中的映像,以优化虚拟机的磁盘空间 我附上了源代码。

public static function uploadToAmazon($photos, $ad_id)
{

    // Start Uploading to Amazon Cloud
    try {

        // Set loop couter
        $counter = 0;

        // Count photos
        $count_photos      = count($photos);

        // Manage each image
        foreach ($photos as $photo) {

            $preview_name   = 'preview_'   . $counter . '.jpg';
            $thumbnail_name = 'thumbnail_' . $counter . '.jpg';

            $image = Image::make($photo->getRealPath());

            // Get Ration
            $ratio        = $image->width() / $image->height();

            /**
            * Upload first thumbnails
            */
            $targetThumbWidth  = 270 * $ratio;
            $targetThumbHeight = $targetThumbWidth / $ratio;

            // Resize Thumbnail
            $thumbNewSize      = $image->fit($targetThumbWidth, $targetThumbHeight)->stream();

            Storage::disk('s3')->put('photos/'.$ad_id.'/thumbnails/'.$thumbnail_name, $thumbNewSize->__toString());
            Storage::disk('s3')->setVisibility('photos/'.$ad_id.'/thumbnails/'.$thumbnail_name, 'public');

            /**
            * Now upload Preview images
            */
            $resposniveSize = Uploader::resposniveSize($image->width(), $image->height());

            // Resize Thumbnail
            $previewNewSize      = $image->fit($resposniveSize[0], $resposniveSize[1])->stream();

            // Check if Watermark enabled or not
            $is_watermark   = Uploader::is_watermark();

            if ($is_watermark) {

                // Get Watermark Options
                $watermark = $is_watermark['watermark'];
                $position  = $is_watermark['position'];

                // Insert Watermark
                $image->insert($watermark, $position);

            }

            Storage::disk('s3')->put('photos/'.$ad_id.'/previews/'.$preview_name, $previewNewSize->__toString());
            Storage::disk('s3')->setVisibility('photos/'.$ad_id.'/previews/'.$preview_name, 'public');

            // New Counter
            $counter ++;

        }

        if($counter == $count_photos){

            // Return Previews Array
            $previews_array   = array();

            // Return Thumbnails Array
            $thumbnails_array = array();

            for ($i = 0 ; $i <= $count_photos - 1 ; $i++) { 

                array_push($previews_array, '/'.$ad_id.'/previews/preview_'.$i.'.jpg');
                array_push($thumbnails_array, '/'.$ad_id.'/thumbnails/thumbnail_'.$i.'.jpg');

            }

            // Return Data
            $data  = array(
                'previews_array'   => $previews_array, 
                'thumbnails_array' => $thumbnails_array, 
            );

            return $data;

        }else{

            return false;

        }

    } catch (\Exception $e) {

        /*echo $e->getMessage();
        die();*/
        return false;

    }

}

我无法找到图像质量下降的那一刻,是调整尺寸吗?

这是原始图片 enter image description here

这是亚马逊S3中的图像 enter image description here

public static function resposniveSize($originalWidth, $originalHeight)
{
    // Get Ration
    $ratio        = $originalWidth / $originalHeight;

    // New Width
    $targetWidth  = intval(500 * $ratio);

    // New Height
    $targetHeight = intval($targetWidth / $ratio);

    return array($targetWidth, $targetHeight);
}

1 个答案:

答案 0 :(得分:2)

这里遇到的问题是您首先将图像调整为缩略图大小。之后,将其放大到预览大小。这导致质量损失。

所以不要使用:

$thumbNewSize = $image->fit($targetThumbWidth, $targetThumbHeight)->stream();

尝试使用此功能:

$thumbNewSize = $image;
$thumbNewSize->fit($targetThumbWidth, $targetThumbHeight)->stream();

$previewNewSize也一样。

您也可以尝试使用->resize($targetThumbWidth, $targetThumbHeight)代替->fit()