php imagecopyresampled - 保存后,图像为空(全黑)

时间:2013-07-28 07:51:23

标签: php image file resize

此脚本将加载图像(jpg,gif或png),然后保存PNG本地副本以进行缓存。

我试图找到一种方法将图像大小调整为300x300,然后再将其保存为PNG。

我尝试使用imagecopyresampled()函数,但图像仍未调整大小。

现在有2个问题:

  • 脚本将调整大小的PNG图像保存在正确的文件夹中,但图像为空(全黑)

  • 第一次加载图片时,我会收到错误(图片无法显示,因为它包含错误),但图片仍将保存为缓存文件夹中的PNG。第二次加载图像时,它将正确显示(使用缓存版本),但不会调整大小。

这是我的页面的完整代码。第一部分用于缓存图像,第二部分用于显示非缓存图像(它从ZIP文件中读取图像并输出内容而不提取任何内容)

if (empty($_GET['display'])) {
header('Content-Type: image/png');

            $imgpochette = $_GET['i'];

            $ENABLE_CACHE = true;
            $CACHE_TIME_HOURS = 744;
            $CACHE_FILE_PATH = "pochette_album/$imgpochette.png";

            if($ENABLE_CACHE && file_exists($CACHE_FILE_PATH) && (time() - filemtime($CACHE_FILE_PATH) < ($CACHE_TIME_HOURS * 60 * 60))) {
              echo @file_get_contents($CACHE_FILE_PATH);
            } else {
                    // Load the requested image
                    $imgdisplay = "http://www.pirate-punk.com/pochette.php?i=$imgpochette&display=1";
                    $image = imagecreatefromstring(file_get_contents($imgdisplay));
$width = "30";
$height = "30";
list($originalWidth, $originalHeight) = getimagesize($CACHE_FILE_PATH);
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight);
                    // Send the image
                    imagepng($new_image, $CACHE_FILE_PATH);
                    exit();
              @file_put_contents($CACHE_FILE_PATH, $output);
              echo $output;
            }

}







if (!empty($_GET['display'])) {
        function showimage($zip_file, $file_name) {
            $z = new ZipArchive();
            if ($z->open($zip_file) !== true) {
                echo "File not found.";
                return false;
            }

            $stat = $z->statName($file_name);
            $fp   = $z->getStream($file_name);
                // search for a path/to/file matching file, returning the index of it
                $index = $z->locateName($file_name, ZipArchive::FL_NOCASE|ZipArchive::FL_NODIR);
                // get the name of the file based on the index
                $full_file_name = $z->getNameIndex($index);
                // now get the stream
                $fp = $z->getStream($full_file_name);

            if(!$fp) {
                echo "Could not load image.";
                return false;
            }

            header('Content-Type: image/jpeg');
            header('Content-Length: ' . $stat['size']);
            fpassthru($fp);
            return true;
        }

        $imgsrcencoded = $_GET['i'];
        $imagesrc = base64_decode($imgsrcencoded);
        $explodez = explode("#",$imagesrc);
        $imgg = utf8_encode($explodez[1]);
        $dirnfile = $explodez[0];
        $zipp = end((explode('/', $dirnfile)));
        $dirr = str_replace($zipp,"",$dirnfile);
        $dirr = rtrim($dirr,"/");
        $imgg = rtrim($imgg);
        chdir($dirr);
            if (empty($_GET['debug'])) {
            echo showimage($zipp,$imgg);
            }
}

1 个答案:

答案 0 :(得分:1)

获取.png张图片的solution