Transparency...

images php coding issues or problems here.

Moderators: macek, egami, gesf

jfuller
New php-forum User
New php-forum User
Posts: 3
Joined: Tue May 27, 2003 6:14 am

Transparency...

Postby jfuller » Tue May 27, 2003 6:19 am

I have written a script that uploads a jpg, enters an entry in a mysqldb, renames the jpg to the id of the entry then resamples the jpg and saves it in the same folder but with "tn_" appended to the beginning of the file name and it saves it as a png instead of jpg.

my question is this...
how do i determine the colour usage of the png? (in this instance I would like it to use 64 colours or somthing like that) with transparency set to purewhite (#ffffff)

any help would be appreciated.
Thanks.
Jamie Fuller

User avatar
mike
New php-forum User
New php-forum User
Posts: 73
Joined: Sun May 04, 2003 4:26 am
Location: Athens
Contact:

Postby mike » Thu May 29, 2003 2:21 pm

imagecolortransparent()

int imagecolortransparent ( resource image [, int color])


imagecolortransparent() sets the transparent color in the image image to color. image is the image identifier returned by imagecreate() and color is a color identifier returned by imagecolorallocate().
bla ... bla... (check on manual)

jfuller
New php-forum User
New php-forum User
Posts: 3
Joined: Tue May 27, 2003 6:14 am

Postby jfuller » Thu May 29, 2003 11:36 pm

mike wrote:imagecolortransparent()

int imagecolortransparent ( resource image [, int color])


imagecolortransparent() sets the transparent color in the image image to color. image is the image identifier returned by imagecreate() and color is a color identifier returned by imagecolorallocate().
bla ... bla... (check on manual)

Hi Mike,

Sorry if I sound rude but I'm not so dumb as to not read the manual, In my post I described the complicated script I had already created, You can't create that without searching the manual quite a few times. All your post has served to do is make people think a (sensible) reply has been given therefore no need for them to offer help. :evil:

I have used that code but it simply doesnt create the transparency information, (although it did once but on totally the wrong colour. :) )

heres my code

Code: Select all

// Load image from jpg
    $src_img = ImageCreateFromJPEG($imgdir.$img);

// Get image size
    $org_h = imagesy($src_img);
    $org_w = imagesx($src_img);

// Initialize destination image
    $dst_img = imagecreatetruecolor(80,60);

// Resize it
    ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, 80, 60, $org_w, $org_h);

// Convert to 256 colours, set transparency and then save it!
    imagetruecolortopalette ($dst_img, false, 255);
    $white = imagecolorallocate ($dst_img, 255, 255, 255);
    imageColorTransparent($dst_img, $white);
    ImagePNG($dst_img, $imgdir."tn_".$img);

// clean up
    imageDestroy($src_img);
    imageDestroy($dst_img);


Cheers
Jamie

User avatar
pootergeist
New php-forum User
New php-forum User
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK
Contact:

Postby pootergeist » Fri May 30, 2003 7:37 am

As a transparency has existed I would guess (yup, guess as I've not used transparencies on png [which should in most instancies use an alpha channel as opposed to index - though obviously IE cannot display alpha-transparency pngs and moz has a few snags still])

imagecolorallocate is attributing white to a second index, whereas you want the primary index to take the transparency - a workaround would be

$indx = imagecolorclosest($img, 255, 255, 255);
$white = ImageColorSet($img, $indx, 255, 255, 255); // resets that index to true white

using that rather than imagecolorallocate *should* work better theoretically - though you will obviously have problems due to basing your images creates on speckled-lossy jpegs which never have good colour holding ability

jfuller
New php-forum User
New php-forum User
Posts: 3
Joined: Tue May 27, 2003 6:14 am

Postby jfuller » Fri May 30, 2003 8:44 am

Thanks for your help, I tried that code but to no avail,

All it did was set one of the off-white colour indexes to pure-white, there was still no transparency info saved :(

I would have thought this was quite simple but it is still escaping me!...arrrgghh.

THanks for your idea though, keep 'em coming! :)
Cheers

User avatar
pootergeist
New php-forum User
New php-forum User
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK
Contact:

Postby pootergeist » Sat May 31, 2003 2:04 am

note: a few comments in the online manual state that transparency only works for black


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest

cron