Php code for uploading Image proportional scaling

Source: Internet
Author: User
Php code for uploading Image proportional scaling

  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 and height of the thumbnail
  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 background image
  55. $ Bgimg = imagecreatetruecolor ($ width, $ height );
  56. $ White = imagecolorallocate ($ BGB, 255,255,255 );
  57. // The background color is 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. // The width is large.
  71. If ($ imagewidth> $ width ){
  72. // Scale the image to 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 the image to the background image
  79. $ Copy = true;
  80. }
  81. } Else {
  82. // High
  83. If ($ imageheight> $ height ){
  84. // Scale the image
  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 the image to the background image
  91. $ Copy = true;
  92. }
  93. }
  94. If ($ copy ){
  95. // Copy the image to the 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. // Customize the file upload size
  113. Ini_set ('upload _ max_filesize ', $ size );
  114. $ Mathsize = str_replace ('M', '', $ size );
  115. If ($ imgsize> $ mathsize ){
  116. Echo "the image size cannot 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 a thumbnail
  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 generated image is :";
  129. } Else {
  130. Echo $ _ FILES ['IMG '] ['error'];
  131. Return;
  132. }
  133. } Else {
  134. Echo "the image format is incorrect. Upload jpg, gif, and png formats! ";
  135. Return;
  136. }

  137. } Else {

  138. Echo <
  139. Save the scaled image as a square
  140. EOT;
  141. }

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.