简体   繁体   中英

imagepng() of gd library not working in php

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

}

?>

I want to resize the png images to png images .According to my code imagepng() function is not working .Always execute the else condition and print not tested.I have checked that gd library is installed .

Value of compression quality must between 0 and 9 for png. I think, it will fix the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM