You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

265 lines
8.1 KiB

  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 yii\base\Exception;
  17. class UploadPic
  18. {
  19. /**
  20. * Des:生成新图片
  21. * Name: _UPLOADPIC
  22. * @param $upfile
  23. * @param $maxsize
  24. * @param $updir
  25. * @return array
  26. * @author 倪宗锋
  27. */
  28. public static function _UPLOADPIC($upfile, $maxsize, $updir)
  29. {
  30. $type = $upfile ["type"];
  31. $size = $upfile ["size"];
  32. switch ($type) {
  33. case 'image/jpeg' :
  34. $extend = ".jpg";
  35. break;
  36. }
  37. if (empty($extend)) {
  38. return Util::returnArrEr('只能上传图片类型:JPG ');
  39. }
  40. $pin = 100;
  41. if ($size > $maxsize) {
  42. $pin = round($maxsize / $size, 2) * 100;
  43. }
  44. try{
  45. list($width, $height) = getimagesize($upfile['tmp_name']);
  46. move_uploaded_file($upfile['tmp_name'], $updir);
  47. $new_image = imagecreatetruecolor($width, $height);
  48. $old_image = imagecreatefromjpeg($updir);
  49. imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $width, $height, $width, $height);
  50. imagejpeg($new_image, $updir, $pin);
  51. return Util::returnArrSu('', ['url' => $updir]);
  52. }catch (Exception $e){
  53. return Util::returnArrEr('上传图片失败! ');
  54. }
  55. }
  56. /**
  57. * Des:获取比例压缩后的图片大小
  58. * Name: show_pic_scal
  59. * @param $width
  60. * @param $height
  61. * @param $picpath
  62. * @return mixed
  63. * @author 倪宗锋
  64. */
  65. public static function show_pic_scal($width, $height, $picpath)
  66. {
  67. $imginfo = GetImageSize($picpath);
  68. $imgw = $imginfo [0];
  69. $imgh = $imginfo [1];
  70. $ra = number_format(($imgw / $imgh), 1); //宽高比
  71. $ra2 = number_format(($imgh / $imgw), 1); //高宽比
  72. if ($imgw > $width or $imgh > $height) {
  73. if ($imgw > $imgh) {
  74. $newWidth = $width;
  75. $newHeight = round($newWidth / $ra);
  76. } elseif ($imgw < $imgh) {
  77. $newHeight = $height;
  78. $newWidth = round($newHeight / $ra2);
  79. } else {
  80. $newWidth = $width;
  81. $newHeight = round($newWidth / $ra);
  82. }
  83. } else {
  84. $newHeight = $imgh;
  85. $newWidth = $imgw;
  86. }
  87. $newsize [0] = $newWidth;
  88. $newsize [1] = $newHeight;
  89. return $newsize;
  90. }
  91. public static function getImageInfo($src)
  92. {
  93. return getimagesize($src);
  94. }
  95. /**
  96. * 创建图片,返回资源类型
  97. * @param string $src 图片路径
  98. * @return resource $im 返回资源类型
  99. * **/
  100. public static function create($src)
  101. {
  102. $info = static::getImageInfo($src);
  103. switch ($info[2]) {
  104. case 1:
  105. $im = imagecreatefromgif($src);
  106. break;
  107. case 2:
  108. $im = imagecreatefromjpeg($src);
  109. break;
  110. default:
  111. $im = imagecreatefrompng($src);
  112. break;
  113. }
  114. return $im;
  115. }
  116. /**
  117. * 缩略图主函数
  118. * @param string $src 图片路径
  119. * @param int $w 缩略图宽度
  120. * @param int $h 缩略图高度
  121. * @return mixed 返回缩略图路径
  122. * **/
  123. public static function resize($src, $w, $h, $savepath)
  124. {
  125. //获取图片的基本信息
  126. $info = static::getImageInfo($src);
  127. $width = $info[0];//获取图片宽度
  128. $height = $info[1];//获取图片高度
  129. $per1 = round($width / $height, 2);//计算原图长宽比
  130. $per2 = round($w / $h, 2);//计算缩略图长宽比
  131. //计算缩放比例
  132. if ($per1 > $per2 || $per1 == $per2) {
  133. //原图长宽比大于或者等于缩略图长宽比,则按照宽度优先
  134. $per = $w / $width;
  135. } else {
  136. //原图长宽比小于缩略图长宽比,则按照高度优先
  137. $per = $h / $height;
  138. }
  139. $temp_w = intval($width * $per);//计算原图缩放后的宽度
  140. $temp_h = intval($height * $per);//计算原图缩放后的高度
  141. $temp_img = imagecreatetruecolor($temp_w, $temp_h);//创建画布
  142. $im = static::create($src);
  143. imagecopyresampled($temp_img, $im, 0, 0, 0, 0, $temp_w, $temp_h, $width, $height);
  144. if ($per1 > $per2) {
  145. imagejpeg($temp_img, $savepath, 100);
  146. imagedestroy($im);
  147. return static::addBg($savepath, $w, $h, "w");
  148. //宽度优先,在缩放之后高度不足的情况下补上背景
  149. }
  150. if ($per1 == $per2) {
  151. imagejpeg($temp_img, $savepath, 100);
  152. imagedestroy($im);
  153. return $savepath;
  154. //等比缩放
  155. }
  156. if ($per1 < $per2) {
  157. imagejpeg($temp_img, $savepath, 100);
  158. imagedestroy($im);
  159. return static::addBg($savepath, $w, $h, "h");
  160. //高度优先,在缩放之后宽度不足的情况下补上背景
  161. }
  162. return '';
  163. }
  164. /**
  165. * Des:添加背景 返回加上背景的图片
  166. * Name: addBg
  167. * @param string $src 图片路径
  168. * @param int $w 背景图像宽度
  169. * @param int $h 背景图像高度
  170. * @param String $first 决定图像最终位置的,w 宽度优先 h 高度优先 wh:等比
  171. * @return mixed
  172. * @author 倪宗锋
  173. */
  174. public static function addBg($src, $w, $h, $first = "w")
  175. {
  176. $bg = imagecreatetruecolor($w, $h);
  177. $white = imagecolorallocate($bg, 255, 255, 255);
  178. imagefill($bg, 0, 0, $white);//填充背景
  179. //获取目标图片信息
  180. $info = static::getImageInfo($src);
  181. $width = $info[0];//目标图片宽度
  182. $height = $info[1];//目标图片高度
  183. $img = static::create($src);
  184. if ($first == "wh") {
  185. //等比缩放
  186. return $src;
  187. } else {
  188. if ($first == "w") {
  189. $x = 0;
  190. $y = ($h - $height) / 2;//垂直居中
  191. } else {
  192. $x = ($w - $width) / 2;//水平居中
  193. $y = 0;
  194. }
  195. imagecopymerge($bg, $img, $x, $y, 0, 0, $width, $height, 100);
  196. imagejpeg($bg, $src, 100);
  197. imagedestroy($bg);
  198. imagedestroy($img);
  199. return $src;
  200. }
  201. }
  202. /**
  203. * Des:剪切图片
  204. * Name: cutPic
  205. * @param $image
  206. * @param $upFile
  207. * @param int $xx
  208. * @param int $yy
  209. * @return array
  210. * @author 倪宗锋
  211. */
  212. public static function cutPic($image, $upFile, $xx = 300, $yy = 300)
  213. {
  214. $imgstream = file_get_contents($image);
  215. $im = imagecreatefromstring($imgstream);
  216. $x = imagesx($im);//获取图片的宽
  217. $y = imagesy($im);//获取图片的高
  218. if ($x > $y) {
  219. //图片宽大于高
  220. $sx = abs(($y - $x) / 2);
  221. $sy = 0;
  222. $thumbw = $y;
  223. $thumbh = $y;
  224. } else {
  225. //图片高大于等于宽
  226. $sy = abs(($x - $y) / 2.5);
  227. $sx = 0;
  228. $thumbw = $x;
  229. $thumbh = $x;
  230. }
  231. if (function_exists("imagecreatetruecolor")) {
  232. $dim = imagecreatetruecolor($yy, $xx); // 创建目标图gd2
  233. } else {
  234. $dim = imagecreate($yy, $xx); // 创建目标图gd1
  235. }
  236. imageCopyreSampled($dim, $im, 0, 0, $sx, $sy, $yy, $xx, $thumbw, $thumbh);
  237. imagejpeg($dim, $upFile);
  238. return Util::returnArrSu('', array('url' => $upFile));
  239. }
  240. }