[英]PHP - Script using imagepng won't save file and then display it
I'm trying to load an image, edit it, save it and then display it from a script that is called within IMG tags. 我正在尝试加载图像,对其进行编辑,保存,然后从在IMG标签内调用的脚本中进行显示。 The script works if I want to just display the image and it does save the image.
如果我只想显示图像,脚本可以工作,并且确实可以保存图像。 But it won't save it and then display it.
但是它不会保存然后显示。 Does anyone know what I'm doing wrong?
有人知道我在做什么错吗? Any help would be greatly appreciated.
任何帮助将不胜感激。
<?php
header('Content-Type: image/png');
$file_location = "test.png";
if (file_exists($file_location)) {
$img_display = @imagecreatefrompng($file_location);
// This section of code removed as doesn't affect result
imagepng($img_display, $file_location);
chmod($file_location, 0777);
imagepng($img_display);
imagedestroy($img_display);
}
?>
Try this and checks if your folder has permission to save the image. 尝试此操作,并检查您的文件夹是否具有保存图像的权限。 chmod 777 on it for sure.
chmod 777肯定可以。
<?php
header('Content-Type: image/png');
$file_location = "test.png";
if (file_exists($file_location)) {
$img_display = imagecreatefrompng($file_location); // Create PNG image
$filename = $file_location + time(); // Change the original name
imagepng($img_display, $filename); // Saves it with another name
imagepng($filename); // Sends it to the browser
imagedestroy($img_display); // Destroy the first image
imagedestroy($filename); // Destroy the second image
}
Try this: 尝试这个:
ob_start();
imagepng($img_display);
$contents = ob_get_clean();
file_put_contents($file_location, $contents);
echo $contents;
imagedestroy($img_display);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.