Create a thumbnail function


/ Published in: PHP
Save to your folder(s)

it needs the GD2 library


Copy this code and paste it in your HTML
  1. function make_thumb($src,$dest,$desired_width)
  2. {
  3.  
  4. /* read the source image */
  5. $source_image = imagecreatefromjpeg($src);
  6. $width = imagesx($source_image);
  7. $height = imagesy($source_image);
  8.  
  9. /* find the "desired height" of this thumbnail, relative to the desired width */
  10. $desired_height = floor($height*($desired_width/$width));
  11.  
  12. /* create a new, "virtual" image */
  13. $virtual_image = imagecreatetruecolor($desired_width,$desired_height);
  14.  
  15. /* copy source image at a resized size */
  16. imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
  17.  
  18. /* create the physical thumbnail image to its destination */
  19. imagejpeg($virtual_image,$dest, 83);
  20. }

URL: http://davidwalsh.name/create-image-thumbnail-php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.