Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm UploadPic.php
  13. * Create By 2017/3/10 15:10 $
  14. */
  15. namespace common\util;
  16. use backend\modules\api\util\Util;
  17. use yii\base\Exception;
  18. class UploadPic
  19. {
  20. /**
  21. * Des:生成新图片
  22. * Name: _UPLOADPIC
  23. * @param $upfile
  24. * @param $maxsize
  25. * @param $updir
  26. * @return array
  27. * @author 倪宗锋
  28. */
  29. public static function _UPLOADPIC($upfile, $maxsize, $updir)
  30. {
  31. $type = $upfile ["type"];
  32. $size = $upfile ["size"];
  33. switch ($type) {
  34. case 'image/jpeg' :
  35. $extend = ".jpg";
  36. break;
  37. }
  38. if (empty($extend)) {
  39. return Util::returnArrEr('只能上传图片类型:JPG ');
  40. }
  41. $pin = 100;
  42. if ($size > $maxsize) {
  43. $pin = round($maxsize / $size, 2) * 100;
  44. }
  45. try{
  46. list($width, $height) = getimagesize($upfile['tmp_name']);
  47. move_uploaded_file($upfile['tmp_name'], $updir);
  48. $new_image = imagecreatetruecolor($width, $height);
  49. $old_image = imagecreatefromjpeg($updir);
  50. imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $width, $height, $width, $height);
  51. imagejpeg($new_image, $updir, $pin);
  52. return Util::returnArrSu('', ['url' => $updir]);
  53. }catch (Exception $e){
  54. return Util::returnArrEr('上传图片失败! ');
  55. }
  56. }
  57. /**
  58. * Des:获取比例压缩后的图片大小
  59. * Name: show_pic_scal
  60. * @param $width
  61. * @param $height
  62. * @param $picpath
  63. * @return mixed
  64. * @author 倪宗锋
  65. */
  66. public static function show_pic_scal($width, $height, $picpath)
  67. {
  68. $imginfo = GetImageSize($picpath);
  69. $imgw = $imginfo [0];
  70. $imgh = $imginfo [1];
  71. $ra = number_format(($imgw / $imgh), 1); //宽高比
  72. $ra2 = number_format(($imgh / $imgw), 1); //高宽比
  73. if ($imgw > $width or $imgh > $height) {
  74. if ($imgw > $imgh) {
  75. $newWidth = $width;
  76. $newHeight = round($newWidth / $ra);
  77. } elseif ($imgw < $imgh) {
  78. $newHeight = $height;
  79. $newWidth = round($newHeight / $ra2);
  80. } else {
  81. $newWidth = $width;
  82. $newHeight = round($newWidth / $ra);
  83. }
  84. } else {
  85. $newHeight = $imgh;
  86. $newWidth = $imgw;
  87. }
  88. $newsize [0] = $newWidth;
  89. $newsize [1] = $newHeight;
  90. return $newsize;
  91. }
  92. public static function getImageInfo($src)
  93. {
  94. return getimagesize($src);
  95. }
  96. /**
  97. * 创建图片,返回资源类型
  98. * @param string $src 图片路径
  99. * @return resource $im 返回资源类型
  100. * **/
  101. public static function create($src)
  102. {
  103. $info = static::getImageInfo($src);
  104. switch ($info[2]) {
  105. case 1:
  106. $im = imagecreatefromgif($src);
  107. break;
  108. case 2:
  109. $im = imagecreatefromjpeg($src);
  110. break;
  111. default:
  112. $im = imagecreatefrompng($src);
  113. break;
  114. }
  115. return $im;
  116. }
  117. /**
  118. * 缩略图主函数
  119. * @param string $src 图片路径
  120. * @param int $w 缩略图宽度
  121. * @param int $h 缩略图高度
  122. * @param string $savepath
  123. * @return mixed 返回缩略图路径
  124. * **/
  125. public static function resize($src, $w, $h, $savepath)
  126. {
  127. //获取图片的基本信息
  128. $info = static::getImageInfo($src);
  129. $width = $info[0];//获取图片宽度
  130. $height = $info[1];//获取图片高度
  131. $per1 = round($width / $height, 2);//计算原图长宽比
  132. $per2 = round($w / $h, 2);//计算缩略图长宽比
  133. //计算缩放比例
  134. if ($per1 > $per2 || $per1 == $per2) {
  135. //原图长宽比大于或者等于缩略图长宽比,则按照宽度优先
  136. $per = $w / $width;
  137. } else {
  138. //原图长宽比小于缩略图长宽比,则按照高度优先
  139. $per = $h / $height;
  140. }
  141. $temp_w = intval($width * $per);//计算原图缩放后的宽度
  142. $temp_h = intval($height * $per);//计算原图缩放后的高度
  143. $temp_img = imagecreatetruecolor($temp_w, $temp_h);//创建画布
  144. $im = static::create($src);
  145. imagecopyresampled($temp_img, $im, 0, 0, 0, 0, $temp_w, $temp_h, $width, $height);
  146. if ($per1 > $per2) {
  147. imagejpeg($temp_img, $savepath, 100);
  148. imagedestroy($im);
  149. return static::addBg($savepath, $w, $h, "w");
  150. //宽度优先,在缩放之后高度不足的情况下补上背景
  151. }
  152. if ($per1 == $per2) {
  153. imagejpeg($temp_img, $savepath, 100);
  154. imagedestroy($im);
  155. return $savepath;
  156. //等比缩放
  157. }
  158. if ($per1 < $per2) {
  159. imagejpeg($temp_img, $savepath, 100);
  160. imagedestroy($im);
  161. return static::addBg($savepath, $w, $h, "h");
  162. //高度优先,在缩放之后宽度不足的情况下补上背景
  163. }
  164. return '';
  165. }
  166. /**
  167. * Des:添加背景 返回加上背景的图片
  168. * Name: addBg
  169. * @param string $src 图片路径
  170. * @param int $w 背景图像宽度
  171. * @param int $h 背景图像高度
  172. * @param String $first 决定图像最终位置的,w 宽度优先 h 高度优先 wh:等比
  173. * @return mixed
  174. * @author 倪宗锋
  175. */
  176. public static function addBg($src, $w, $h, $first = "w")
  177. {
  178. $bg = imagecreatetruecolor($w, $h);
  179. $white = imagecolorallocate($bg, 255, 255, 255);
  180. imagefill($bg, 0, 0, $white);//填充背景
  181. //获取目标图片信息
  182. $info = static::getImageInfo($src);
  183. $width = $info[0];//目标图片宽度
  184. $height = $info[1];//目标图片高度
  185. $img = static::create($src);
  186. if ($first == "wh") {
  187. //等比缩放
  188. return $src;
  189. } else {
  190. if ($first == "w") {
  191. $x = 0;
  192. $y = ($h - $height) / 2;//垂直居中
  193. } else {
  194. $x = ($w - $width) / 2;//水平居中
  195. $y = 0;
  196. }
  197. imagecopymerge($bg, $img, $x, $y, 0, 0, $width, $height, 100);
  198. imagejpeg($bg, $src, 100);
  199. imagedestroy($bg);
  200. imagedestroy($img);
  201. return $src;
  202. }
  203. }
  204. /**
  205. * Des:剪切图片
  206. * Name: cutPic
  207. * @param $source_path
  208. * @param $upFile
  209. * @param int $target_width
  210. * @param int $target_height
  211. * @return array
  212. * @author 倪宗锋
  213. */
  214. public static function cutPic($source_path, $upFile, $target_width = 300, $target_height = 300)
  215. {
  216. $source_info = getimagesize($source_path);
  217. $source_width = $source_info[0];
  218. $source_height = $source_info[1];
  219. $source_mime = $source_info['mime'];
  220. $source_ratio = $source_height / $source_width;
  221. $target_ratio = $target_height / $target_width;
  222. // 源图过高
  223. if ($source_ratio > $target_ratio) {
  224. $cropped_width = $source_width;
  225. $cropped_height = $source_width * $target_ratio;
  226. $source_x = 0;
  227. $source_y = ($source_height - $cropped_height) / 2;
  228. } elseif ($source_ratio < $target_ratio) { // 源图过宽
  229. $cropped_width = $source_height / $target_ratio;
  230. $cropped_height = $source_height;
  231. $source_x = ($source_width - $cropped_width) / 2;
  232. $source_y = 0;
  233. } else { // 源图适中
  234. $cropped_width = $source_width;
  235. $cropped_height = $source_height;
  236. $source_x = 0;
  237. $source_y = 0;
  238. }
  239. switch ($source_mime) {
  240. case 'image/gif':
  241. $source_image = imagecreatefromgif($source_path);
  242. break;
  243. case 'image/jpeg':
  244. $source_image = imagecreatefromjpeg($source_path);
  245. break;
  246. case 'image/png':
  247. $source_image = imagecreatefrompng($source_path);
  248. break;
  249. default:
  250. return Util::returnArrEr();
  251. break;
  252. }
  253. $target_image = imagecreatetruecolor($target_width, $target_height);
  254. $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
  255. // 裁剪
  256. imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
  257. // 缩放
  258. imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
  259. imagejpeg($target_image, $upFile, 100);
  260. imagedestroy($source_image);
  261. imagedestroy($target_image);
  262. imagedestroy($cropped_image);
  263. return Util::returnArrSu('', array('url' => $upFile));
  264. }
  265. }