php修改上传图片尺寸的方法

这篇文章主要介绍了php修改上传图片尺寸的方法,涉及php操作图片的技巧,非常具有实用价值,需要的朋友可以参考下。

本文实例讲述了php修改上传图片尺寸的方法,分享给大家供大家参考,具体实现方法如下:

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