如何使用GD库在不丢失其原始质量的情况下在PHP中创建缩略图图像

时间:2014-03-06 08:04:01

标签: php gd thumbnails

if(isset($_POST["insert"]))
   {
      foreach($_FILES as $imgfile)
      {
        $tmp_name = $imgfile['tmp_name'];
        $type = $imgfile['type'];
        $name = $imgfile['name']; //name of original image
        $size = $imgfile['size'];

        if (file_exists($tmp_name))
        {
         if(is_uploaded_file($tmp_name))
            {
                $target="realimage/";
                $target .= basename($_FILES['image']['name']); //path of original image
                $file = fopen($tmp_name,'r');
                $data = fread($file,filesize($tmp_name));
                fclose($file);
                $data = chunk_split(base64_encode($data));
                move_uploaded_file($tmp_name,$target);
            }
                $extension=  explode(".", $target);
                $extension=$extension[count($extension)-1]; //gives the image extension
                $maxwidth=400;
                $maxheight=200;
                switch($extension) {
                    case 'gif':
                        $tmpimg = imagecreatefromgif($target);
                        break;
                    case 'jpg':
                        $tmpimg = imagecreatefromjpeg($target);
                        break;
                    case 'png':
                        $tmpimg = imagecreatefrompng($target);
                        break;
                }               
                list($width,$height)=getimagesize($target);
                if($width > $height)
                {
                    $thumb_width=$maxwidth;
                    $thumb_height=  intval($height*$thumb_width/$width);
                }
                else
                {
                    $thumb_height= $maxheight;
                    $thumb_width=intval($width*$thumb_height/$height);
                }
                $dest_x = intval(($maxwidth - $thumb_width) / 2);
                $dest_y = intval(($maxheight - $thumb_height) / 2);

                $newimg=  imagecreatetruecolor($maxwidth,$maxheight);


imagecopyresampled($newimg,$tmpimg,$dest_x,$dest_y,0,0,$thumb_width,$thumb_height,$width,$height);


               imagejpeg($newimg,"thumbimage/$name",100);
        } 
        }
        }

代码是这样的 1)我正在“realimage”文件夹中上传一张图片。 2)我想在我的网站上显示一个重新调整的realimage图像。为此,我创建了一个文件夹“thumbimage”,用于存储原始缩略图。

代码确实为大于指定缩略图宽度和高度的图像创建缩略图,但质量松散,我得到模糊图像。我希望图像质量与原始图像相同,只是尺寸应该小于原始图像。

当我上传的图片小于指定的缩略图宽度&时,我也遇到了另一个问题。高度,然后图像不适合该区域,并填充该区域的某些部分,使其他区域变黑。

帮助我创建符合所有场景的理想缩略图。

1 个答案:

答案 0 :(得分:0)

试试这个:

$type = exif_imagetype($pathToImages);

$width = imagesx($img);

$height = imagesy($img);

// calculate thumbnail size

if ($width >= $height) {

    //If width is greater than height

    $new_width = $thumbWidth;

    $new_height = floor($height * ($thumbWidth / $width));

} else {

    //If height is greater than width

    $new_height = $thumbWidth;

    $new_width = floor($width * ($thumbWidth / $height));

}

// create a new temporary image

// echo $new_height;

$tmp_img = imagecreatetruecolor($new_width, $new_height);

  imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);    

// save thumbnail into a file
switch($type) {

    case 1 : 
        imagegif($tmp_img, $pathToThumbs . $file_name,90);
        break;
    case 2 :            
        imagejpeg($tmp_img, $pathToThumbs . $file_name,90);
        break;
    case 3 :                           
        imagepng($tmp_img, $pathToThumbs . $file_name,90);
        break;
    case 6 :            
        imagewbmp($tmp_img, $pathToThumbs . $file_name,90);
        break;
}