Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Reducing all image types quality [closed]

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

like image 628
Ron Avatar asked Dec 07 '12 17:12

Ron


3 Answers

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');
like image 59
harikrishnan.n0077 Avatar answered Nov 12 '22 15:11

harikrishnan.n0077


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?

like image 3
Tiger-222 Avatar answered Nov 12 '22 14:11

Tiger-222


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");  
like image 2
mattsches Avatar answered Nov 12 '22 16:11

mattsches