【问题标题】:Making thumbnails of images using php without reducing its quality使用 php 制作图像缩略图而不降低其质量
【发布时间】:2014-12-21 22:30:18
【问题描述】:

我正在使用以下代码制作图像的缩略图,但质量达不到标准。需要帮助

   <?php
    function make_thumb($src,$dest,$desired_width)

    {
    $source_image=imagecreatefromjpeg($src);
    $width = imagesx($source_image);//width of the image.
    $height = imagesy($source_image);//height of the image.
    $desired_height = floor($height*($desired_width/$width));
    $virtual_image = imagecreatetruecolor($desired_width,$desired_height);

 imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
    $fname= basename($src); 
    $dir="upload/thumbs/";
    imagejpeg($virtual_image,$dir.$fname);

    }
    ?>

缩略图大小合适,但质量不合适。

【问题讨论】:

    标签: php image gallery thumbnails


    【解决方案1】:

    您可以做一些事情,例如使图像尺寸更大(过采样),并根据样式或您拥有的东西&lt;img style="max-width: 80%;" /&gt; 强制其输出更小,但您的问题可能是您的压缩是未在您的imagejpeg() 上设置:

    // This function has a quality setting (3rd value ranges from 0-100)
    imagejpeg($virtual_image,$dir.$fname,100);
    

    【讨论】:

    • 您的解决方案有效,谢谢,但想知道默认质量值是多少?
    • 添加 100 质量不会增加缩小质量,但会增加文件大小。
    • 根据 PHP 手册,默认质量是 75,但我发现它太粗了
    • @Bhuwan 默认值为 75 。但是您应该考虑重新采样而不是调整大小。进行一些额外的后处理以提高质量。
    • @AlexLinte 有时压缩质量对开发人员的口味来说不够好......
    【解决方案2】:

    您应该重新采样图像,而不仅仅是简单地调整大小。

    像这样使用imagecopyresampled()

    imagecopyresampled($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
    

    【讨论】:

    • 结合 imagecopyresampled() 和 quality :100 可以得到最好的结果。
    • 但使用 100 质量时文件大小从 9 kb 增加到 45 kb。
    • 正如对 Rasclatt 的回答所评论的那样,100 q 太过分了,实际上删除了 JPEG 完成的任何压缩。重新采样和质量在 80-90 之间应该没问题
    猜你喜欢
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2010-11-12
    相关资源
    最近更新 更多