views:

208

answers:

2

Final: I've decided to basically use this: http://shiftingpixel.com/2008/03/03/smart-image-resizer/

As it handles everything, Ive turned caching off and do this in the admin controllers:

    $image = file_get_contents(SITE_ADMIN_IMAGE.'/SmartImage.php?width='.$this->thumb_width.'&height='.$this->thumb_height.'&image=/images/'.$this->image_directory.'/'.$formData['image_url'].'');
    file_put_contents(ROOT_PATH.'/public/images/'.$this->image_directory.'/thumb/'.$formData['image_url'], $image);

EDIT: I found this works, however it creates very sharp edges, it doesn't look right.

    imagecolortransparent($dstImage, $background);
    imagealphablending($dstImage, false);
    $colorTransparent = imagecolorallocatealpha($dstImage, 0, 0, 0, 127);
    imagefill($dstImage, 0, 0, $colorTransparent);
    imagesavealpha($dstImage, true);
    imagepng($dstImage, $toWhere);

Ideas?

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;
        }

}
A: 

There are a lot of posts around the web addressing this issue...

http://php.net/manual/en/function.imagecopyresampled.php#93166

http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/

Also, about JPEG Image Quality,

http://stackoverflow.com/questions/12661/efficient-jpeg-image-resizing-in-php

Let me know if this helps.

acmatos
Thanks, Ive taken a look over them and I'm doing alot of what theyre doing now.My issue is this though: http://img15.imageshack.us/img15/2668/essexlads.pngNotice the black area within the image now, any idea how to overcome it?
azz0r
A: 

Regarding JPEG image quality you need to make use of the third argument in imagejpeg():

imagejpeg($dstImage, $toWhere, 90); // any value above 85 should be fine

Regarding PNG transparency, you're doing it wrong. Your script is horrible and has fundamental problems, I'm gonna leave you with a revised one that fixes both of your problems. It can still be further optimized but I choose to leave some of your original less important mistakes so you don't feel lost:

class OpenSource_ImageResize
{
    // $extension is not used?
    function __construct($theFile, $toWhere, $mime, $extension, $newWidth, $newHeight)
    {
        $sourceImage = ImageCreateFromString(file_get_contents($theFile));

        if (is_resource($sourceImage))
        {
            $info = getimagesize($theFile);

            if (is_null($mime))
            {
                $mime = $info['mime'];
            }

            if ($info[0] <= $newWidth && $info[1] <= $newHeight)
            {
                imagedestroy($sourceImage);
                return copy($theFile, $toWhere);
            }

            if (is_null($newWidth))
            {
                $thumbHeight = $newHeight;
                $thumbWidth = round($newHeight/($info[1]/$info[0]));
            }

            else if ($info[0] > $info[1])
            {
                $thumbWidth = $newWidth;
                $thumbHeight = $info[1] * ($newHeight/$info[0]);
            }

            if ($info[0] < $info[1])
            {
                $thumbWidth = round($newHeight/($info[1]/$info[0]));
                $thumbHeight = $newHeight;
            }

            if ($info[0] == $info[1])
            {
                $thumbWidth = $newWidth;
                $thumbHeight = $newHeight;
            }

            $dstImage = ImageCreateTrueColor($thumbWidth, $thumbHeight);

            /* fix PNG transparency issues */           
            ImageFill($dstImage, 0, 0, IMG_COLOR_TRANSPARENT);
            ImageSaveAlpha($dstImage, true);
            ImageAlphaBlending($dstImage, true);

            imagecopyresampled($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $info[0], $info[1]);

            switch ($mime)
            {
                case 'image/png':
                    imagepng($dstImage, $toWhere, 9); // compress it (level 1 to 9)
                break;

                case 'image/jpeg':
                    imagejpeg($dstImage, $toWhere, 90); // quality = 90 (1 to 100, default is "about" 75)
                break;

                case 'image/gif':
                    imagegif($dstImage, $toWhere);
                break;
            }

            imagedestroy($dstImage);
            imagedestroy($sourceImage);

            return true;
        }
    }
}

I'm sorry for not explicitly pointing out your mistakes but they are so many and it's 3 AM here, need to get some sleep - study it and read the manual, if you have any doubts let me know.

Alix Axel