简体   繁体   中英

How to create Thumbnail Image in PHP without loosing its Original Quality using GD Library

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);
        } 
        }
        }

The code is like this 1) I am uploading an Image in the folder "realimage" . 2) I want to display a resized image of the realimage on my website . For that I hav created a folder "thumbimage" which stores the thumbnail image of the original.

The code does create Thumbnails for the images which are larger than the specified thumbnail width and height, but the quality Looses and I get blur Images. I want the Image quality to be same as the original image , just the size should be small then the original.

I have got another problem as well, when I upload an Image smaller than the specified Thumbnail Width & Height , then the image does not fit into the area and fills just some part of the area leaving other area black.

Help Me in Creating Ideal Thumbnail meeting all the scenarios.

try this:

$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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM