Posted By

iTony on 10/03/08


Tagged

php image function thumbnail GD2


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

davidsword
JimiJay
wizard04
keame
Mithun
vali29
huynguye
jafar


Create a thumbnail function


 / Published in: PHP
 

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

it needs the GD2 library

  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. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Bluaj on November 13, 2009

imagecopyresized() result isn't good. Image is sharped and ugly. Try with imagecopyresampled(). Result is better than imagecopyresized()

You need to login to post a comment.