dpjjmo3079 2014-03-19 02:03
浏览 53
已采纳

使用GD生成图像缩略图仅对PNG失败

I got following code for generate a image thumbnail, but it only works for JPEG for unknown reason, i cant figure out why it doesnt work for PNG ?

function generate_image_thumbnail($source_image_path, $thumbnail_image_path)
    {
        list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
        switch ($source_image_type) {
            case IMAGETYPE_GIF:
                $source_gd_image = imagecreatefromgif($source_image_path);
                break;
            case IMAGETYPE_JPEG:
                $source_gd_image = imagecreatefromjpeg($source_image_path);
                break;
            case IMAGETYPE_PNG:{
                $source_gd_image = imagecreatefrompng($source_image_path);
                break;
            }
        } //$source_image_type
        if ($source_gd_image === false) {
            return false;
        } //$source_gd_image === false
        $source_aspect_ratio    = $source_image_width / $source_image_height;
        $thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT;
        if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
            $thumbnail_image_width  = $source_image_width;
            $thumbnail_image_height = $source_image_height;
        } //$source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT
        elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
            $thumbnail_image_width  = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
            $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
        } //$thumbnail_aspect_ratio > $source_aspect_ratio
        else {
            $thumbnail_image_width  = THUMBNAIL_IMAGE_MAX_WIDTH;
            $thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio);
        }
        $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
        imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);

        switch ($source_image_type) {
            case IMAGETYPE_GIF:
                imagegif($thumbnail_gd_image, $thumbnail_image_path, 90);
                break;
            case IMAGETYPE_JPEG:
                imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);
                break;
            case IMAGETYPE_PNG:{
                //imagealphablending($thumbnail_gd_image, false); 
                //imagesavealpha($thumbnail_gd_image,true);
                imagepng($thumbnail_gd_image, $thumbnail_image_path, 90);
                break;
            }
        }               



        imagedestroy($source_gd_image);
        imagedestroy($thumbnail_gd_image);
        return true;
    }
  • 写回答

2条回答 默认 最新

  • duanjiashai9140 2014-03-19 03:42
    关注

    the problem is in the second png case, exactly in

    imagepng($thumbnail_gd_image, $thumbnail_image_path, 90);
    

    according to imagepng function

    quality

    Compression level: from 0 (no compression) to 9.
    

    so, change 90 to 9 (for example)

    imagepng($thumbnail_gd_image, $thumbnail_image_path, 9);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的