【发布时间】: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/… 。它显示损坏的图像
-
请查看您发布的链接中显示的文字。这不是损坏的图像。
-
在向用户提供图像时,您可以使用可以为您完成的图像调整服务,而不是调整图像大小。请参阅:Cloudinary 和 Imgix
标签: php amazon-web-services amazon-s3