ImageResize.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2016/1/11
  6. * Time: 14:34
  7. */
  8. namespace common\library;
  9. /**
  10. * 缩略图生成类,使用示例:
  11. **/
  12. class ImageResize
  13. {
  14. private $localimage;//原图路径
  15. private $remoteimage;//缩略图保存路径
  16. private $localinfo;//原图属性
  17. private $error;
  18. function resize($localimg, $remoteimg, $x, $y)
  19. {
  20. //检测是否支持gd图像处理
  21. if (!$this->_checkenv()) {
  22. return false;
  23. }
  24. $this->localimage = $localimg;
  25. $this->remoteimage = $remoteimg;
  26. $this->localinfo = getimagesize($this->localimage); //获取本地图像的信息
  27. return $this->_resize($x, $y);
  28. }
  29. function cut_resize($localimg, $remoteimg, $x, $y){
  30. //检测是否支持gd图像处理
  31. if (!$this->_checkenv()) {
  32. return false;
  33. }
  34. $this->localimage = $localimg;
  35. $this->remoteimage = $remoteimg;
  36. $this->localinfo = getimagesize($this->localimage); //获取本地图像的信息
  37. return $this->_resizeAndCut($x,$y);
  38. }
  39. /**
  40. * 检测当前环境是否支持GD
  41. * @return bool
  42. */
  43. private function _checkenv()
  44. {
  45. if (!function_exists('gd_info')) {
  46. $this->error[] = "当前环境不支持GD图像处理,请先安装GD库并开启PHP相关扩展";
  47. return false;
  48. }
  49. return true;
  50. }
  51. /**
  52. * @param $x
  53. * @param $y
  54. * @return bool
  55. */
  56. private function _resize($x, $y)
  57. {
  58. if (!$this->localinfo) {
  59. $this->error[] = "本地图像文件不存在";
  60. return false;
  61. }
  62. //创建图像句柄
  63. $im = @$this->_create($this->localinfo[2]);
  64. if (!$im) {
  65. $this->error[] = "当前GD库不支持图像类型:{$this->localinfo['mime']}";
  66. return false;
  67. }
  68. $dstsize = $this->_dstsize($x, $y);
  69. $dstim = @imagecreatetruecolor($dstsize["width"], $dstsize["height"]);
  70. if($this->localinfo[2] == 3)
  71. $whitecolor = @imagecolorallocatealpha($dstim, 255, 255, 255, 127);
  72. else
  73. $whitecolor = @imagecolorallocatealpha($dstim, 0, 0, 0, 127);
  74. imagefill($dstim, 0, 0, $whitecolor);
  75. $re = @imagecopyresampled($dstim, $im, 0, 0, 0, 0, $dstsize["width"], $dstsize["height"], $this->localinfo[0], $this->localinfo[1]);
  76. if (!$re) {
  77. $this->error[] = "图像重新采样失败";
  78. return false;
  79. }
  80. if (!imagejpeg($dstim, $this->remoteimage)) {
  81. if (!imagepng($dstim, $this->remoteimage)) {
  82. if (!imagegif($dstim, $this->remoteimage)) {
  83. $this->error[] = "保存缩略图到{$this->remoteimage}失败,请检查gd环境是否正常和缩略图文件夹的写入权限。";
  84. return false;
  85. }
  86. }
  87. }
  88. $this->error[] = "success";
  89. return true;
  90. }
  91. /**
  92. * 修改图片尺寸并裁剪为正方形
  93. * @param $x
  94. * @param $y
  95. * @return bool
  96. */
  97. private function _resizeAndCut($x,$y){
  98. if (!$this->localinfo) {
  99. $this->error[] = "本地图像文件不存在";
  100. return false;
  101. }
  102. //创建图像句柄
  103. $im = @$this->_create($this->localinfo[2]);
  104. if (!$im) {
  105. $this->error[] = "当前GD库不支持图像类型:{$this->localinfo['mime']}";
  106. return false;
  107. }
  108. $dstsize = $this->_dstsize($x, $y);
  109. $dstposition = $this->_initPosition($this->localinfo[0],$this->localinfo[1]);
  110. $dstminsize = min($dstsize);
  111. $srcminsize = min($this->localinfo[0],$this->localinfo[1]);
  112. $dstim = @imagecreatetruecolor($dstminsize, $dstminsize);
  113. $whitecolor = @imagecolorallocatealpha($dstim, 0, 0, 0, 127);
  114. imagefill($dstim, 0, 0, $whitecolor);
  115. $re = @imagecopyresampled($dstim, $im, 0, 0, $dstposition['x'], $dstposition['y'], $dstminsize, $dstminsize, $srcminsize, $srcminsize);
  116. if (!$re) {
  117. $this->error[] = "图像重新采样失败";
  118. return false;
  119. }
  120. if (!imagejpeg($dstim, $this->remoteimage)) {
  121. if (!imagepng($dstim, $this->remoteimage)) {
  122. if (!imagegif($dstim, $this->remoteimage)) {
  123. $this->error[] = "保存缩略图到{$this->remoteimage}失败,请检查gd环境是否正常和缩略图文件夹的写入权限。";
  124. return false;
  125. }
  126. }
  127. }
  128. $this->error[] = "success";
  129. return true;
  130. }
  131. /**
  132. *
  133. * 根据本地图片类型,创建图片资源
  134. * @param $code 图像类型代码
  135. * @return resource/boolean 成功则返回resourse失败则返回false
  136. */
  137. private function _create($code)
  138. {
  139. $src = $this->localimage;
  140. switch ($code) {
  141. case 1:
  142. return imagecreatefromgif($src);
  143. break;
  144. case 2:
  145. return imagecreatefromjpeg($src);
  146. break;
  147. case 3:
  148. return imagecreatefrompng($src);
  149. break;
  150. default:
  151. return false;
  152. break;
  153. }
  154. }
  155. /**
  156. * 计算长宽
  157. * @param $x
  158. * @param $y
  159. */
  160. private function _dstsize($x, $y)
  161. {
  162. list($srcwidth, $srcheight) = $this->localinfo;
  163. if (($srcwidth / $srcheight) < ($x / $y)) {
  164. $x = floor($y * $srcwidth / $srcheight);
  165. } else {
  166. $y = floor($x * $srcheight / $srcwidth);
  167. }
  168. $dstsize["width"] = $x;
  169. $dstsize["height"] = $y;
  170. return $dstsize;
  171. }
  172. /**
  173. ** 获取最后一条错误信息
  174. * * return string
  175. **/
  176. function GetLastError()
  177. {
  178. return array_pop($this->error);
  179. }
  180. /**
  181. * * 获取所有错误信息
  182. * * return array
  183. * */
  184. function GetAllError()
  185. {
  186. return $this->error;
  187. }
  188. function test(){
  189. }
  190. /**
  191. * 计算初始坐标
  192. * @return array
  193. */
  194. function _initPosition($x,$y,$init = ''){
  195. $local = ['x'=>'','y'=>''];
  196. if(bccomp($x,$y) == -1){
  197. $local['x'] = '0';
  198. $local['y'] = bcsub($y,$x,2)/2;
  199. }else{
  200. $local['y'] = '0';
  201. $local['x'] = bcsub($x,$y,2)/2;
  202. }
  203. return $local;
  204. }
  205. }