Skip to content
Advertisement

Resize PNG image in PHP

I’m getting a no image display when resizing PNG however the following code works for JPEG.

list($width_orig, $height_orig) = getimagesize( $fileName );

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);

if( $type )){
    switch( $type ){
        case 'image/jpeg':
            $image = imagecreatefromjpeg($fileName);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
            imagejpeg($image_p, null, 100);
        break;

        case 'image/png':
            imagealphablending( $image_p, false );
            imagesavealpha( $image_p, true );
            $image = imagecreatefrompng( $fileName );
            imagecopyresampled( $image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
            imagepng($image_p, null, 100);
        break;
    }
}

I’ve put the headers in but for some reason I’m doing something wrong for png images.

Advertisement

Answer

Last argument in imagepng($image_p, null, 100) should be between 0 and 9.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement