• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resizing of transparent/animated gifs [solved]

#11
[eluser]Unknown[/eluser]
Great post! Thanks a lot for sharing this... It would really be a big help...

#12
[eluser]vigna[/eluser]
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:
Code:
$dst_img = $create($this->width, $this->height);

I inserted the below code after the above line as specified by #1313
Code:
$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_img, 0, 0, $transparencyIndex);
imagecolortransparent($dst_img, $transparencyIndex);

#13
[eluser]Unknown[/eluser]
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

#14
[eluser]Unknown[/eluser]
This worked for me:

Code:
// keep transparency
$transparent_index = imagecolortransparent($src_img);
if ($transparent_index >= 0)
{
    imagepalettecopy($src_img, $dst_img);
    imagefill($dst_img, 0, 0, $transparent_index);
    imagecolortransparent($dst_img, $transparent_index);
    imagetruecolortopalette($dst_img, true, 256);
}
elseif ($this->image_type == 3)
{
    imagealphablending($dst_img, false);
    $transparent_index = imagecolorallocatealpha($src_img, 0, 0, 0, 127);
    imagefill($dst_img, 0, 0, $transparent_index);
    imagesavealpha($dst_img, true);
}

Hope it helps.
- Landon

#15
[eluser]sidhartha[/eluser]
replace the line
Code:
$copy($dst_img, $src_img, 0, 0, $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.
Code:
$proprts         = $this->get_image_properties($this->full_src_path, TRUE);
        $cr_img_type    = $proprts['image_type'];
        //$cr_width        = $proprts['width'];
        //$cr_height        = $proprts['height'];
                    
                    
        if ($cr_img_type == 3)//for png
        {
                        
            imagealphablending( $dst_img, false );
            imagesavealpha( $dst_img, true );
            $transparent_index = imagecolorallocatealpha($dst_img,0,255,0,127);
         //  imagefill($dst_img, 0, 0, $transparent_index);
            imagefilledrectangle($dst_img, 0, 0, $this->width, $this->height, $transparent_index);
            $copy($dst_img, $src_img, 0, 0, $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_img, 0, 0, $this->width, $this->height, $transparent_index);
                      imagecolortransparent($dst_img, $transparent_index);
                      imagetruecolortopalette($dst_img, true, 256);
                  }
                    
            $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
        }
        else//for other
       {
            
        $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);    
        
        }

#16
[eluser]DaTran[/eluser]
[quote author="landons" date="1279266116"]This worked for me:

Code:
// keep transparency
$transparent_index = imagecolortransparent($src_img);
if ($transparent_index >= 0)
{
    imagepalettecopy($src_img, $dst_img);
    imagefill($dst_img, 0, 0, $transparent_index);
    imagecolortransparent($dst_img, $transparent_index);
    imagetruecolortopalette($dst_img, true, 256);
}
elseif ($this->image_type == 3)
{
    imagealphablending($dst_img, false);
    $transparent_index = imagecolorallocatealpha($src_img, 0, 0, 0, 127);
    imagefill($dst_img, 0, 0, $transparent_index);
    imagesavealpha($dst_img, true);
}

Hope it helps.
- Landon[/quote]
Thanks Landon, this one works the best.

#17
[eluser]naiyumie natsume[/eluser]
thank you

#18
[eluser]Unknown[/eluser]
[quote author="DaTran" date="1293202024"][quote author="landons" date="1279266116"]This worked for me:

Code:
// keep transparency
$transparent_index = imagecolortransparent($src_img);
if ($transparent_index >= 0)
{
    imagepalettecopy($src_img, $dst_img);
    imagefill($dst_img, 0, 0, $transparent_index);
    imagecolortransparent($dst_img, $transparent_index);
    imagetruecolortopalette($dst_img, true, 256);
}
elseif ($this->image_type == 3)
{
    imagealphablending($dst_img, false);
    $transparent_index = imagecolorallocatealpha($src_img, 0, 0, 0, 127);
    imagefill($dst_img, 0, 0, $transparent_index);
    imagesavealpha($dst_img, true);
}

Hope it helps.
- Landon[/quote]
Thanks Landon, this one works the best.[/quote]

And for me, thanks Landon.
+1

Best is to create a 'MY_Image_lib' with a replacement of 'function image_process_gd()' so there's no need to hack the core.
See the user guide here: http://ellislab.com/codeigniter/user-gui...aries.html
(Extending Native Libraries)

#19
[eluser]Grasp Minds[/eluser]
Thank you for the codes but still have a problem in resizing for some gif images. After resizing, image background is green.

So I found another solution from php.net and its working fine!

Code:
//Start code- For transparency//
$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_img, 0, 0, $transparencyIndex);
imagecolortransparent($dst_img, $transparencyIndex);
//End code- For transparency//

Please put the above code after the following line

Code:
$dst_img = $create($this->width, $this->height);

#20
[eluser]Grasp Minds[/eluser]
Improved code!

Code:
//Start- For transparency//
imagecolortransparent($dst_img, imagecolorallocatealpha($dst_img, 0, 0, 0, 127));
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
//End- For transparency//

Please put the above code after the following line

Code:
$dst_img = $create($this->width, $this->height);

Thank You!
Asik


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.