Jump to content

imagecolortransparent not working as expected - any ideas?


zq29

Recommended Posts

I'm trying to rescale a gif file with transparency but am having difficulty with the imagecolortransparent() function - It's not working as I would hope... I'm telling it to replace the colour red with transparency and then overlay my transparent gif, but resized. For some reason, it is turning anything black in my transparent gif, transparent, instead of the red - Can anyone else figure out why it might be doing this?

 

<?php
//Load source image
$m_source = imagecreatefromgif("im.gif");

//Create destination image
$n_source = imagecreatetruecolor(162,23);
imagegif($n_source,"a.gif");

//Fill the default black image with red
$fill = imagecolorallocate($n_source,255,0,0);
imagefill($n_source,0,0,$fill);
imagegif($n_source,"b.gif");

//Make red fill transparent
imagecolortransparent($n_source,$fill);
imagegif($n_source,"c.gif");

//Copy and resize the source image into the destination image
imagecopyresized($n_source,$m_source,0,0,0,0,162,23,352,50);
imagegif($n_source,"d.gif");
?>

 

I've attached my im.gif if anyone wants to see what I'm seeing.

 

Many thanks :)

 

[attachment deleted by admin]

I know the word 'Rasta' is not pure red, I'm not trying to make that part transparent, pure coincidence on that part. I'm trying to take a gif with a transparent background, resize it, crop it and overlay it over a jpeg. This was the only step that was giving me an issue.

 

Not to worry too much though now, I have rewritten the whole process using ImageMagick - Although GD might have produced a slightly better looking output, I was getting a bit frustrated with it! Might be interesting still though to see why my snippet of code above was not doing as expected though...

 

Thanks for your input guys.

Looks like im.gif background is already transparent so this works (if I understood OK)

 

<?php
$im = imagecreatefromjpeg('desert sands.jpg');
$im1 = imagecreatefromgif('im.gif');
$w1 = imagesx($im1);
$h1 = imagesy($im1);
imagecopyresampled($im, $im1, 40,40,0,0, $w1/2, $h1/2, $w1, $h1);

header ("content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
imagedestroy($im1);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.