【问题标题】:Why does the thumbnail I create have a larger file size than the original image?为什么我创建的缩略图的文件大小比原始图像大?
【发布时间】:2015-01-19 20:22:17
【问题描述】:

我想将 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();

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

为什么缩略图比原图大?

【问题讨论】:

  • 呃。原稿为 640×640。缩略图为 480×480。我无法达到您的预期。
  • 因为 480
  • 请注意,您正在尝试做的是违反地图 TOS developers.google.com/maps/terms 10.1.3。

标签: php image google-maps


【解决方案1】:

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


编辑:

减少位深度非常简单,但我看不到任何方法可以通过 CodeIgniter 做到这一点:

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

但是,此文件仍比原始文件大(~17KiB 与 ~13KiB)。通过TinyPNG 运行它可以将其降至~13KiB,接近原始数据。

【讨论】:

    【解决方案2】:

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

    下面是对每个文件的分析:

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

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

    【讨论】:

    • 图像分析看起来非常有用!你用什么来生成.txt 文件?
    • @timclutton 嗨,蒂姆,我使用 ImageMagick 是这样的:identify -verbose image.jpg 它可以告诉你所有的 EXIF 信息、直方图、统计数据等。然后我将它们与 Mac 上的opendiff 进行了比较,这是也很棒。
    • 我一直想深入研究 ImageMagick,这给了我更大的理由。太棒了,谢谢马克;投赞成票!
    猜你喜欢
    • 2016-11-16
    • 2017-06-10
    • 2012-10-26
    • 2012-01-09
    • 2011-08-12
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    相关资源
    最近更新 更多