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

php imagecopyresampled-保存后图像为空(全部为黑色),php,image,file,resize,Php,Image,File,Resize,此脚本将加载图像(jpg、gif或png),然后保存png本地副本以进行缓存 我正试图找到一种方法,在将图像保存为PNG之前,将其大小调整为300x300 我尝试使用函数imagecopyresampled(),但图像仍然没有调整大小 现在有两个问题: 脚本将调整大小的PNG图像保存在正确的文件夹中,但图像为空(全部为黑色) 第一次加载图像时,我会收到一个错误(图像无法显示,因为它包含错误),但图像仍将保存为PNG,保存在缓存文件夹中。第二次加载图像时,它将正确显示(使用缓存版本),但不会调整

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

我正试图找到一种方法,在将图像保存为PNG之前,将其大小调整为300x300

我尝试使用函数imagecopyresampled(),但图像仍然没有调整大小

现在有两个问题:

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

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

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

if(空($\u GET['display'])){
标题('Content-Type:image/png');
$imgpochette=$_GET['i'];
$ENABLE_CACHE=true;
$CACHE\u TIME\u HOURS=744;
$CACHE\u FILE\u PATH=“pochette\u album/$imgpochette.png”;
如果($ENABLE_CACHE&&file_存在($CACHE_file_PATH)&&(time()-filemtime($CACHE_file_PATH)<($CACHE_time_HOURS*60*60))){
echo@file\u get\u内容($CACHE\u file\u路径);
}否则{
//加载请求的映像
$imgdisplay=”http://www.pirate-punk.com/pochette.php?i=$imgpochette&display=1”;
$image=imagecreatefromstring(文件获取内容($imgdisplay));
$width=“30”;
$height=“30”;
列表($originalWidth,$originalHeight)=getimagesize($CACHE\u FILE\u PATH);
$new_image=imageCreateTureColor($width,$height);
imagecopyresampled($new_image,$image,0,0,0,$width,$height,$originalWidth,$originalHeight);
//发送图像
imagepng($new\u image,$CACHE\u FILE\u PATH);
退出();
@文件内容($CACHE\u file\u PATH,$output);
echo$输出;
}
}
如果(!empty($\u GET['display'])){
函数showimage($zip\u文件,$file\u名称){
$z=新的ZipArchive();
如果($z->open($zip_文件)!==true){
回显“未找到文件。”;
返回false;
}
$stat=$z->statName($file\u name);
$fp=$z->getStream($file\u name);
//搜索与文件匹配的路径/到/文件,返回其索引
$index=$z->locateName($file_name,ZipArchive::FL_NOCASE | ZipArchive::FL_NODIR);
//根据索引获取文件名
$full_file_name=$z->getNameIndex($index);
//现在去小溪边
$fp=$z->getStream($full\u file\u name);
如果(!$fp){
echo“无法加载图像。”;
返回false;
}
标题(“内容类型:图像/jpeg”);
标题('Content-Length:'.$stat['size']);
fpassthru($fp);
返回true;
}
$imgsrcencoded=$_GET['i'];
$imagesrc=base64_解码($imgsrcencoded);
$explodez=explode(“#”,$imagesrc);
$imgg=utf8_编码($explodez[1]);
$dirnfile=$explodez[0];
$zipp=end((分解('/',$dirnfile));
$dirr=str_replace($zipp,“,$dirnfile);
$dirr=rtrim($dirr,“/”);
$imgg=rtrim($imgg);
chdir($dirr);
if(空($\u GET['debug'])){
echo showimage($ZIP,$imgg);
}
}

请用我的脚本获取
.png
图像的示例?
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);
            }
}