문제

I'm creating thumnails of uploaded file.

if image width and height are greater than 200 than then i re size them to 200px.

Here is code i used to do that:

if (file_exists($old_file)) {
        $path_parts = pathinfo($old_file);
        $extension = $path_parts['extension'];
        $filename_path = $filepath . $filename;
        $destination_path = $filename_path;
        if (strtolower($extension) == "jpg" || strtolower($extension) == "jpeg") {
            $uploadedfile = $old_file;
            $src = imagecreatefromjpeg($uploadedfile);
        } else if (strtolower($extension) == "png") {
            $uploadedfile = $old_file;
            $src = imagecreatefrompng($uploadedfile);
        } else {
            $uploadedfile = $old_file;
            $src = imagecreatefromgif($uploadedfile);
        }
        list($width, $height) = getimagesize($uploadedfile);
        $newwidth = $Size['width'];
        $newheight = $Size['height'];
        if ($width <= $newwidth && $height <= $newheight) {
            $newwidth = $width;
            $newheight = $height;
            $tmp = imagecreatetruecolor($width, $height);
        } else {
            if ($width > $height) {
                $newheight = ($height / $width) * $newwidth;
                $tmp = imagecreatetruecolor($newwidth, $newheight);
            } else {
                $newwidth = ($width / $height) * $newheight;
                $tmp = imagecreatetruecolor($newwidth, $newheight);
            }
        }
        if ((strtolower($extension) == "png") OR (strtolower($extension) == "gif")) {
            imagealphablending($tmp, false);
            imagesavealpha($tmp, true);
            $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
            imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
        }
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        if (strtolower($extension) == "jpg" || strtolower($extension) == "jpeg") {
            imagejpeg($tmp, $destination_path, 100);
        } elseif (strtolower($extension) == "png") {
            imagepng($tmp, $destination_path, 5);
        } else {
            imagegif($tmp, $destination_path);
        }
        chmod($destination_path, 0777);
        imagedestroy($src);
        imagedestroy($tmp);
        ob_flush();
        flush();
        ob_end_flush();
        return true;
    } else {
        return false;
    }

it re size the large images to 200px by 200px but image size is increased in (byte and kb etc increased).

I tried uploading 8kb png file, and new thumbnail file size was 28kb?

Tried googling but didn't find anything helpful

Thanks.

도움이 되었습니까?

해결책

Your source image is compressed, after parsing it you get a true color image, which is uncompressed. Then you save it with a compression level of 5 (in the case of PNG), which is pretty low compression, thus a higher filesize.

Try a higher compression, like 9, for example. Also try adding a combination of filters to decrease filesize (http://us3.php.net/manual/en/image.constants.php look for PNG_FILTER_*).

See: http://us3.php.net/manual/en/function.imagepng.php

http://en.wikipedia.org/wiki/Portable_Network_Graphics#Compression http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_size_factors

The GD library doesn't appear to provide any interfaces to let you in on low-level PNG data, but you can theoretically find out the source compression level and filters, by using other bindings or trying to read it manually.

http://www.libpng.org/pub/png/spec/1.2/PNG-Compression.html http://www.libpng.org/pub/png/spec/1.2/PNG-Filters.html

The same may happen with JPG and GIF.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top