PHP-使用imagepng的脚本获胜';t保存文件然后显示


PHP - Script using imagepng won't save file and then display it

我正在尝试加载一个图像,编辑它,保存它,然后从IMG标记中调用的脚本中显示它。如果我只想显示图像,并且它确实保存了图像,那么脚本就可以工作。但它不会保存然后显示。有人知道我做错了什么吗?如有任何帮助,我们将不胜感激。

<?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);
    }
?>

试试这个,检查你的文件夹是否有保存图像的权限。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
    } 

试试这个:

ob_start();
imagepng($img_display);
$contents = ob_get_clean();
file_put_contents($file_location, $contents);
echo $contents;
imagedestroy($img_display);