gd没有将白色背景添加到透明PNG

时间:2012-02-24 19:55:35

标签: php image-processing gd

我有一个小问题。我的脚本通过一个基于url查询的目录。在执行此操作时,它会为这些图像创建缩略图。现在的问题是它创建了PNG缩略图,但它不是透明或至少是白色背景,而是标准黑色。

以下是代码可以在代码中指出我的问题,或者可能是我可能错过的东西

<?php

$folder = $_GET['folder']; //POST if from form, GET if from URL
$thisdir = getcwd();
if(!file_exists($thisdir ."/"."$folder/thumbs")) {
    mkdir($thisdir ."/"."$folder/thumbs" , 0777);
}

function returnimages($dirname="") {

    $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
    $handle  = opendir($dirname);
    while(false !== ($filename = readdir($handle))) {
        if(eregi($pattern, $filename)){ //if this file is a valid image
            $files[] = $filename;
        }
    }
    if (count($files)<>0) {
        sort($files);
    }

    $curimage=0;

    while($curimage !== count($files)){
        $cropfile=$dirname.'/'.$files[$curimage];echo '<br>'.$cropfile;

         if(preg_match('/[.](jpg)|(jpeg)$/', $cropfile)) {
             $source_img = imagecreatefromjpeg($cropfile);
         } elseif(preg_match('/[.](png)$/', $cropfile)) {
             imagealphablending($source_img, false);
             imagesavealpha($source_img, true);
             $source_img = imagecreatefrompng($cropfile);
         } elseif(preg_match('/[.](gif)$/', $cropfile)) {
             $source_img = imagecreatefromgif($cropfile);
         } else {
             echo "Code 43: Unable to read file type.";
             exit(0);
         }

        if (!$source_img) {
            echo "could not create image handle";
            exit(0);
        }
        $new_w = 480;
        $new_h = 480;

        $orig_w = imagesx($source_img);
        $orig_h = imagesy($source_img);

        $w_ratio = ($new_w / $orig_w);
        $h_ratio = ($new_h / $orig_h);

        if ($orig_w > $orig_h ) {//landscape from here new
            $crop_w = round($orig_w * $h_ratio);
            $crop_h = $new_h;
            $src_x = ceil( ( $orig_w - $orig_h ) / 2 );
            $src_y = 0;
        } elseif ($orig_w < $orig_h ) {//portrait
            $crop_h = round($orig_h * $w_ratio);
            $crop_w = $new_w;
            $src_x = 0;
            $src_y = ceil( ( $orig_h - $orig_w ) / 2 );
        } else {//square
            $crop_w = $new_w;
            $crop_h = $new_h;
            $src_x = 0;
            $src_y = 0;
        }
        $dest_img = imagecreatetruecolor($new_w,$new_h);
        imagecopyresampled($dest_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h); //till here
        if(imagejpeg($dest_img, $dirname."/thumbs/".$files[$curimage], 80)) {
            imagedestroy($dest_img);
            imagedestroy($source_img);
        } else {
            echo "could not make thumbnail image";
            exit(0);
        }
        $curimage++;
    }
}
returnimages($name=$folder);

?>

1 个答案:

答案 0 :(得分:0)

这是因为您使用imagejpeg作为每种可能类型图像的输出函数。我在while循环中更改了代码以使其正常工作:

// Add these defines somewhere at the top
define('TYPE_JPEG', 0);
define('TYPE_PNG', 1);
define('TYPE_GIF', 2);

// ... skipping some code. Here goes while loop iterating over every image
while($curimage !== count($files)){
    $cropfile=$dirname.'/'.$files[$curimage];echo '<br>'.$cropfile;

     // Determine type of the image
     if(preg_match('/[.](jpg)|(jpeg)$/', $cropfile)) {
         $source_img = imagecreatefromjpeg($cropfile);
         $type = TYPE_JPEG;
     } elseif(preg_match('/[.](png)$/', $cropfile)) {
         $source_img = imagecreatefrompng($cropfile);
         $type = TYPE_PNG;
     } elseif(preg_match('/[.](gif)$/', $cropfile)) {
         $source_img = imagecreatefromgif($cropfile);
         $type = TYPE_GIF;
     } else {
         echo "Code 43: Unable to read file type.";
         exit(0);
     }

    if (!$source_img) {
        echo "could not create image handle";
        exit(0);
    }
    $new_w = 480;
    $new_h = 480;

    $orig_w = imagesx($source_img);
    $orig_h = imagesy($source_img);

    $w_ratio = ($new_w / $orig_w);
    $h_ratio = ($new_h / $orig_h);

    if ($orig_w > $orig_h ) {//landscape from here new
        $crop_w = round($orig_w * $h_ratio);
        $crop_h = $new_h;
        $src_x = ceil( ( $orig_w - $orig_h ) / 2 );
        $src_y = 0;
    } elseif ($orig_w < $orig_h ) {//portrait
        $crop_h = round($orig_h * $w_ratio);
        $crop_w = $new_w;
        $src_x = 0;
        $src_y = ceil( ( $orig_h - $orig_w ) / 2 );
    } else {//square
        $crop_w = $new_w;
        $crop_h = $new_h;
        $src_x = 0;
        $src_y = 0;
    }
    $dest_img = imagecreatetruecolor($new_w,$new_h);
    $dest_path = $dirname."/thumbs/".$files[$curimage];
    switch ($type) {
        case TYPE_JPEG:
            imagecopyresampled($dest_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h);
            $success = imagejpeg($dest_img, $dest_path, 80);
            break;

        case TYPE_PNG:
            // Preserve alpha
            imagesavealpha($dest_img, true);
            // Create transparent color
            $color = imagecolorallocatealpha($dest_img, 0, 0, 0, 127);
            // Fill in background
            imagefill($dest_img, 0, 0, $color);
            // Copy from source
            imagecopyresampled($dest_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h);
            $success = imagepng($dest_img, $dest_path);
            break;

        default:
            $black = imagecolorallocate($dest_img, 0, 0, 0);
            // This will make the background transparent
            imagecolortransparent($dest_img, $black);
            // Copy from source
            imagecopyresampled($dest_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h);
            $success = imagegif($dest_img, $dest_path);
            break;
    }
    if($success) {
        imagedestroy($dest_img);
        imagedestroy($source_img);
    } else {
        echo "could not make thumbnail image";
        exit(0);
    }
    $curimage++;
}

如您所见,根据文件类型,我们使用不同的输出函数:imagejpegimagepngimagegif。 PNG和GIF文件需要一些额外的操作才能保持透明度。我在Windows下使用JPEG,GIF和PNG(透明和常规)文件在PHP 5.2.13上进行了测试,看起来一切正常。您可以调整其他参数,例如imagepng采用可选的$quality$filters参数。

另外,关于代码:

  • 最重要的规则:不要信任用户输入。您正在根据$_GET的值创建目录,这可能不安全,因为用户可以传递类似'/../../../../ etc'
  • 一些清理不会受到伤害。使用foreach代替while循环计数器。我认为glob可以检索文件列表。而returnimages($name=$folder);电话对我来说没有意义。你真的需要分配和拨打一行吗?
相关问题