/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Replace color with PHP

Hello again,

I have a transparent PNG24 and I want to change a color but I have some problem, can you help me ?

I have a PNG24 with 2 col + trasparent, i convert it by [B]imagetruecolortopalette($im, false, 256);[/B]

so I check every color and if it is the color to replace I substituite it. It seem to be soimple but during this I lost the trasparent color which must be in 256,256,256! why ?!

If I set a color to 256,256,256 it comes black!

If I ask to print the color number ([B]imagecolorstotal[/B]) the result is 2 (and not 2+1), and if ask to print the colors i saw different value from phortoshop value:

the color in photoshop 252,252,252 is 252,254,252 in PHP why ?

How I can replace a color from an image with 2 colors and transparent ? I prefere use the png24.

thnk! for your time!

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@bokehApr 07.2008 — Hard to know what you are saying. Are you saying you have a 24 bit PNG which only contains 3 colours an you want to convert to 8 bit PNG.
Copy linkTweet thisAlerts:
@uncokeauthorApr 07.2008 — Thanks a lot for you interesting to my problem!

I have reduced my question to [B]merge 2 tranparent PNG24 images[/B].

It is seem to be simple... but i'm not able!

If I run the code below, the last one image covers the others, the results it is an only one image (the last one), it is transparent but it is alone!

If i cut of the bold code I saw all the images but into the black box...

Thanks a lot for your help!!!


Here my code:

[code=php]<?php

$logo = $urlo."logo.png";
$file_base = "../../core/lab/manof/01/base.png";
$top = "../../core/lab/manof/01/top.png";
$demo = "../../core/lab/manof/01/demo.png";
$star = "../../core/lab/manof/01/star.png";

$x = 350;
$y = 488;

$base = imagecreatefrompng($file_base);
$dst = imagecreatetruecolor($x,$y);

[B]imagealphablending($dst,false);
imagesavealpha($dst,true);[/B]

$back = imagecolorallocatealpha($dst,0,0,0,127);
imagefill($dst,0,0,$back);

imagealphablending($base,false);
imagesavealpha($base,true);
imagecopy($dst,$base,0,0,0,0,$x,$y);

$src = imagecreatefrompng($top);
imagealphablending($src,false);
imagesavealpha($src,true);
imagecopyresampled($dst,$src,0,0,0,0,$x,$y,$x,$y);
imagedestroy($src);

$src = imagecreatefrompng($star);
imagealphablending($src,false);
imagesavealpha($src,true);
imagecopyresampled($dst,$src,0,0,0,0,$x,$y,$x,$y);
imagedestroy($src);

$logo = "demo.png";
imagepng($dst,$logo);

?>[/code]
Copy linkTweet thisAlerts:
@bokehApr 08.2008 — try imagecopyresized. Also why do you need $dst? Why not just copy one image to the other?
Copy linkTweet thisAlerts:
@uncokeauthorApr 08.2008 — try imagecopyresized. Also why do you need $dst? Why not just copy one image to the other?[/QUOTE]

i did it and i did a lot of times and ways. Now I'm updating from php4 to php5, maybe it is a bug...
Copy linkTweet thisAlerts:
@uncokeauthorApr 08.2008 — It's the same on php5 and GD 2 (2 0 34).

I'm starting mad!

All I want to do is... simple, merge a transparent png over another one.

But it works only if the first one image is a PNG but no transparent!

Maybe I have to consider to use 8 bit png. But why?!
Copy linkTweet thisAlerts:
@bokehApr 10.2008 — You marked this resolved but you didn't say how you fixed it.

The following worked for me:[code=php]<?php

$file_1 = "left-hand.png";
$file_2 = "right-hand.png";

// open image 1
$image_1 = imagecreatefrompng($file_1);
imageAlphaBlending($image_1, false);
imageSaveAlpha($image_1, true);
$x1 = imagesx($image_1);
$y1 = imagesy($image_1);

// open image 2
$image_2 = imagecreatefrompng($file_2);
imageAlphaBlending($image_2, false);
imageSaveAlpha($image_2, true);
$x2 = imagesx($image_2);
$y2 = imagesy($image_2);

// make a transparent background
$slate = imagecreatetruecolor(max($x1, $x2), max($y1, $y2));
$transparent = imagecolorallocatealpha($slate,0,255,0,127);
imagefill($slate,0,0,$transparent);

// now do the copying
imagecopy($slate, $image_1, 0, 0, 0, 0, imagesx($image_1)-1, imagesy($image_1)-1);
imagecopy($slate, $image_2, 0, 0, 0, 0, imagesx($image_2)-1, imagesy($image_2)-1);

// for the background do this after copying is finished
imageAlphaBlending($slate, false);
imageSaveAlpha($slate, true);

header('Content-Type: image/png');
imagepng($slate);

?>[/code]
I've attached the source and resulting images.

[upl-file uuid=0759b92c-5555-43a3-80b9-c8dfaae204ed size=64kB]merged.png[/upl-file]

[upl-file uuid=dde1a01f-c0f3-4f2e-935b-f6f0e8e96d76 size=68kB]left-hand.png[/upl-file]

[upl-file uuid=aa8f0244-dea9-46c8-aaff-d9573b0716e1 size=64kB]right-hand.png[/upl-file]
Copy linkTweet thisAlerts:
@uncokeauthorApr 10.2008 — You marked this resolved but you didn't say how you fixed it.[/QUOTE]

THANKS!!!

I didn't resolved it, but i used an alternative way; i used the first png not transparent, then i merged the second transparent PNG and then I take off the background color from the final image. It was bad solution! But just to continue the general work.

I'll read your code soon! To see how you resolved it! In the while I have to say thanks, really thanks for your time! I appreciated it a lot!

I'll write you tomorrow after to have studied your code! cu! And thanks again!

sergioc
Copy linkTweet thisAlerts:
@uncokeauthorApr 13.2008 — Thankx Bokeh!

I have checked your script and it is NOT WORKING on my server (there is a black box background), so finally I know why I was going mad...

Mine and your script are [B]not working[/B] on PHP Version 4.3.10 and (2.0.28 compatible)

and they are [B]working[/B] on PHP 5.2.5 and GD 2.0.34. It is great, now I know why!

Thanks again! You are a very kind man! So I will ask you another question...

I use a php (that php script) to save on the server a "live" image. In the header f the page will use that image i have

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<meta http-equiv="Pragma" content="no-cache">

<meta http-equiv="Cache-Control" content="no-cache">

<META HTTP-EQUIV="Expires" CONTENT="Mon, 06 Jan 1990 00:00:01 GMT">

<meta http-equiv="Expires" content="0">


But it is not enough... several times I need to refresh the page to see the last one image... Do you have some idea to see the last one image whan i open the web page ?

thanks again!

uncoke
Copy linkTweet thisAlerts:
@bokehApr 13.2008 — Get rid of all that jazz. Instead do this:[code=php]echo "<img src='", $image_url, '?', time(), "'>";[/code]
×

Success!

Help @uncoke spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...