Re: Scaling image is losing background transparency [message #171679 is a reply to message #171664] |
Sat, 15 January 2011 12:57 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma:
|
Senior Member |
|
|
Thomas 'PointedEars' Lahn wrote:
> laredotornado(at)zipmail(dot)com wrote:
>
>> I'm using PHP 5 and trying to scale png images programatically. Here
>> is what I'm doing ...
>>
>> $src_img = imagecreatefrompng($imagefile);
>> $hw = getimagesize($imagefile);
>> $new_w = $w;
>> $new_h = $hw["1"]/($hw["0"]/$w);
>> $dst_img = @imagecreatetruecolor($new_w, $new_h);
>> if(!$dst_img) {
>> $dst_img = imageCreate($new_w, $new_h);
>> }
>> imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,
>> $new_h,imagesx($src_img),imagesy($src_img));
>> imagepng($dst_img);
>>
>> The problem is my scaled png image loses its background transparency.
>> The short of this is, does anyone know how to scale PNG images
>> effectively and what other information can I provide to help
>> troubleshoot this further?
>
> I suppose it can be done with the GD library (which you are using), but you
> need to preserve the pixels' alpha channel value (that which you misnamed
> "background transparency"). There is an imagesavealpha() function that
> looks promising.
>
Yes. I made it work here.
I ended up using gif mind you because IE6 couldn't suss tranaparent
background PNG's. Lemme see if I have some code frags somewhere.
Hmm. I remember now. I found the documentation on transparency, opaque.
to say the least. ;-)
$image=imagecreatetruecolor ($needle_width, $needle_height );
imagealphablending($image, true);
//set up arbitrary color for transparent background
$back=imagecolorallocatealpha ($image ,255 , 255 , 255 , 255);
That last but one line seems to be needed or else anything copied into
it loses alpha.
The final line is a 'transparent' colour I created to fill it with.
imagefilledrectangle ($image ,0,0,$backx-1 ,$backy-1, $back );
After some other stuff - copying transparent gifs over it..to build up
a layered picture. I resized and spat it out this way....
$thumbnail=imagecreatetruecolor($newsize,$newsize);
imagecopyresampled($thumbnail,$image,0,0,0,0,$newsize,$newsize,$needle_widt h,$needle_height);
imagecolortransparent ( $thumbnail, $back);
header('content-type: image/gif');
imagegif($thumbnail);
The imagecolortransaprent seems to set that color I set up = $back - to
transparent.
Now some of this may be redundant. I 'fiddled till it worked'
BUT work it does..
So passed on with usual health warnings.
>
> PointedEars
|
|
|