How to Resize an Image Using PHP - Image Resizing Script


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



Copy this code and paste it in your HTML
  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($tmp); // NOTE: PHP will clean up the temp file it created when the request
  31. // has completed.
  32. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.