Home > Backend Development > PHP Tutorial > Code for php uploading pictures to scale pictures in equal proportions

Code for php uploading pictures to scale pictures in equal proportions

WBOY
Release: 2016-07-25 08:51:48
Original
1012 people have browsed it
  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 thumbnail Width and height
  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. //Upload Image size
  48. $imagesize=getimagesize($filename);
  49. $imagewidth=$imagesize[0];
  50. $imageheight=$imagesize[1];
  51. $mime = $imagesize['mime'];
  52. //Width and 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 the 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. //The width is larger
  71. if ($imagewidth > height-$new_height)/2));
  72. imagecopyresampled($bgimg, $im, 0, $bg_y, 0, 0, $new_width, $new_height, $imagewidth, $imageheight);
  73. }else{
  74. //Copy image Go to the background image
  75. $copy = true;
  76. }
  77. }else{
  78. //The height is larger
  79. if($imageheight > $height){
  80. //Scale the image
  81. $new_height = $height;
  82. $new_width = ( $height*$imagewidth)/$imageheight;
  83. $bg_x = ceil(($width-$new_width)/2);
  84. imagecopyresampled($bgimg, $im, $bg_x, 0, 0, 0, $new_width, $new_height , $imagewidth, $imageheight);
  85. }else{
  86. //Copy the image to the background image
  87. $copy = true;
  88. }
  89. }
  90. if($copy){
  91. //Copy the image to the background image
  92. $bg_x = ceil(($width-$imagewidth)/2);
  93. $bg_y = ceil(($height-$imageheight)/2);
  94. imagecopy($bgimg, $im, $bg_x, $bg_y, 0, 0, $imagewidth, $imageheight);
  95. }
  96. $ext = _file_type($mime);
  97. $outfun($bgimg, $savepath.'/'.$ext);
  98. imagedestroy($bgimg);
  99. return $savepath.' /'.$ext;
  100. }else{
  101. return false;
  102. }
  103. }
  104. if($_POST){
  105. $size = $_POST['size']?strtoupper(trim($_POST['size'])) :'2M';
  106. $imgsize = $_FILES['img']['size']?$_FILES['img']['size']/(1024*1024):0;
  107. $imgwidth = $imgheight = $_POST['width-height']?intval($_POST['width-height']):300;
  108. //Customize file upload size
  109. ini_set('upload_max_filesize',$size);
  110. $mathsize = str_replace ('M','',$size);
  111. if($imgsize>$mathsize){
  112. echo "The image size must not exceed {$size}!";
  113. return;
  114. }
  115. if($file_name = _file_type($ _FILES['img']['type'])){
  116. if($_FILES['img']['error'] == UPLOAD_ERR_OK){
  117. $savepath = 'upload/';
  118. if(!is_dir($ savepath)){
  119. mkdir($savepath,0644);
  120. }
  121. //Generate thumbnail
  122. $thumb_file = _make_thumb($_FILES['img']['tmp_name'], $imgwidth, $imgheight, $savepath);
  123. //move_uploaded_file($_FILES['img']['tmp_name'],$savepath.$file_name);
  124. echo "The generated image is: ";
  125. }else{
  126. echo $_FILES['img']['error'];
  127. return;
  128. }
  129. }else{
  130. echo "The image format is incorrect, please upload jpg, gif, png format!";
  131. return;
  132. }

  133. }else{

  134. echo <<
  135. Scale the image and save it into a square
  • EOT;
  • }

  • Copy code


  • 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