|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
GD image resize transparent gif
hey i was wondering if anyone knew the best way to resample a transparent gif using PHP/GD library. Currently this is the closest i've got:
PHP Code:
basically the plan with this was to find the colors in the resized image that were opaque and then just generate some random color to use as the transparency. when i convert it to a palette image some of the transparent color goes transparent but i get weird spots of color appearing: i'm out of ideas lol anybody else got any? ![]() |
|
#2
|
|||
|
|||
|
I would just re-size the image, dump it back to a file, then read the first 13 bytes of the image file to get the transparent index, if it has one, then allocate that color as transparent, and use imagecopy(); to set the transparency back. Using IO functions, fopen(), fread(), is a lot more resource friendly than using some of the core image functions, because they read LEN, when you only need to read offset to offset for any information you need. The (height, width, type, size, background color index, (?) is transparent) is found in the first 13 bytes for GIF!
printf! |
|
#3
|
|||
|
|||
|
I have been knocking my head against the wall with this problem as well. I am using gd from Perl but the library is the same as far as I know. It seems that conversion from true color to palette does respect the transparent color. I have tried various ways of guessing at the color based on what it used to be but have not found any reliable way to set the transparent color.
If any one finds the magic formula, I also would be grateful. For an application where you want to let the user upload a .gif, chop it down and then display it another way is to output it as a png and generate the appropriate JS to render the PNG properly in IE using the Alpha filter. Not a great solution but PNG conversion work much better (as long as you set and fill transparency in the destination image before resampling). |
|
#4
|
|||
|
|||
|
Solution is in php manual
setting this to true resolved the problem for me :
imagetruecolortopalette($imgDestination5, true, 256); |
|
#5
|
|||
|
|||
|
Resize transparent GIFs using masks
To securely resize transparent GIFs I would recommend using a negative and a positive mask. I would have posted a link to the page where I explain the whole process in detail, but it seems I am not allowed to.
If you would like to know the URL you can send me a private message. Martin |
|
#6
|
|||
|
|||
|
Quote:
PHP Code:
Bear in mind that this only works for indexed-to-indexed resizing, and that this won't look that great when resized because there are only 255 colors (not counting the transparent index) available when dithering. That's why resampling to a Truecolor image looks a lot better. However, what was once one color is now multiple colors (for example, an anti-aliased curved line), and the single transparent color is no longer helpful. Unlike ImageMagick, GD (to my knowledge) has no concept of threshold to sort of get around this. Your best bet is to avoid resizing transparent GIFs altogether. |
|
#7
|
|||
|
|||
|
Its very possible to generalize this and generate the proper transparency, most PHP GD image resizers don't ever do it the right way and usually see a black background. Check out a post I wrote on this subject at: mummey.org/2008/11/transparent-gifs-with-php-and-gd/
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > PHP Development > GD image resize transparent gif |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|