정보실

웹학교

정보실

php 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. ?>
  • 트위터로 보내기
  • 페이스북으로 보내기
  • 구글플러스로 보내기
  • 카카오톡으로 보내기

페이지 정보

조회 88회 ]  작성일18-08-30 13:22
php

웹학교