UploadImg.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Handlers;
  3. use Illuminate\Support\Facades\Log;
  4. class UploadImg
  5. {
  6. /*图片上传函数*/
  7. public function uploadImg($destination_folder,$input_file_name,$maxwidth,$maxheight) {
  8. /******************************************************************************
  9. 参数说明:
  10. $destination_folder //图片存放路径
  11. $input_file_name //文件上传input标签的name
  12. $maxwidth="640";//设置压缩后图片的最大宽度
  13. $maxheight="1136";//设置压缩图片的最大高度
  14. 使用说明: 如果出错看看是不是下面的问题
  15. 1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;
  16. 2. 将extension_dir =改为你的php_gd2.dll所在目录;
  17. ******************************************************************************/
  18. //上传文件类型列表
  19. $uptypes=array(
  20. 'image/jpg',
  21. 'image/jpeg',
  22. 'image/png',
  23. 'image/pjpeg',
  24. 'image/gif',
  25. 'image/bmp',
  26. 'image/x-png'
  27. );
  28. $max_file_size=8000000; //上传文件大小限制, 单位BYTE
  29. $image_name='';
  30. if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_FILES["$input_file_name"]['tmp_name']) )//已选择图片才执行下面
  31. {
  32. if (!is_uploaded_file($_FILES["$input_file_name"]['tmp_name']))
  33. //判断指定的文件是否是通过 HTTP POST 上传的
  34. {
  35. Log::info('121a');
  36. echo "post出错,尝试修改服务器post文件大小限制,默认2M";
  37. exit;
  38. }
  39. $file = $_FILES["$input_file_name"];
  40. if($max_file_size < $file["size"])
  41. //检查文件大小
  42. {
  43. Log::info('123s');
  44. echo "文件太大!";
  45. exit;
  46. }
  47. if(!in_array($file["type"], $uptypes))
  48. //检查文件类型
  49. {
  50. echo "文件类型不符!".$file["type"];
  51. exit;
  52. }
  53. if(!file_exists($destination_folder))
  54. {
  55. mkdir($destination_folder);
  56. }
  57. $filename=$file["tmp_name"];
  58. //$image_size = getimagesize($filename);
  59. $pinfo=pathinfo($file["name"]);
  60. $ftype=$pinfo['extension'];
  61. $current_time = time();
  62. $image_name=$current_time.".".$ftype;
  63. $destination = $destination_folder.$image_name;
  64. if (file_exists($destination) && $overwrite != true)
  65. {
  66. echo "同名文件已经存在了";
  67. exit;
  68. }
  69. if(!move_uploaded_file ($filename, $destination))
  70. {
  71. echo "移动文件出错";
  72. exit;
  73. }
  74. //图片压缩并写回原位置替代原文件
  75. $route=$destination;//原图片路径
  76. $name=$destination_folder.$current_time;//压缩图片存放路径加名称,不带后缀
  77. $filetype=$ftype;//图片类型
  78. self::resizeImage($route,$maxwidth,$maxheight,$name,$filetype);//调用函数
  79. return $image_name;
  80. }
  81. return false;
  82. }
  83. /*图片压缩函数
  84. $route;//原图片的存放路径
  85. $maxwidth="640";//设置图片的最大宽度
  86. $maxheight="1136";//设置图片的最大高度
  87. $name=$destination_folder.$current_time;//压缩图片存放路径加名称,不带后缀
  88. $filetype="jpg";//图片类型
  89. */
  90. public function resizeImage($route,$maxwidth,$maxheight,$name,$filetype)
  91. {
  92. $im = '';
  93. if( !strcasecmp($filetype,"jpg") || !strcasecmp($filetype,"jpeg") ){
  94. $im=imagecreatefromjpeg("$route");//参数是原图片的存放路径
  95. }
  96. else if( !strcasecmp($filetype,"png") ){
  97. $im=imagecreatefrompng("$route");//参数是原图片的存放路径
  98. }
  99. else if( !strcasecmp($filetype,"gif") ){
  100. $im=imagecreatefromgif("$route");//参数是原图片的存放路径
  101. }
  102. $pic_width = imagesx($im);
  103. $pic_height = imagesy($im);
  104. if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)){
  105. if($maxwidth && $pic_width>$maxwidth){
  106. $widthratio = $maxwidth/$pic_width;
  107. $resizewidth_tag = true;
  108. }
  109. if($maxheight && $pic_height>$maxheight){
  110. $heightratio = $maxheight/$pic_height;
  111. $resizeheight_tag = true;
  112. }
  113. if($resizewidth_tag && $resizeheight_tag){
  114. if($widthratio<$heightratio)
  115. $ratio = $widthratio;
  116. else
  117. $ratio = $heightratio;
  118. }
  119. if($resizewidth_tag && !$resizeheight_tag)
  120. $ratio = $widthratio;
  121. if($resizeheight_tag && !$resizewidth_tag)
  122. $ratio = $heightratio;
  123. $newwidth = $pic_width * $ratio;
  124. $newheight = $pic_height * $ratio;
  125. if(function_exists("imagecopyresampled")){
  126. $newim = imagecreatetruecolor($newwidth,$newheight);//PHP系统函数
  127. imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP系统函数
  128. }
  129. else{
  130. $newim = imagecreate($newwidth,$newheight);
  131. imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
  132. }
  133. $name = $name.".".$filetype;
  134. if( !strcasecmp($filetype,"jpg") || !strcasecmp($filetype,"jpeg") ){
  135. imagejpeg($newim,$name);
  136. }
  137. else if( !strcasecmp($filetype,"png") ){
  138. imagepng($newim,$name);
  139. }
  140. // else if( !strcasecmp($filetype,"gif") ){ //不处理GIF文件因为压缩后就不会动了,开注释可处理
  141. // imagegif($newim,$name);
  142. // }
  143. imagedestroy($newim);
  144. }
  145. else{ //原图小于设定的最大长度和宽度,则不进行压缩,原图输出
  146. $name = $name.".".$filetype;
  147. if( !strcasecmp($filetype,"jpg") && !strcasecmp($filetype,"jpeg") ){
  148. imagejpeg($im,$name);
  149. }
  150. else if( !strcasecmp($filetype,"png") ){
  151. imagepng($im,$name);
  152. }
  153. // else if( !strcasecmp($filetype,"gif") ){ //不处理GIF文件因为压缩后就不会动了,开注释可处理
  154. // imagegif($im,$name);
  155. // }
  156. }
  157. }
  158. }