1

我正在创建上传文件的缩略图。

如果图像宽度和高度大于 200,那么我将它们的大小调整为 200 像素。

这是我曾经这样做的代码:

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

它将大图像的大小重新调整为 200px x 200px,但图像大小会增加(字节和 kb 等增加)。

我尝试上传 8kb png 文件,新的缩略图文件大小为 28kb?

尝试谷歌搜索,但没有发现任何有用的信息

谢谢。

4

1 回答 1

4

您的源图像已被压缩,解析后您将获得未压缩的真彩色图像。然后以 5 的压缩级别(在 PNG 的情况下)保存它,这是相当低的压缩率,因此文件大小更高。

尝试更高的压缩率,例如 9。还可以尝试添加过滤器组合以减小文件大小(http://us3.php.net/manual/en/image.constants.php查找PNG_FILTER_*)。

见: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

GD 库似乎没有提供任何接口让您进入低级 PNG 数据,但理论上您可以通过使用其他绑定或尝试手动读取来找出源压缩级别和过滤器。

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

同样的情况也可能发生在 JPG 和 GIF 上。

于 2013-12-01T08:27:15.277 回答