Posted By

andyhartleeds on 03/28/08


Tagged

php image gd library resizing


Versions (?)

Who likes this?

17 people have marked this snippet as a favorite

SpinZ
luman
paullorentzen
Nix
wizard04
jamesming
Jenhale
salibaray
irishsk
nb109
vali29
adiian
Adam42
dinkan
bobbym245
wirenaught
cristiansannino


How to Resize an Image Using PHP - Image Resizing Script


 / Published in: PHP
 

URL: http://www.4wordsystems.com/php_image_resize.php

  1. <?php
  2.  
  3. // This is the temporary file created by PHP
  4. $uploadedfile = $_FILES['uploadfile']['tmp_name'];
  5.  
  6. // Create an Image from it so we can do the resize
  7. $src = imagecreatefromjpeg($uploadedfile);
  8.  
  9. // Capture the original size of the uploaded image
  10. list($width,$height)=getimagesize($uploadedfile);
  11.  
  12. // For our purposes, I have resized the image to be
  13. // 600 pixels wide, and maintain the original aspect
  14. // ratio. This prevents the image from being "stretched"
  15. // or "squashed". If you prefer some max width other than
  16. // 600, simply change the $newwidth variable
  17. $newwidth=600;
  18. $newheight=($height/$width)*600;
  19. $tmp=imagecreatetruecolor($newwidth,$newheight);
  20.  
  21. // this line actually does the image resizing, copying from the original
  22. // image into the $tmp image
  23. imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
  24.  
  25. // now write the resized image to disk. I have assumed that you want the
  26. // resized, uploaded image file to reside in the ./images subdirectory.
  27. $filename = "images/". $_FILES['uploadfile']['name'];
  28. imagejpeg($tmp,$filename,100);
  29.  
  30. imagedestroy($src);
  31. imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
  32. // has completed.
  33. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: rhl on September 12, 2009

really very nice code . its very easy to integrate. http://sendgifttoindian.com/

Posted By: rhl on September 12, 2009

but its not working with only .jpg file

Posted By: rhl on September 12, 2009

but its not working with only .jpg file

Posted By: rhl on September 12, 2009

really very nice code . its very easy to integrate. http://sendgifttoindian.com/

You need to login to post a comment.