无法保存上传的 PNG 文件
我可以上传 JPG 文件,但不能上传 PNG 文件。
PNG 图像未保存,但输出此 ‰PNG À À°£ûïS Jäž ŸØ™Ù<íû;
我的代码是这样的:
$source_gd_image = imagecreatefrompng( $sourcefile ); // <-- If PNG file
$target_gd_image = imagecreatetruecolor( $target_image_width, $target_image_height );
if($source_image_type == IMAGETYPE_PNG) {
imagealphablending($target_gd_image, true); // Should this be false?
imagesavealpha($target_gd_image, true);
imagepng($target_gd_image); // <-- This is where the gibberish is outputed
}
if($source_image_type == IMAGETYPE_JPEG) {
imagecopyresampled( $target_gd_image, $source_gd_image, 0, 0, 0, 0, $target_image_width, $target_image_height, $source_image_width, $source_image_height );
imagejpeg( $target_gd_image, $target_file, $quality );
}
imagedestroy( $source_gd_image );
imagedestroy( $target_gd_image );
我做错了什么?
I'm able to upload JPG files, but not PNG files.
The PNG image is not saved, but ouputs this ‰PNG À À°£ûïS JÄž Ÿ¬™Ù<íû;
My code is like this:
$source_gd_image = imagecreatefrompng( $sourcefile ); // <-- If PNG file
$target_gd_image = imagecreatetruecolor( $target_image_width, $target_image_height );
if($source_image_type == IMAGETYPE_PNG) {
imagealphablending($target_gd_image, true); // Should this be false?
imagesavealpha($target_gd_image, true);
imagepng($target_gd_image); // <-- This is where the gibberish is outputed
}
if($source_image_type == IMAGETYPE_JPEG) {
imagecopyresampled( $target_gd_image, $source_gd_image, 0, 0, 0, 0, $target_image_width, $target_image_height, $source_image_width, $source_image_height );
imagejpeg( $target_gd_image, $target_file, $quality );
}
imagedestroy( $source_gd_image );
imagedestroy( $target_gd_image );
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像
imagejpeg
一样,您必须指定 文件名作为第二个参数 如果要将其保存到文件中,否则它只会输出到浏览器。假设 $target_file 是所需的输出文件名,这意味着使用:
Just like with
imagejpeg
, you have to specify a file name as the second parameter if you want to save it to a file, otherwise it just gets output to the browser.Assuming $target_file is the desired output file name, that means using: