gpt4 book ai didi

php - imagejpeg() 没有在 php 中正确给出输出

转载 作者:行者123 更新时间:2023-12-04 06:51:11 26 4
gpt4 key购买 nike

我想从磁盘上传图像,调整其大小,然后将其上传到 Amazon S3。

但是,我无法从 imagejpeg() 获得正确的图像输出。

继承人我的代码:

$sourceUrl = $_FILES['path']['tmp_name'];       
$thumbWidth = '100';
$thumbid = uniqid();
$img = imagecreatefromjpeg($sourceUrl);
$width = imagesx( $img );
$height = imagesy( $img );

// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// output the image
imagejpeg($tmp_img);

// upload thumbnail to s3
$s3->putObjectFile($tmp_img, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);

Firebug 给了我这个错误:
illegal character
[Break on this error] (�����JFIF���������>CREATOR: g...(using IJG JPEG v62), default quality\n

如果我这样修改 imagejpeg,
imagejpeg($tmp_img, 'abc.jpg');

然后我得到同样的错误。 :(

我可以在这里得到一些帮助吗?

最佳答案

如果您查看 imagejpeg 的文档你可以看到它输出图像,这意味着你调用它的方式被发送到浏览器。您可以通过在第二个参数中传递文件名,以第二种调用方式将其保存到文件中。

另外,$tmp_img是图像资源,而不是现成的图像文件。

我不知道您的上传功能如何工作,但是:如果您需要上传文件内容,请这样做:

ob_start();
imagejpeg($tmp_image);
$image_contents = ob_get_clean();
$s3->putObjectFile($image_contents, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);

如果您需要上传文件名:
$filename = tempnam(sys_get_temp_dir(), "foo");
imagejpeg($tmp_image, $filename);
$s3->putObjectFile($filename, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);

关于php - imagejpeg() 没有在 php 中正确给出输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3100695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com