Create thumbnail from image with PHP

Everybody who has a webpage where it is possible to upload images (for example a member picture for member profile) knows this problem: Digital cameras in these days creates pictures with high resolution but also with a big file size. Most pages forbid to upload pictures like this and restrict the size of the picture files.

You can create a good workaround for that. One version is to store the original file and create an additional thumbnail, the other way is, to store only the thumbnail, if you have less space on your webserver.In both ways you can use this PHP function to create a thumbnail from the uploaded picture. PHP has nice functions directly built in to create thumbnails in an easy way. You are only restricted to some file types,but with the possible filetypes you are able to cover, I think, 99% of all images all over the world, which are used for internet. You are restricted to jpg, jpeg, gif and png.

The function reads the image from the server, loads it in the memory, resizes it and stores it to a new, resized image to the given path and, in my example, to the given width. The height will be calculated then automatically. If you need a fixed height, you can change the calculation of the image size to the other way around.

if you have less space, you can delete the original file afterwards.

function createThumbs( $pathToImages, $filename, $pathToThumbs, $thumbWidth ) {
  $info = pathinfo($pathToImages . $filename); // reads image information
  // continue only if this is a JPEG image
  if ( strtolower($info['extension']) == 'jpg' || strtolower($info['extension']) == 'jpeg' ) {
    // load image and get image size
    $img = imagecreatefromjpeg( "{$pathToImages}{$filename}" );
    // resize image
    $tmp_img = resizeImage($img);
    // save thumbnail into a file
    imagejpeg( $tmp_img, "{$pathToThumbs}{$filename}" );
  } else if ( strtolower($info['extension']) == 'gif' ) {
    // load image and get image size
    $img = imagecreatefromgif( "{$pathToImages}{$filename}" );
    // resize image
    $tmp_img = resizeImage($img);
    // save thumbnail into a file
    imagegif( $tmp_img, "{$pathToThumbs}{$filename}" );
  } else if ( strtolower($info['extension']) == 'png' ) {
    // load image and get image size
    $img = imagecreatefrompng( "{$pathToImages}{$filename}" );
    // resize image
    $tmp_img = resizeImage($img);
    // save thumbnail into a file
    imagepng( $tmp_img, "{$pathToThumbs}{$filename}" );
  }
}
 
function resizeImage($img) {
  $width = imagesx( $img );
  $height = imagesy( $img );
 
  // calculate thumbnail size
  $new_width = $thumbWidth;
  $new_height = floor( $height * ( $thumbWidth / $width ) );
 
  // create a new temporary image
  $tmp_img = imagecreatetruecolor( $new_width, $new_height );
  // copy and resize old image into new image
  imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
 
  return $tmp_img;
}

Print Friendly, PDF & Email

Leave a Comment

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*
*