EllisLab text mark
Advanced Search
1 of 2
1
   
Resizing of transparent/animated gifs [solved]
Posted: 17 October 2007 10:02 AM
Avatar
Joined: 2007-08-22
27 posts

Hi there, people.

Did anybody here encounter any problems with CI imagelib when working (i.e. resizing) with animated gifs or gifs with transparency? In my case the resulting image gets slightly messed up—all of transparent areas are black.

I know that this is not CI’s fault, but a general problem with native PHP functions such as imagecopyresampled(), which discard transparency and every frame but first one of the animated gif. My question is, actually, did anyone here already solved this on CI’s grounds? Some magick patches to the core applied, maybe?

 Signature 
$config['log_threshold'2
 
Posted: 17 October 2007 10:15 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-10-11
166 posts

I too have same problem, please somebody answer. #1313 if i am correct says right that its core PHP problem, but there must be some solutions to these type of high end image manipulation.

 Signature 

http://www.nirbhab.com/  |  Follow Me On Twitter

 
Posted: 17 October 2007 10:18 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-22
27 posts

Well, yes, there are some techniques, like http://forums.devshed.com/php-development-5/gd-image-resize-transparent-gif-337812.html, but my question was actually about the working implementation on CI.

 Signature 
$config['log_threshold'2
 
Posted: 17 October 2007 11:11 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-23
3194 posts

Try changing the image lib library.  GD2 is better then GD, and Imagemagik might be better still (I don’t know).  There’s also NetPBM if you have access to it.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design

 
Posted: 23 October 2007 07:55 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-22
27 posts

There are no native functions capable of resizing transparent gifs in GD2.


So i wrote a little patch to the image_lib image_process_gd() function (actually, this patch is copied from PHP Online Manual):

0. backup your \system\libraries\Image_lib.php
1. locate following line of code in the \system\libraries\Image_lib.php (somewhere in the image_process_gd() function, near line #512)

$dst_img $create($this->width$this->height); 

2. insert these lines right after that line:

// keeping transparency 
        
$transparent_index imagecolortransparent($src_img);
        if (
$transparent_index >= 0{
            imagepalettecopy
($src_img$dst_img);
            
imagefill($dst_img00$transparent_index);
            
imagecolortransparent($dst_img$transparent_index);
            
imagetruecolortopalette($dst_imgtrue256);
        

3. test and see if this helped

In my case this patch completely did the magic and now my transparent gifs (and pngs) remain transparent after resizing. Hope this’ll help someone.

 Signature 
$config['log_threshold'2
 
Posted: 15 November 2007 06:57 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2007-03-12
8 posts

@ #1313

Thank you!

 
Posted: 07 April 2008 07:41 PM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-03-20
51 posts

Hi There,

just wondering if Derecks patch made it into CI 1.6.1?
I’m not getting transparency after resizing png’s (using 1.6.1)

may I should just go and look wink

 
Posted: 23 August 2009 03:09 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2009-08-23
1 posts

Using 1.70


Solved the problem inserting only these lines:

$transparent_index imagecolortransparent($src_img);

imagepalettecopy($src_img$dst_img);
imagefill($dst_img00$transparent_index);
imagecolortransparent($dst_img$transparent_index);
imagetruecolortopalette($dst_imgtrue256); 
Fabian Wesner - 15 November 2007 11:57 AM

@ #1313

Thank you!

+1

 
Posted: 15 September 2009 08:28 AM   [ # 8 ]   [ Rating: 0 ]
Joined: 2008-09-26
12 posts

Thank you very much #1313 for the code. It indeed worked.

Thank you so much…

 
Posted: 15 September 2009 10:01 AM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-10
778 posts

Great code, much appreciated. Was wondering what the hell was wrong with some of my images but not others.

 Signature 

CreativeHalls Web Design and Printing
A few of my projects:
OurGulfCoast Property Management and Vacation Rental (ASP/.NET)
BukuBux - Money Saving Coupons and Gift Certificates (CodeIgniter, LAMP/MySQL)
Rentals800.com - Find a place to rent (CodeIgniter, LAMP/MySQL)
bdh (dot) hall (at) gmail (dotcom)

 
Posted: 15 September 2009 10:21 AM   [ # 10 ]   [ Rating: 0 ]
Joined: 2009-09-15
1 posts

Great post! Thanks a lot for sharing this… It would really be a big help…

 
Posted: 19 September 2009 10:18 AM   [ # 11 ]   [ Rating: 0 ]
Joined: 2008-09-26
12 posts

At first impression I thought I got it working by using #1313 but unfortunately it didn’t for me.
I got two problems by keeping the if condition and by removing it ref: #1313 code
1. The resized image came with a black background OR
2. The resized image came in black and white color with transparent background.

Finally I used the below code found in php.net

Find Code:

$dst_img $create($this->width$this->height); 

I inserted the below code after the above line as specified by #1313

$transparencyIndex imagecolortransparent($src_img);
$transparencyColor = array('red' => 255'green' => 255'blue' => 255);

if (
$transparencyIndex >= 0{
    $transparencyColor    
imagecolorsforindex($src_img$transparencyIndex);   
}

$transparencyIndex    
imagecolorallocate($dst_img$transparencyColor['red']$transparencyColor['green']$transparencyColor['blue']);
imagefill($dst_img00$transparencyIndex);
imagecolortransparent($dst_img$transparencyIndex); 
 
Posted: 17 April 2010 12:49 PM   [ # 12 ]   [ Rating: 0 ]
Joined: 2009-09-15
1 posts

Thanks vigna, i hade the same problem. Your solution worked like a charm!

edit: This should really be put into the real lib smile
edit: The png’s were f*cked up though, if you have white in the middle it will get transparent

 
Posted: 15 July 2010 11:41 PM   [ # 13 ]   [ Rating: 0 ]
Joined: 2009-07-22
3 posts

This worked for me:

// keep transparency
$transparent_index imagecolortransparent($src_img);
if (
$transparent_index >= 0)
{
    imagepalettecopy
($src_img$dst_img);
    
imagefill($dst_img00$transparent_index);
    
imagecolortransparent($dst_img$transparent_index);
    
imagetruecolortopalette($dst_imgtrue256);
}
elseif ($this->image_type == 3)
{
    imagealphablending
($dst_imgfalse);
    
$transparent_index imagecolorallocatealpha($src_img000127);
    
imagefill($dst_img00$transparent_index);
    
imagesavealpha($dst_imgtrue);

Hope it helps.
- Landon

 
Posted: 25 November 2010 07:21 AM   [ # 14 ]   [ Rating: 0 ]
Joined: 2010-05-27
13 posts

replace the line

$copy($dst_img$src_img00$this->x_axis$this->y_axis$this->width$this->height$this->orig_width$this->orig_height); 

with the following at line number 512 or around it in Image_lib.php. it maintains transparency for both gif and png images. note the imagefilledrectangle() replacing
imagefill(). with imagefill() the transparent part becomes black. but imagefilledrectangle() maintains transparency.

$proprts         $this->get_image_properties($this->full_src_pathTRUE);
        
$cr_img_type    $proprts['image_type'];
        
//$cr_width        = $proprts['width'];
        //$cr_height        = $proprts['height'];
                    
                    
        
if ($cr_img_type == 3)//for png
        
{
                        
            imagealphablending
$dst_imgfalse );
            
imagesavealpha$dst_imgtrue );
            
$transparent_index imagecolorallocatealpha($dst_img,0,255,0,127);
         
//  imagefill($dst_img, 0, 0, $transparent_index);
            
imagefilledrectangle($dst_img00$this->width$this->height$transparent_index);
            
$copy($dst_img$src_img00$this->x_axis$this->y_axis$this->width$this->height$this->orig_width$this->orig_height);
           
        

        

        
        
else if($cr_img_type == 1)//for gif
        
        
{
             $transparent_index 
imagecolortransparent($src_img);
                  if (
$transparent_index >= 0{
                      imagepalettecopy
($src_img$dst_img);
                    
//  imagefill($dst_img, 0, 0, $transparent_index);
                      
imagefilledrectangle($dst_img00$this->width$this->height$transparent_index);
                      
imagecolortransparent($dst_img$transparent_index);
                      
imagetruecolortopalette($dst_imgtrue256);
                  

                    
            $copy
($dst_img$src_img00$this->x_axis$this->y_axis$this->width$this->height$this->orig_width$this->orig_height);
        
}
        
else//for other
       
{
            
        $copy
($dst_img$src_img00$this->x_axis$this->y_axis$this->width$this->height$this->orig_width$this->orig_height);    
        
        
 
Posted: 24 December 2010 04:47 AM   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2010-05-14
23 posts
landons - 16 July 2010 03:41 AM

This worked for me:

// keep transparency
$transparent_index imagecolortransparent($src_img);
if (
$transparent_index >= 0)
{
    imagepalettecopy
($src_img$dst_img);
    
imagefill($dst_img00$transparent_index);
    
imagecolortransparent($dst_img$transparent_index);
    
imagetruecolortopalette($dst_imgtrue256);
}
elseif ($this->image_type == 3)
{
    imagealphablending
($dst_imgfalse);
    
$transparent_index imagecolorallocatealpha($src_img000127);
    
imagefill($dst_img00$transparent_index);
    
imagesavealpha($dst_imgtrue);

Hope it helps.
- Landon

Thanks Landon, this one works the best.

 
1 of 2
1