Home >Backend Development >PHP Tutorial >Example of generating thumbnails with php GD library

Example of generating thumbnails with php GD library

WBOY
WBOYOriginal
2016-07-25 08:54:51943browse
  1. header("content-type:text/html;charset=gbk");
  2. ini_set("date.timezone","Asia/chong");
  3. //Determine whether the file is empty
  4. if(empty($_FILES)){
  5. echo "The uploaded file is too large";
  6. exit;
  7. }
  8. / /Determine whether there is an error in file upload
  9. if($_FILES['pic']['error']){
  10. echo "Upload file";
  11. exit;
  12. }
  13. //Determine whether the file type is illegal to obtain the file suffix
  14. $allowtype =array("jpg","png","jpeg","gif");
  15. $a=explode('.',$_FILES['pic']['name']);
  16. $index=count( $a)-1;
  17. $ex=strtolower($a[$index]);
  18. if(!in_array($ex,$allowtype)){
  19. echo "Illegal file upload";
  20. exit;
  21. }
  22. $file =date('YmdHis').rand().".".$ex;
  23. $src=$_FILES['pic']['tmp_name'];
  24. $des="upload/".$file;
  25. $ rs=move_uploaded_file($src,$des);

  26. //Thumbnail

  27. //Read uploaded images
  28. $image=imagecreatefromjpeg($des);
  29. $a=getimagesize($ des);
  30. $w=$a[0];
  31. $h=$a[1];
  32. if($w>$h){
  33. $width=300;
  34. $height=$width/$w*$ h;
  35. }else if($w<$h){
  36. $height=300;
  37. $width=$height/$h*$w;
  38. }else{
  39. $width=300;
  40. $height=300;
  41. }
  42. //Create a blank new image
  43. $newimage=imagecreatetruecolor($width, $height);
  44. //Copy source image content copy new image
  45. imagecopyresized($newimage, $image, 0,0, 0,0, $width , $height, $w, $h);
  46. $filename="upload/s_".$file;
  47. imagejpeg($newimage,$filename);

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn