Jump to content

PNG Thumbnails


graham23s

Recommended Posts

Hi Guys,

 

My thumbnail function doesn't seem to make a .png thumbnail but it works on .jpg & .gif but not png files can anyone see any errors in my code at all i don't see any:

 

code:

 

function resize_image($uploadDirectory, $newFileName)
{
    $original_image = $uploadDirectory;
    
    $ext = substr($original_image, strrpos($original_image, '.') + 1);
    
    $canvas_width  = 65;
    $canvas_height = 65;
    
    $canvas = imagecreatetruecolor($canvas_width, $canvas_height);
    
    $white_background = imagecolorallocate($canvas, 255, 255, 255);
    
    imagefill($canvas, 0, 0, $white_background);
    
    list($image_width, $image_height) = getimagesize($uploadDirectory);
    
    $ratio = $image_width / $image_height;
    
    if ($ratio > 1) {
        $new_image_width  = 65;
        $new_image_height = 65 / $ratio;
        
    } //$ratio > 1
    else {
        $new_image_width  = (float) 65 * $ratio;
        $new_image_height = 65;
        
    }
    
    
    if ($ext == "jpg") {
        $original_image = imagecreatefromjpeg($original_image);
        
    } //$ext == "jpg"
    
    if ($ext == "gif") {
        $original_image = imagecreatefromgif($original_image);
        
    } //$ext == "gif"
    
    if ($ext == "png") {
        $original_image = imagecreatefrompng($original_image);
        
    } //$ext == "png"
    
    imagecopyresampled($canvas, $original_image, 0, 0, 0, 0, $new_image_width, $new_image_height, $image_width, $image_height);
    
    $new_thumbnail_name = "thumb-$newFileName";
    
    if ($ext == "jpg") {
        imagejpeg($canvas, "../imgProducts/img-th/$new_thumbnail_name", 100);
        return ("$new_thumbnail_name");
        
    } //$ext == "jpg"
    
    if ($ext == "gif") {
        imagegif($canvas, "../imgProducts/img-th/$new_thumbnail_name", 100);
        return ("$new_thumbnail_name");
        
    } //$ext == "gif"
    
    if ($ext == "png") {
        imagepng($canvas, "../imgProducts/img-th/$new_thumbnail_name", 100);
        return ("$new_thumbnail_name");
        
    } //$ext == "png"
    
    imagedestroy($original_image);
    imagedestroy($canvas);
    
}

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/195936-png-thumbnails/
Share on other sites

try using 9 instead of 100

 

9 is the maximum for a png, and 0 is minimum.

 

also try adding the filter.

 

 

imagepng($canvas, "../imgProducts/img-th/$new_thumbnail_name", 9, PNG_ALL_FILTERS);

 

edit:

your gif shouldn't work either, because it only accepts two parameters, and not 3. take out the last parameter, there is no quality parameter for a gif.

Link to comment
https://forums.phpfreaks.com/topic/195936-png-thumbnails/#findComment-1029211
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.