PHP upload pictures, etc. than the code to zoom the picture

Source: Internet
Author: User
  1. /**

  2. *
  3. * @author Zhao Jinhan
  4. * @date January 13, 2014 11:54:30
  5. * @email xb_zjh@126.com
  6. *
  7. */
  8. Header (' content-type:text/html; Charset=utf-8 ');
  9. Define the width height of the thumbnail image
  10. Define (' Thumb_width ', 300);
  11. Define (' Thumb_height ', 300);
  12. /**
  13. * Regenerate the uploaded file name
  14. * @return String
  15. * @author Zhao Jinhan
  16. *
  17. */
  18. function _file_type ($filetype = null) {
  19. Switch ($filetype)
  20. {
  21. Case "Image/jpeg":
  22. $fileextname = "JPG";
  23. Break
  24. Case "Image/gif":
  25. $fileextname = "gif";
  26. Break
  27. Case "Image/png":
  28. $fileextname = "png";
  29. Break
  30. Default
  31. $fileextname = false;
  32. Break
  33. }
  34. return $fileextname date (' Ymdhis ', Time ()). $fileextname: false;
  35. }
  36. /**
  37. *
  38. * @param string $filename
  39. * @param string $width
  40. * @param string $height
  41. * @param string $quality
  42. * @param string $savepath
  43. * @return Boolean
  44. */
  45. function _make_thumb ($filename = ", $width =thumb_width, $height =thumb_height, $savepath = './upload ') {
  46. if (file_exists ($filename)) {
  47. Size of the uploaded image
  48. $imagesize =getimagesize ($filename);
  49. $imagewidth = $imagesize [0];
  50. $imageheight = $imagesize [1];
  51. $mime = $imagesize [' MIME '];
  52. Width/height ratio
  53. $ratio = $imagewidth/$imageheight;
  54. Create a new background image
  55. $bgimg = Imagecreatetruecolor ($width, $height);
  56. $white = Imagecolorallocate ($bgimg, 255, 255, 255);
  57. Fill background color to white
  58. Imagefill ($bgimg, 0,0, $white);
  59. if ($mime = = ' Image/gif ') {
  60. $im = @imagecreatefromgif ($filename); /* Attempt to open */
  61. $outfun = ' imagegif ';
  62. }elseif ($mime = = ' Image/png ') {
  63. $im = @imagecreatefrompng ($filename); /* Attempt to open */
  64. $outfun = ' imagepng ';
  65. }else{
  66. $im = @imagecreatefromjpeg ($filename); /* Attempt to open */
  67. $outfun = ' imagejpeg ';
  68. }
  69. if ($ratio > 1) {
  70. Large width
  71. if ($imagewidth > $width) {
  72. Zoom the picture onto the background image
  73. $new _width = $width;
  74. $new _height = ($width * $imageheight)/$imagewidth;
  75. $BG _y = Ceil (ABS (($height-$new _height)/2);
  76. Imagecopyresampled ($bgimg, $im, 0, $bg _y, 0, 0, $new _width, $new _height, $imagewidth, $imageheight);
  77. }else{
  78. Copy picture to background image
  79. $copy = true;
  80. }
  81. }else{
  82. Highly large
  83. if ($imageheight > $height) {
  84. Zoom picture
  85. $new _height = $height;
  86. $new _width = ($height * $imagewidth)/$imageheight;
  87. $BG _x = Ceil (($width-$new _width)/2);
  88. Imagecopyresampled ($bgimg, $im, $bg _x, 0, 0, 0, $new _width, $new _height, $imagewidth, $imageheight);
  89. }else{
  90. Copy picture to background image
  91. $copy = true;
  92. }
  93. }
  94. if ($copy) {
  95. Copy picture to background image
  96. $BG _x = Ceil (($width-$imagewidth)/2);
  97. $BG _y = Ceil (($height-$imageheight)/2);
  98. Imagecopy ($bgimg, $im, $bg _x, $bg _y, 0, 0, $imagewidth, $imageheight);
  99. }
  100. $ext = _file_type ($mime);
  101. $outfun ($bgimg, $savepath. ' /'. $ext);
  102. Imagedestroy ($BGIMG);
  103. return $savepath. ' /'. $ext;
  104. }else{
  105. return false;
  106. }
  107. }
  108. if ($_post) {
  109. $size = $_post[' size ']?strtoupper (Trim ($_post[' size ')): ' 2M ';
  110. $imgsize = $_files[' img ' [' Size ']?$_files[' img '] [' Size ']/(1024*1024): 0;
  111. $imgwidth = $imgheight = $_post[' Width-height ']?intval ($_post[' Width-height ']): 300;
  112. Custom definition File Upload size
  113. Ini_set (' upload_max_filesize ', $size);
  114. $mathsize = Str_replace (' M ', ' ', $size);
  115. if ($imgsize > $mathsize) {
  116. echo "Picture size must not exceed {$size}!";
  117. Return
  118. }
  119. if ($file _name = _file_type ($_files[' img ' [' type '])) {
  120. if ($_files[' img ' [' error '] = = UPLOAD_ERR_OK) {
  121. $savepath = ' upload/';
  122. if (!is_dir ($savepath)) {
  123. mkdir ($savepath, 0644);
  124. }
  125. Generate thumbnail images
  126. $thumb _file = _make_thumb ($_files[' img ' [' tmp_name '], $imgwidth, $imgheight, $savepath);
  127. Move_uploaded_file ($_files[' img '] [' tmp_name '], $savepath. $file _name);
  128. echo "The resulting image is:";
  129. }else{
  130. echo $_files[' img ' [' ERROR '];
  131. Return
  132. }
  133. }else{
  134. echo "image format is not correct, please upload jpg,gif,png format!";
  135. Return
  136. }

  137. }else{

  138. Echo <<
  139. Zoom picture Save to Square
  140. EOT;
  141. }

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.