0

I have this piece of code for uploading images. It makes thumbnails with PNG extension with level 9 compression, but the images do not look good. I want just -50% or little more compression of PNG with transparency.

$path_thumbs = "../pictures/thumbs/";
$path_big = "../pictures/";
$img_thumb_width = 140; // 
$extlimit = "yes"; 
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");       
$file_type = $_FILES['image']['type'];
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];

if(!is_uploaded_file($file_tmp)){
    echo "choose file for upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">return</a>";
    exit(); 
}

$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
    echo "dissallowed!  <br>--<a href=\"$_SERVER[PHP_SELF]\">return</a>";
    exit();
}

$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
$rand_name = md5(time());
$rand_name= rand(0,10000);
$ThumbWidth = $img_thumb_width;

if($file_size){
    if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
        $new_img = imagecreatefromjpeg($file_tmp);
    }elseif($file_type == "image/x-png" || $file_type == "image/png"){
        $new_img = imagecreatefrompng($file_tmp);
    }elseif($file_type == "image/gif"){
        $new_img = imagecreatefromgif($file_tmp);
    }

    list($width, $height) = getimagesize($file_tmp);
    $imgratio = $width/$height;

    if ($imgratio>1){
        $newwidth = $ThumbWidth;
        $newheight = $ThumbWidth/$imgratio;
    }else{
        $newheight = $ThumbWidth;
        $newwidth = $ThumbWidth*$imgratio;
    }
    if (@function_exists(imagecreatetruecolor)){
        $resized_img = imagecreatetruecolor($newwidth, $newheight);
    }else{
        die("Error: Please make sure you have GD library ver 2+");
    }
    imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    ImagePng ($resized_img, "$path_thumbs/$rand_name.$file_ext,9");
    ImageDestroy ($resized_img);
    ImageDestroy ($new_img);
}

move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
gre_gor
  • 6,669
  • 9
  • 47
  • 52

2 Answers2

1

For better better quality of the resized image, you should use imagecopyresampled instread of imagecopyresized.

For image transparency you should look at imagesavealpha.
To make it work, you need to enable it, before you resize the image and you also need to disable alpha blending. It's best, to put it just after imagecreatetruecolor.

$resized_img = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($resized_img, false);
imagesavealpha($resized_img, true);

As for the size, you have a typo in your code

ImagePng ($resized_img,"$path_thumbs/$rand_name.$file_ext,9");

should be

ImagePng ($resized_img, "$path_thumbs/$rand_name.$file_ext", 9);

you put your compression level parameter into the filename instead of the function.

And compression level here doesn't mean it will make your filesize much smaller. It's a tradeoff between speed and filesize.
There is a limit how much you can losslessly compress a file. If filesize is a concern, you should compress it with lossy compression like JPEG.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
0

I think (I did not verify) that you should use the function imagecopyresampled instead of imagecopyresize. I think imagecopyresampled has a higher quality. You might also want to start of by using imagecreatetruecolor

Tobias Gassmann
  • 11,399
  • 15
  • 58
  • 92