PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 26th, 2006, 03:25 AM
Rossy Rossy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 5 Rossy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 15 sec
Reputation Power: 0
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:
 $imgSource imagecreatefromgif('source.gif');

$imgDestination3 imagecreatetruecolor(9090);
$imgDestination4 imagecreatetruecolor(9090);

$black imagecolorallocate($imgDestination3000);
$white imagecolorallocate($imgDestination4255255255);

imagefill($imgDestination300$black);
imagefill($imgDestination400$white);

imagecopyresampled($imgDestination3$imgSource00009090$intImage_Width$intImage_Height);
imagecopyresampled($imgDestination4$imgSource00009090$intImage_Width$intImage_Height);

$intImage_Width 90;
$intImage_Height 90;

$arrColors = array();
for(
$i=0$i<$intImage_Width$i++)
{
    for(
$j=0$j<$intImage_Height$j++)
      {
           
$pos1 imagecolorat($imgDestination3$i$j);
           
$color1 imagecolorsforindex($imgDestination3$pos1);
        
        
$pos2 imagecolorat($imgDestination4$i$j);
        
$color2 imagecolorsforindex($imgDestination4$pos2);
        
        if ((
$color1['red'] == $color2['red']) && ($color1['green'] == $color2['green']) && ($color1['blue'] == $color2['blue']))
        {
            
$arrColors[count($arrColors)] = $color1['red'].$color1['green'].$color1['blue'];
        }
     }
}
$red rand(0,255);
$green rand(0,255);
$blue rand(0,255);
$i 0;
while (
$i count($arrColors))
{
    if (
$arrColors[$i] == $red.$green.$blue)
    {
        
$red rand(0,255);
        
$green rand(0,255);
        
$blue rand(0,255);
        
$i 0;
    }
    
    
$i++;
}

$imgDestination5 imagecreatetruecolor(9090);

$transparent imagecolorallocate($imgDestination5$red$green$blue);
imagefill($imgDestination500$transparent);
imagecolortransparent($imgDestination5$transparent);

$intImage_Width imagesx($imgSource);
$intImage_Height imagesy($imgSource);

imagecopyresampled($imgDestination5$imgSource00009090$intImage_Width$intImage_Height);

imagetruecolortopalette($imgDestination5false256);

imagegif($imgDestination5'thumbnail.gif'); 


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?

Reply With Quote
  #2  
Old March 26th, 2006, 10:02 AM
printf printf is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2005
Posts: 1,586 printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 4 Weeks 1 h 32 m 14 sec
Reputation Power: 270
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!

Reply With Quote
  #3  
Old April 6th, 2006, 09:23 PM
magixman magixman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 magixman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 24 sec
Reputation Power: 0
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).

Reply With Quote
  #4  
Old May 5th, 2006, 10:10 AM
nonopoubelle nonopoubelle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 1 nonopoubelle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 57 sec
Reputation Power: 0
Solution is in php manual

setting this to true resolved the problem for me :
imagetruecolortopalette($imgDestination5, true, 256);

Reply With Quote
  #5  
Old January 20th, 2007, 07:47 AM
martinvie martinvie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 1 martinvie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 26 sec
Reputation Power: 0
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

Reply With Quote
  #6  
Old April 1st, 2007, 09:21 PM
mratzloff mratzloff is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 2 mratzloff User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m
Reputation Power: 0
Quote:
Originally Posted by printf
The (height, width, type, size, background color index, (?) is transparent) is found in the first 13 bytes for GIF!
For anyone trying to figure this out, I just wanted to point out that printf is slightly incorrect--the transparent index is not in the GIF header block (the first 13 bytes). Instead, it is specified in the graphic control extension (or each frame's graphic control extension, for animated GIFs). But that's beside the point; to determine the transparent index for an image using PHP and GD, simply use
PHP Code:
 $transparentIndex imagecolortransparent($image); 
Then get the color for this index and allocate it for the new image.

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.

Reply With Quote
  #7  
Old March 13th, 2009, 03:34 AM
GenghisJuan GenghisJuan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 1 GenghisJuan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 37 sec
Reputation Power: 0
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/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > GD image resize transparent gif


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
Stay green...Green IT