Home > Backend Development > PHP Tutorial > PHP uses GD library to generate thumbnails

PHP uses GD library to generate thumbnails

WBOY
Release: 2016-07-25 08:43:43
Original
807 people have browsed it
  1. < input type="submit" value="Upload 1" />
  2. header("content-type:text/html;charset=gbk");
  3. ini_set( "date.timezone","Asia/chong");
  4. //Judge whether the file is empty
  5. if(empty($_FILES)){
  6. echo "The uploaded file is too large";
  7. exit;
  8. }
  9. //Judge the file Check if there are any errors in the upload
  10. if($_FILES['pic']['error']){
  11. echo "Upload file";
  12. exit;
  13. }
  14. //Determine whether the file type is illegal to obtain the file suffix
  15. $allowtype=array( "jpg","png","jpeg","gif");
  16. $a=explode('.',$_FILES['pic']['name']);
  17. $index=count($a) -1;
  18. $ex=strtolower($a[$index]);
  19. if(!in_array($ex,$allowtype)){
  20. echo "Illegal file upload";
  21. exit;
  22. }
  23. $file=date( 'YmdHis').rand().".".$ex;
  24. $src=$_FILES['pic']['tmp_name'];
  25. $des="upload/".$file;
  26. $rs=move_uploaded_file ($src,$des);
  27. //Thumbnail
  28. //Read uploaded images
  29. $image=imagecreatefromjpeg($des);
  30. $a=getimagesize($des);
  31. $w=$a[ 0];
  32. $h=$a[1];
  33. if($w>$h){
  34. $width=300;
  35. $height=$width/$w*$h;
  36. }else if($w< $h){
  37. $height=300;
  38. $width=$height/$h*$w;
  39. }else{
  40. $width=300;
  41. $height=300;
  42. }
  43. //Create a new blank image
  44. $ newimage=imagecreatetruecolor($width, $height);
  45. //Copy the source image content and copy the new image
  46. imagecopyresized($newimage, $image, 0,0, 0,0, $width, $height, $w, $h) ;
  47. $filename="upload/s_".$file;
  48. imagejpeg($newimage,$filename);
Copy code

php


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template