From e582e8b61ef6a1fd75a8fe1d507c67da8d249d4c Mon Sep 17 00:00:00 2001
From: s_wienecke <s_wienecke@web.de>
Date: Fri, 21 Dec 2007 19:14:19 +0100
Subject: [PATCH] transparent_gif

darcs-hash:20071221181419-3d7ce-f05705a0357412a87e7984a041ac5812eea83048.gz
---
 lib/exe/fetch.php | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/lib/exe/fetch.php b/lib/exe/fetch.php
index d29ed9f64..92572b6ef 100644
--- a/lib/exe/fetch.php
+++ b/lib/exe/fetch.php
@@ -324,6 +324,7 @@ function resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
  * resize images using PHP's libGD support
  *
  * @author Andreas Gohr <andi@splitbrain.org>
+ * @author Sebastian Wienecke <s_wienecke@web.de>
  */
 function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
   global $conf;
@@ -349,7 +350,7 @@ function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
   }
   if(!$image) return false;
 
-  if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){
+  if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){
     $newimg = @imagecreatetruecolor ($to_w, $to_h);
   }
   if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
@@ -364,6 +365,25 @@ function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
     imagesavealpha($newimg,true);
   }
 
+  //keep gif transparent color if possible
+  if($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
+    if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
+      $transcolorindex = @imagecolortransparent($image);
+      if($transcolorindex >= 0 ) { //transparent color exists
+        $transcolor = @imagecolorsforindex($image, $transcolorindex);
+        $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
+        @imagefill($newimg, 0, 0, $transcolorindex);
+        @imagecolortransparent($newimg, $transcolorindex);
+      }else{ //filling with white
+        $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
+        @imagefill($newimg, 0, 0, $whitecolorindex);
+      }
+    }else{ //filling with white
+      $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
+      @imagefill($newimg, 0, 0, $whitecolorindex);
+    }
+  }
+
   //try resampling first
   if(function_exists("imagecopyresampled")){
     if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
-- 
GitLab