473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

GD & Transparent Gifs

devonknows
137 100+
Ok ive been wracking my brains over this for absolutely ages and cant find a simple answer anywere not even on the php.net site so i was wondering if you could help me.

I have designed a gif file in photoshop with transparent backgrounds, but when i use
[PHP]$im = imagecreatefromgif($imgname)[/PHP]
the transparent images are showing up as white and i cant seem to figure out how to keep the transparent background within the new image.

Surrounding Code
[PHP]
$im = imagecreatefromgif($imgname);

$imsize = getimagesize($imgname);
$colorTransparent = imagecolortransparent($im);
$im2 = imagecreate($imsize['0'],$imsize['1']);

imagepalettecopy($im2,$im);
imagefill($im2,0,0,$colorTransparent);
imagecolortransparent($im2, $colorTransparent);
imagecopyresized($im2,$im,0,0,0,0,$imsize['0'],$imsize['1'], $imsize['0'], $imsize['1']);
[/PHP]

I would be most appreciative if someone can help me with this cus ive been looking for hours and cant find anything.

Kind Regards
Devon
Aug 7 '08 #1
2 2868
MarkoKlacar
296 Expert 100+
Hi,

The image, do you display it in a browser? In that case what browser?

I've seen similar cases where the transparent area goes black or white.

/MK
Sep 22 '08 #2
Atli
5,058 Expert 4TB
Hi.

I remember testing this, and the only way I could keep the transparency was to set the transparent color (via imagecolortransparent) of the new image to black.

Seeing as your backgrounds are showing white, maybe this should be white in your case.

Trying to get the transparent color of the original image (like you do) never worked.

This worked for me:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Create original container
  3. $original = imagecreatefromgif($pathToOriginal);
  4.  
  5. // Calculate new size
  6. $imgSize = getimagesize($pathToOriginal);
  7. $newx = $imgSize[0] * 0.25;
  8. $newy = $imgSize[1] * 0.25;
  9.  
  10. // Create copy container
  11. $copy = imagecreate($newx, $newy);
  12.  
  13. // Set transparent as black
  14. $cBlack = imagecolorallocate($copy, 0, 0, 0);
  15. imagecolortransparent($copy, $cBlack);
  16.  
  17. // Copy
  18. imagecopyresampled($copy, $original, 0, 0, 0, 0, $newx, $newy, $imgSize[0], $imgSize[1]);
  19. ?>
  20.  
I never used it because it didn't seem reliable, but under controlled circumstances it might work.
Sep 22 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Shiperton Henethe | last post by:
Dreamweaver 4 Hi Can anyone recommend a decent utility for compressing HTML that is safe - i.e. that *definitely* doesn't mess with the appearance in any browsers. I run a growing website...
5
by: Jim | last post by:
Now this is just weird. And yes, I validated the html and the stylesheet. I created a transparent gif image in Paint Shop Pro 7. I made the mistake of making a transparent png and went along...
6
by: Michael Rozdoba | last post by:
I've had some trouble getting IE to behave in respect of applying absolute positioning to a span on an a:hover. I can get it to work, but I don't understand why certain code causes it to fail to...
8
by: djmanmaster | last post by:
Am I right in thinking that images with transparent areas (GIFs or PNGs) should allow clicks to "pass through" to any underlying elements behind them? I have had no luck getting this to work in...
1
by: Chris Tremblay | last post by:
I am trying to create an image using GDI+ in VB.NET. I want to have the background be transparent, but when I do run the following: myGraphicsObject.clear(color.transparent) the resulting...
5
by: Mark Deibert | last post by:
I'm a former VB6 coder. Quit a few years ago. Now I'm back and trying to teach myself VB.NET. I don't remember having this much difficulty learning VB6. I'm totally stuck on something and need your...
9
by: RCS | last post by:
I'm working on an ASP.NET webpage that generates an image (and am using VWD 2005 until my copy of VS2005 ships). I have done all the obvious (and not-so-obvious) things to try to give a gif that I...
2
by: Trond Michelsen | last post by:
Hi. I have a transparent PNG-image that I would like to display on top of the rest of the web page. I've already got this part working. But, I'd like the background (as in "the part of the image...
8
by: salmobytes | last post by:
Making thumbnail images isn't all that hard, for the most part. There is lots of shrink-wrapped code out there you can download and/or munge from. But nothing I've yet seen does the right thing...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.