Posted By

welancers on 08/16/12


Tagged

image thumbnail


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tux-world


Create Image Thumbnail Quickly using PHP


 / Published in: PHP
 

Create Image Thumbnail Quickly using PHP by simply passing the 4 arguments 1. the path to original image directory 2. the path to thumbnail directory 3. name of the file 4. width of the Thumbnail to be set Height it will apply based on width to maintain the resolution Supports jpg, gif and png files.

  1. function createThumbnail($path_to_image_directory, $path_to_thumbs_directory, $filename, $final_width_of_image) {
  2.  
  3. if(preg_match('/[.](jpg)$/', $filename)) {
  4. $im = imagecreatefromjpeg($path_to_image_directory . $filename);
  5. } else if (preg_match('/[.](gif)$/', $filename)) {
  6. $im = imagecreatefromgif($path_to_image_directory . $filename);
  7. } else if (preg_match('/[.](png)$/', $filename)) {
  8. $im = imagecreatefrompng($path_to_image_directory . $filename);
  9. }
  10.  
  11. $ox = imagesx($im);
  12. $oy = imagesy($im);
  13.  
  14. $nx = $final_width_of_image;
  15. $ny = floor($oy * ($final_width_of_image / $ox));
  16.  
  17. $nm = imagecreatetruecolor($nx, $ny);
  18.  
  19. imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
  20. imagejpeg($nm, $path_to_thumbs_directory . $filename);
  21. }

Report this snippet  

You need to login to post a comment.