/    Sign up×
Community /Pin to ProfileBookmark

PHP & GD: imagecolorset for PNG-24

I’ve been working with an image layering code for a while now. Currently I have one that is working properly but it will only recolor images saved in PNG-8 which makes the images look rather choppy. Ideally, I would like to be able to recolor my images saved in PNG-24 but I’m having trouble finding a code for this if its even possible. My current code is layering everything properly, just omits the recoloring of the “stockings2.png” file. Any advice is GREATLY appreciated! I’ve been ripping my hair out over this ? Here is my current code:

[code=php]
<?php
(“Content-type: image/png”);

include “dbconnect.php”;
$hid=$_GET[‘id’];

// Get new dimensions
list($width, $height) = getimagesize(‘horses/ridingpony/base.png’);
$new_width=$width;
$new_height=$height;

$horsecolor = $_GET[‘color’];

$red = hexdec(substr($horsecolor, 0, 2));
$green = hexdec(substr($horsecolor, 2, 2));
$blue = hexdec(substr($horsecolor, 4, 2));

//load the horse’s base image
$image_p = @imagecreatetruecolor($new_width, $new_height);

$image = imagecreatefrompng(‘horses/ridingpony/background.png’);
imagealphablending($image_p, true);
imagesavealpha($image_p, true);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

$image1 = imagecreatefrompng(‘horses/ridingpony/stockings2.png’);
imagecolorset($image1, 0, $red, $green, $blue);
imagealphablending($image_p, true);
imagecopyresampled($image_p, $image1, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagesavealpha($image_p, true);

$image = imagecreatefrompng(‘horses/ridingpony/shading.png’);
imagealphablending($image_p, true);
imagesavealpha($image_p, true);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

$image = imagecreatefrompng(‘horses/ridingpony/lines.png’);
imagealphablending($image_p, true);
imagesavealpha($image_p, true);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

//create the horse’s image
imagepng($image_p, ‘horseimages/hi.gif’);

print “<img src=horseimages/hi.gif>”;
?>
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ZnupiFeb 16.2009 — Seems like a typical GD limitation. Honestly, I'm not sure you can overcome this using GD (I've worked with GD quite a lot before). You should try using ImageMagick (IMagik for PHP), if you have that available.
Copy linkTweet thisAlerts:
@xxjennauthorFeb 17.2009 — I'm very unfamiliar with ImageMagick, I don't even know where to start but I know my web host does support it.
Copy linkTweet thisAlerts:
@ZnupiFeb 18.2009 — Well, the first question is whether or not your host has the imagick module installed or if they just have imagemagick binaries installed (which would mean you would have to execute system commands in order to use imagemagick). Look for this section in phpinfo():

[img]http://i498.photobucket.com/albums/rr348/Znupi2/phpinfo-imagick.png[/img]

If you have it, then you can use [url=http://php.net/imagick]IMagick[/url] functions and classes. If you don't, you'll have to use it at the command line, see the documentation on the [url=http://imagemagick.org/script/command-line-tools.php]official ImageMagick site[/url].

[b]Edit:[/b] check out [url=http://valokuva.org/]Mikko's blog[/url] for some nice tutorials and example usages of IMagick ?
×

Success!

Help @xxjenn 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.21,
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,
)...