制作缩略图,但PNG有问题

时间:2012-09-26 20:55:55

标签: php gd

我正在使用php创建缩略图,它适用于GIF和JPEG,但不适用于PNG。当我使用PNG运行此脚本时,缩略图不会保存。你能告诉我我做错了什么吗?提前谢谢!

public function make_thumbs($img_src)
{
    //Desired thumbnail width 
    $width = 100;
    //Thumbnail name 
    $thumb = 'th_' . $img_src;

    //Ensure the image exists
    if(file_exists($img_src)){

        if (exif_imagetype($img_src) == IMAGETYPE_GIF) 
        {
            //Create image stream 
            $image = imagecreatefromgif($img_src);
        }
        elseif(exif_imagetype($img_src) == IMAGETYPE_JPEG)
        {
            //Create image stream 
            $image = imagecreatefromjpeg($img_src);
        }
        elseif(exif_imagetype($img_src) == IMAGETYPE_PNG)
        {
            //Create image stream 
            $image = imagecreatefrompng($img_src);
        }


        //Gather and store the width and height
        list($image_width, $image_height) = getimagesize($img_src);

        //Calculate new height while maintaining aspect ratio 
        $height = (($width / $image_width) * $image_height);

        //Resample/resize the image 
        $tmp_img = imagecreatetruecolor($width, $height);
        imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);

        //Attempt to save the new thumbnail 
        if(is_writeable(dirname($thumb)))
        {
            if (exif_imagetype($img_src) == IMAGETYPE_GIF) 
            {
                imagegif($tmp_img, $thumb, 100);
            }
            elseif(exif_imagetype($img_src) == IMAGETYPE_JPEG)
            {
                imagejpeg($tmp_img, $thumb, 100);
            }
            elseif(exif_imagetype($img_src) == IMAGETYPE_PNG)
            {
                imagepng($tmp_img, $thumb, 100);
            }
            }

            //clear memory 
            imagedestroy($tmp_img);
            imagedestroy($image);
        }
        }

0 个答案:

没有答案