为什么我创建的缩略图的文件大小比原始图像大?

时间:2014-11-21 16:08:24

标签: php image google-maps

我想将Google地图图片保存到我的服务器。下面是我用来获取和保存这些图像的代码,以及用于创建缩略图的代码。我正在使用CodeIgniter。

//saving original image on server
$post = $_POST;
$file = file_get_contents("http://maps.google.com/maps/api/staticmap?size=".$post['w']."x".$post['h']."&sensor=false&markers=color:red|size:mid|".$post['lt'].",".$post['lg']."&&zoom=".$post['z']);

$filename = 'map_'.uniqid().'.png';
$name     = './assets/images/upload/'.$filename;
file_put_contents($name, $file);

// creating thumbnail 
$config_manip = array(
    'image_library' => 'gd2',
    'source_image' => './assets/images/upload/'.$filename,
    'new_image' => './assets/images/upload/thumb_'.$filename,
    'maintain_ratio' => false,
    'quality' => "10%",
    'width' => 480,
    'height' => 480 
);

$this->load->library('image_lib', $config_manip);
$this->image_lib->resize();

我的问题是生成的缩略图图像的尺寸比原始图像大得多。为了比较:

为什么缩略图大于原始缩略图?

2 个答案:

答案 0 :(得分:4)

您创建的缩略图的位深度是原始位移的4倍。减小位深度将减小文件大小。

Properties of original file Properties of thumbnail file


<强> 编辑:

减少位深度非常简单,但我无法通过CodeIgniter看到任何方法:

$im = imagecreatefrompng('./original.png');
imagetruecolortopalette($im, false, 256);
imagepng($im, './output.png');

然而,这个文件仍然比原来的大(~17KiB vs.~13KiB)。通过TinyPNG运行它会降低到~13KiB,接近原始值。

答案 1 :(得分:4)

主要区别在于原始图像包含调色板,而缩略图则不包含调色板。因此,不必将每个像素的8位索引存储到调色板中,缩略图必须为每个像素存储3个8位真彩色。您需要一种方法来强制使用缩略图 - 即在输出之前使用imagecreate()而不是imagecreatetruecolor()或致电imagetruecolortopalette()

以下是每个文件的分析:

enter image description here

根据您选择包含在调色板中的颜色数量,您将获得不同的文件大小,如下所示:

Colours    Filesize (bytes)
=======    ================
10          3,380
16         12,199
32         12,415
64         36,581
128        36,825
256        42,013
相关问题