2015-09-15 24 views
1
<?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条件和打印不tested.I已经检查过安装了gd库。imagepng(GD库)在PHP不工作

+0

你'ob_end_clean()'或类似的地方? – Rasclatt

回答

0

对于png,压缩质量的值必须介于0和9之间。 我认为,它会解决这个问题。