Make thumb using php with respect the ratio width and height

In this post i would like to explain how to create thumbnails with respect to ratio of width and height. Rather than scaling images to create a thumbnails and we can reduce the page load time also. With simple code of php we can get the thumbnails of images.

 make thumb using php with respect the ratio width and height | Anil Labs

make thumb using php with respect the ratio width and height | Anil Labs


 

PHP Code

 

<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' )
    {
      echo "Creating thumbnail for {$fname} <br />";

      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $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 );

      // save thumbnail into a file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
    }
  }
  // close the directory
  closedir( $dir );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("images/","images/thumb/",400);
?>

Hope that it will be useful.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

8 Responses

  1. Kates says:

    I like it .

  2. Amitash says:

    There is another script called timthumb that can do this.. This looks better. Thanks Anil.

  3. Andejo says:

    do you have any example haow to use the function, i am quiet new in php.
    hope you can help me little bit

  1. February 12, 2011

    […] make thumb using php with respect the ratio width and height … […]

  2. February 17, 2011

    […] make ride regulating php with apply oneself a ratio breadth as well as height … […]

  3. February 19, 2011

    […] make ride regulating php with apply oneself a ratio breadth as well as height … […]

  4. February 26, 2011

    […] make ride regulating php with apply oneself the ratio breadth as well as height … […]

  5. December 7, 2017

    […] Make thumb using php with respect the ratio width and height […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.