I want to add a new feature in my present image upload script and that is automatic compression of images just like TinyPng.Basically i want a code snippet that detect 24bit PNG file and convert it to 8bit png file with full support for transparency and that also strip any metadata present

Any php based solution would do ,all i found till now use command line and i don't know how to implement these in php.
I need a solution in php please

Thanks for the guidance

Recommended Answers

All 5 Replies

@diafol thanks for the referance but how do detect if an image is 24bit or 8bit.
Also from googling i found that php gd quality isn't that good and thread didn't say anything about metadata
Also i don't want to convert png to jpg

Member Avatar for diafol

I thought the thread actually created 8bit pngs not jpg.

Try this for info:

$array = get_png_imageinfo($filename);
print_r($array);

Also from googling i found that php gd quality isn't that good and thread didn't say anything about metadata

I thought I'd leave you something to do for yourself. :)

Thanks for the tip diafol
Really dude i must admit that i am well verse with image properties and a rookie when it comes to image conversion.
Its a project that i am passionate about and makes me learn new stuff

True Color Image

Array
 (
     [width] => 400
     [height] => 100
     [bit-depth] => 8
     [color] => 6
     [compression] => 0
     [filter] => 0
     [interface] => 0
     [color-type] => Truecolour with alpha
     [channels] => 4
     [bits] => 8
 )

Image Compressed by TinyPNG

 Array
 (
     [width] => 400
     [height] => 100
     [bit-depth] => 8
     [color] => 3
     [compression] => 0
     [filter] => 0
     [interface] => 0
     [color-type] => Indexed-colour
     [channels] => 1
     [bits] => 8
 )

Using the formula
[channels] * [bits] , the image has been converted from 24bit to 8 bit
I used this site to learn more about transparency.
How can this be achieve in php ?
which element in array represent transparency?
How color-type can be converted to index color?

Wow all this questions are poping in my head now.
Anyone can provide a short and yet understand explaination with code sample if possible...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.