gd库的imagepng()无法在php

时间:2015-09-15 05:15:02

标签: php gd

<?php
ob_start();
if (extension_loaded('gd') && function_exists('gd_info')) {
    echo "PHP GD library is installed on your web server";
} else {
    echo "PHP GD library is NOT installed on your web server";
}


ini_set('max_execution_time', 0);
$ImagesDirectory     = '/var/www/html/images1/images2/';
$DestImagesDirectory = '/var/www/html/images1/images3/';
$NewImageWidth       = 21;
$NewImageHeight      = 21;
$Quality             = 80;
$ext1                = "png";
if ($dir = opendir($ImagesDirectory)) {
    while (($file = readdir($dir)) !== false) {

        $imagePath       = $ImagesDirectory . $file;
        $destPath        = $DestImagesDirectory . $file;
        $checkValidImage = @getimagesize($imagePath);

        if (file_exists($imagePath) && $checkValidImage) {

            if (resizeImage($imagePath, $destPath, $NewImageWidth, $NewImageHeight, $ext1)) {
                echo $file . ' resize Success!<br />';


            } else {
                echo $file . ' resize Failed!<br />';
            }
        }
    }
    closedir($dir);
}

function resizeImage($target, $newcopy, $w, $h, $ext)
{

    list($w_orig, $h_orig) = getimagesize($target);
    $scale_ratio = $w_orig / $h_orig;
    if (($w / $h) > $scale_ratio) {
        $w = $h * $scale_ratio;
    } else {
        $h = $w / $scale_ratio;
    }
    $img = "";

    $img = imagecreatefrompng($target);
    echo $img;
    $tci = imagecreatetruecolor($w, $h);
    echo $tci;
    imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
    if (imagepng($tci, $newcopy, 80)) {
        echo "tested";

    } else {
        echo "not tested";
    }

}

?>

我想将png图像调整为png图像。根据我的代码,imagepng()函数无效。始终执行else条件并打印未测试。我检查过安装了gd库。

1 个答案:

答案 0 :(得分:0)

对于png,压缩质量的值必须在0到9之间。 我认为,这将解决问题。