簡體   English   中英

gd不向透明PNG添加白色背景

[英]gd not adding white background to transparent PNG

好吧,我有一個小問題。 我的腳本通過基於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);

?>

這是因為您將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); 電話對我來說沒有意義。 您真的需要在一行中分配和呼叫嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM