5

I am looking for a library that can reduce all image types quality (PNG, GIF and JPEG).
I know that I can reduce JPEG using imagejpeg()
I also know that I can reduce PNG using imagepng() although this is not powerful enough.

I need something that can convert PNG 24 to PNG8 without removing the alpha.

Can't use ImageMagick since I cannot install anything on my server.

EDIT:
I also need something that can convert from 32 bit to 8 (which I am pretty sure is the same like from 32)

Found the soultion here

Thanks

Community
  • 1
  • 1
Ron
  • 3,975
  • 17
  • 80
  • 130
  • 2
    Maybe I'm missing something, but converting from PNG24 to PNG8 by definition removes alpha data. – ceejayoz Dec 07 '12 at 17:29
  • 1
    Nope, your absolutely right. PNG8 is PNG without Alpha. Its just the RBG channels combined, no seperate R G B + Alpha Channel. – Dorvalla Dec 07 '12 at 17:34
  • so how come I can save pictures as PNG 8 in Photoshop keeping the alpha? – Ron Dec 07 '12 at 17:50
  • 2
    People are very mistaken here. I have many PNG8 images (grayscale ones) with alpha data. – mmgp Dec 07 '12 at 17:59
  • PNG 24 is similar to 8, but has support for 16 million colors and will preserve color variations such as gradients better and help prevent "banding." PNG 8 can have alpha but since it's only 8 bit it has less colors (256). I am not wrong. – Ron Dec 07 '12 at 18:01
  • 3
    @Ron, I know you are not. It looks like they are thinking PNG always need a full alpha channel (thus PNG X+8) to have alpha data. Here is a very simple example PNG8 with alpha (I can upload a color version that is PNG8 and contains alpha data if that seems impossible for whatever reason): http://s16.postimage.org/lkbjf58kh/gearsgrayscale.png – mmgp Dec 07 '12 at 18:07
  • 2
    Check [TinyPng](http://tinypng.org) -- they're doing pretty much the same thing. You can easily create a script that sends the image to their service and then get it back. I can provide example code, if this sounds interesting to you. – Emil M Dec 16 '12 at 07:42
  • There seems a minor contradiction in your question. You're looking for a library, but you can't install a library. Imagemagick is definitely a good tool for what you are trying to do. How about having clients pull converted images from a separate server that has configuration you can control? Or perhaps implement image conversion as a SOAP service, again on hardware you can control, provided to the locked production server? Both could be done "in the cloud," for example on Heroku, which will run the rmagick gem for you if you implement in Ruby. It's probably possible in PHP, too. – Gene Dec 16 '12 at 17:26
  • EmilM - I didn't think about using another service but it might be a good solution if I want to have even lower size. Gene - library doesn't have to be installed, it can be standalone. for example lets look on jQuery in JavaScript - its library and you don't have to install it... – Ron Dec 17 '12 at 04:34

4 Answers4

5

To convert any PNG image to 8-bit PNG use this function, I've just created

function convertPNGto8bitPNG()

  function convertPNGto8bitPNG($sourcePath, $destPath) {
    $srcimage = imagecreatefrompng($sourcePath);
    list($width, $height) = getimagesize($sourcePath);
    $img = imagecreatetruecolor($width, $height);
    $bga = imagecolorallocatealpha($img, 0, 0, 0, 127);
    imagecolortransparent($img, $bga);
    imagefill($img, 0, 0, $bga);
    imagecopy($img, $srcimage, 0, 0, 0, 0, $width, $height);
    imagetruecolortopalette($img, false, 255);
    imagesavealpha($img, true);
    imagepng($img, $destPath);
    imagedestroy($img);
    }

Parameters

$sourcePath - Path to source PNG file $destPath - Path to destination PNG file Usage

convertPNGto8bitPNG('pfc.png', 'pfc8bit.png');
harikrishnan.n0077
  • 1,927
  • 4
  • 21
  • 27
3

I know you do not want GD library, but with this code:

$img = imagecreatefrompng($src);
imagesavealpha($img, true);
imagepng($img, $dst, 9, PNG_ALL_FILTERS);
imagedestroy($img);

The optimized picture is about 40% smaller than the original size and alpha is not removed.

What percent of optimization are you looking for?

hakre
  • 193,403
  • 52
  • 435
  • 836
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
  • 1
    this just compressing the image and DOES save alpha, yet saved with the original bits of the image. saving image as png 8 without applying any filter will reduce its size by 65% atleast (checked in photoshop). – Ron Dec 14 '12 at 20:36
2

Please try this, is this the result you're looking for?

<?php
$size = getimagesize("original.png");
$source = imagecreatefrompng("original.png");
$target = imagecreate($size[0], $size[1]);
imagecopy($target, $source, 0, 0, 0, 0, $size[0], $size[1]);
imagepng($target,"copy.png");  
mattsches
  • 583
  • 4
  • 6
2

Through experience I have with PHP, I always had some difficulty in relation to image processing (using GD, standard library).
I believe without any PHP extension is not indicated. I suggest you use some extension that will facilitate the work, for example: ImageMagick

After installing the extension, try the following:

# Load your image
$im = new Imagick('your-png32-image.png'); 
# Set quality/colors
$im->setImageFormat('PNG8');
$colors = min(255, $im->getImageColors());
$im->quantizeImage($colors, Imagick::COLORSPACE_RGB, 0, false, false);
# 8bits
$im->setImageDepth(8);
# Save image
$im->writeImage('your-png8-image.png');
Fred Wuerges
  • 1,965
  • 2
  • 21
  • 42