Image resizing: poor quality jpeg and black PNG backgrounds

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

Since it handles everything, Ive disconnected and did this in 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 to work, however it creates very sharp edges, it doesn’t look like that.

    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 questions with my class, basically the quality of jpeg images is pretty poor, but I'm not sure if this has decreased to my attitude. Ideally, I would like this class to be strict with image sizes and crop them, but I can't lower my head around it.

My main problem is that pngs always have black bg, does anyone have any experience with this case?

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

}
+3
3

JPEG, imagejpeg():

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

PNG, . script , , . , , :

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

, , , 3 , - , - , .

+2

Define the background color png in your class. You can also change the background color of jpg and other files after determining the background color.

-1
source

Source: https://habr.com/ru/post/1745425/


All Articles