上传到亚马逊s3时调整图像大小

时间:2017-06-29 12:07:47

标签: php amazon-web-services amazon-s3

我需要将图片大小调整为150 x 150像素,然后将其上传到Amazon S3

以下是代码:

               $image = $_FILES["userImage"]["name"];

                $fileTempName = $_FILES['userImage']['tmp_name'];

                $new_width  = 150;
                $new_height = 150;

                $image_p    = imagecreatetruecolor($new_width, $new_height);

                $image      = imagecreatefromstring(file_get_contents($fileTempName));
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, imagesx($image), imagesy($image));

                $newFielName = tempnam(sys_get_temp_dir(), "tempfilename"); 
                imagepng($image_p, $newFielName, 9); 

                 $s3 = new S3(awsAccessKey, awsSecretKey);

                //move the file
                if ($s3->putObjectFile($fileTempName, "urimages", $newFielName, S3::ACL_PUBLIC_READ)) {

                    $image_link = 'https://s3-us-west-2.amazonaws.com/urimages/' . $newFielName . '';

                    $this->Product->saveField('image', $image_link);

                } 

以下是我在上传时收到的链接:https://s3-us-west-2.amazonaws.com/urimages/C:/Users/LS/AppData/Local/Temp/9102.tmp

可以请你帮我调试代码

1 个答案:

答案 0 :(得分:0)

  

我认为路径问题。请在s3上创建文件夹并使其有效   根据该文件夹的路径

     

https://s3-us-west-2.amazonaws.com/urimages/C:/Users/LS/AppData/Local/Temp/9102.tmp

Example :- Resampling an image proportionally

<?php
// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 200;
$height = 200;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>