【问题标题】:Resize image while uploading to amazon s3上传到亚马逊 s3 时调整图像大小
【发布时间】:2017-06-29 20:19:37
【问题描述】:

我需要将图像大小调整为 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

你能帮我调试一下代码吗

【问题讨论】:

  • 您没有提到任何错误消息或您在使用此代码时遇到的任何问题 - 究竟需要调试什么?请提供您面临的问题的相关信息,以便社区可以帮助您。
  • @Lix 上传后我得到了 url s3-us-west-2.amazonaws.com/urimages/C:/Users/LS/AppData/Local/… 。它显示损坏的图像
  • 请查看您发布的链接中显示的文字。这不是损坏的图像。
  • 在向用户提供图像时,您可以使用可以为您完成的图像调整服务,而不是调整图像大小。请参阅:CloudinaryImgix

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


【解决方案1】:

我认为路径有问题。请在 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);
?>

【讨论】:

  • 从文档中复制粘贴一个示例非常容易,不是吗?
  • 您至少可以添加一个link back to the documentation,以便人们可以阅读更多有关该主题的内容。
  • 我知道您只是想提供帮助 - 但您在这里所做的只是将文档扔给提出问题的人。
  • @Lix 抱歉,我将添加文档链接
猜你喜欢
  • 2014-05-19
  • 2012-07-07
  • 2013-11-24
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 2017-03-22
相关资源
最近更新 更多