Image Resizing: Poor jpeg quality and black PNG backgrounds

Posted by azz0r on Stack Overflow See other posts from Stack Overflow or by azz0r
Published on 2010-05-14T09:09:33Z Indexed on 2010/05/14 9:14 UTC
Read the original article Hit count: 303

Filed under:
|

Hello,

I have two issues with my class, basically the quality of the jpeg images is quite poor, but I'm not sure if thats down to my ratio resizing. Ideally I'd like this class to be strict with image sizes and crop into them, but I cant get my head around it.

My main issue is that pngs always have a black bg, does anyone have experience with this happening?

<?php


    class OpenSource_ImageResize {


        function __construct($theFile, $toWhere, $mime, $extension, $newWidth, $newHeight) {

            if ($mime == NULL) {
                $mime = getimagesize($theFile);
                $mime = $mime['mime'];
            }


            if ($mime == 'image/jpeg') {
                $size = getimagesize($theFile);

                if ($size[0] > $newWidth || $size[1] > $newHeight) {
                    $sourceImage = imagecreatefromjpeg($theFile);
                } else {
                    return copy($theFile, $toWhere);
                    throw new exception('Could not create jpeg');
                    return false;
                }
            } else if ($mime == 'image/png') {
                $size = getimagesize($theFile);

                if ($size[0] > $newWidth || $size[1] > $newHeight) {
                    $sourceImage = imagecreatefrompng($theFile);
                } else {
                    return copy($theFile, $toWhere);
                    //throw new exception('Could not create png');
                    return false;
                }
            } else if ($mime == 'image/gif') {
                $size = getimagesize($theFile);

                if ($size[0] > $newWidth || $size[1] > $newHeight) {
                    $sourceImage = imagecreatefromgif ($theFile);
                } else {
                    return copy($theFile, $toWhere);
                    //throw new exception('Could not create gif');
                    return false;
                }
            } else {
                throw new exception('Not a valid mime type');
                return false;
            }

                $oldX = imageSX($sourceImage); 
                $oldY = imageSY($sourceImage); 

                if ($newWidth == NULL) {
                    $thumbHeight = $newHeight;
                    $thumbWidth = round($newHeight/($oldY/$oldX));
                } else 

                if ($oldX > $oldY) {
                    $thumbWidth = $newWidth; 
                    $thumbHeight = $oldY * ($newHeight/$oldX);
                } 

                if ($oldX < $oldY) {
                    $thumbWidth = round($newHeight/($oldY/$oldX));
                    $thumbHeight = $newHeight;
                }

                if ($oldX == $oldY) {
                    $thumbWidth = $newWidth; 
                    $thumbHeight = $newHeight;
                }

                    if (!gd_info()) {
                        $dstImage = ImageCreate($thumbWidth, $thumbHeight); 
                        imagecopyresized($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $oldX, $oldY);
                    } else {
                        $dstImage = ImageCreateTrueColor($thumbWidth, $thumbHeight); 
                        imagecopyresampled($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $oldX, $oldY);
                    } 

                    if ($mime == 'image/png') {
                        $xparent = imagecolorresolvealpha($dstImage, 255,2,240, 0) ;
                        imagecolortransparent($dstImage,$xparent);
                        imagealphablending($dstImage,true);
                        imagepng($dstImage, $toWhere);

                    } else if ($mime == 'image/jpeg') { 
                        imagejpeg($dstImage, $toWhere);

                    } else if ($mime == 'image/gif') {
                        imagegif ($dstImage, $toWhere);

                    }

                    imagedestroy($dstImage); 
                    imagedestroy($sourceImage);
                    return true;
        }

}

© Stack Overflow or respective owner

Related posts about php

Related posts about image-processing