Image.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liuyifan
  5. * Date: 2019/3/19
  6. * Time: 10:13
  7. */
  8. namespace app\platform\modules\system\models;
  9. use app\common\models\BaseModel;
  10. class Image extends BaseModel
  11. {
  12. private $src;
  13. private $actions = array();
  14. private $resize_width = 0;
  15. private $resize_height = 0;
  16. private $image = null;
  17. private $imageinfo = array();
  18. private $crop_width = 0;
  19. private $crop_height = 0;
  20. private $crop_position = 1;
  21. private $ext = '';
  22. public function __construct($src)
  23. {
  24. $this->src = $src;
  25. $this->ext = pathinfo($src, PATHINFO_EXTENSION);
  26. }
  27. public static function create($src)
  28. {
  29. return new self($src);
  30. }
  31. public function resize($width = 0, $height = 0)
  32. {
  33. if ($width > 0 || $height > 0) {
  34. $this->actions[] = 'resize';
  35. }
  36. if ($width > 0 && $height == 0) {
  37. $height = $width;
  38. }
  39. if ($height > 0 && $width == 0) {
  40. $width = $height;
  41. }
  42. $this->resize_width = $width;
  43. $this->resize_height = $height;
  44. return $this;
  45. }
  46. public function crop($width = 400, $height = 300, $position = 1)
  47. {
  48. if ($width > 0 || $height > 0) {
  49. $this->actions[] = 'crop';
  50. }
  51. if ($width > 0 && $height == 0) {
  52. $height = $width;
  53. }
  54. if ($height > 0 && $width == 0) {
  55. $width = $height;
  56. }
  57. $this->crop_width = $width;
  58. $this->crop_height = $height;
  59. $this->crop_position = min(intval($position), 9);
  60. return $this;
  61. }
  62. public function getExt()
  63. {
  64. return in_array($this->ext, array('jpg', 'jpeg', 'png', 'gif')) ? $this->ext : 'jpeg';
  65. }
  66. public function isPng()
  67. {
  68. return file_is_image($this->src) && $this->getExt() == 'png';
  69. }
  70. public function isJPEG()
  71. {
  72. return file_is_image($this->src) && in_array($this->getExt(), array('jpg', 'jpeg'));
  73. }
  74. public function isGif()
  75. {
  76. return file_is_image($this->src) && $this->getExt() == 'gif';
  77. }
  78. public function saveTo($path, $quality = null)
  79. {
  80. $path = safe_gpc_path($path);
  81. if (empty($path)) {
  82. return false;
  83. }
  84. $result = $this->handle();
  85. if (!$result) {
  86. return false;
  87. }
  88. $ext = $this->getExt();
  89. if ($ext == 'jpg') {
  90. $ext = 'jpeg';
  91. }
  92. $func = 'image' . $ext;
  93. $real_quality = $this->realQuality($quality);
  94. $saved = false;
  95. $image = $this->image();
  96. imagealphablending($image, false);
  97. imagesavealpha($image, true);
  98. if (is_null($real_quality)) {
  99. $saved = $func($image, $path);
  100. } else {
  101. if (!$this->isGif()) {
  102. $saved = $func($image, $path, $real_quality);
  103. }
  104. }
  105. $this->destroys();
  106. return $saved ? $path : $saved;
  107. }
  108. private function realQuality($quality = null)
  109. {
  110. if (is_null($quality)) {
  111. return null;
  112. }
  113. $quality = min($quality, 100);
  114. if ($this->isJPEG()) {
  115. return $quality * 0.75;
  116. }
  117. if ($this->isPng()) {
  118. return round(abs((100 - $quality) / 11.111111));
  119. }
  120. return null;
  121. }
  122. protected function handle()
  123. {
  124. if (!function_exists('gd_info')) {
  125. return false;
  126. }
  127. $this->image = $this->createResource();
  128. if (!$this->image) {
  129. return false;
  130. }
  131. $this->imageinfo = getimagesize($this->src);
  132. $actions = array_unique($this->actions);
  133. $src_image = $this->image;
  134. foreach ($actions as $action) {
  135. $method = 'do' . ucfirst($action);
  136. $src_image = $this->{$method}($src_image);
  137. }
  138. $this->image = $src_image;
  139. return true;
  140. }
  141. protected function doCrop($src_image)
  142. {
  143. list($dst_x, $dst_y) = $this->getCropDestPoint();
  144. if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
  145. $new_image = imagecrop($src_image, array('x' => $dst_x, 'y' => $dst_y, 'width' => $this->crop_width, 'height' => $this->crop_height));
  146. imagedestroy($src_image);
  147. } else {
  148. $new_image = $this->modify($src_image, $this->crop_width,
  149. $this->crop_height, $this->crop_width, $this->crop_height, 0, 0, $dst_x, $dst_y);
  150. }
  151. $this->imageinfo[0] = $this->crop_width;
  152. $this->imageinfo[1] = $this->crop_height;
  153. return $new_image;
  154. }
  155. protected function doResize($src_image)
  156. {
  157. $newimage = $this->modify($src_image, $this->resize_width, $this->resize_height,
  158. $this->imageinfo[0], $this->imageinfo[1]);
  159. $this->imageinfo[0] = $this->resize_width;
  160. $this->imageinfo[1] = $this->resize_height;
  161. return $newimage;
  162. }
  163. protected function modify($src_image, $width, $height, $src_width,
  164. $src_height, $dst_x = 0, $dst_y = 0, $src_x = 0, $src_y = 0)
  165. {
  166. $image = imagecreatetruecolor($width, $height);
  167. imagealphablending($image, false);
  168. imagesavealpha($image, true);
  169. imagecopyresampled($image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $width, $height,
  170. $src_width, $src_height);
  171. imagedestroy($src_image);
  172. return $image;
  173. }
  174. private function image()
  175. {
  176. return $this->image;
  177. }
  178. private function destroys()
  179. {
  180. if ($this->image) {
  181. imagedestroy($this->image);
  182. }
  183. }
  184. private function createResource()
  185. {
  186. if (file_exists($this->src) && !is_readable($this->src)) {
  187. return null;
  188. }
  189. if ($this->isPng()) {
  190. return imagecreatefrompng($this->src);
  191. }
  192. if ($this->isJPEG()) {
  193. return imagecreatefromjpeg($this->src);
  194. }
  195. if ($this->isGif()) {
  196. return imagecreatefromgif($this->src);
  197. }
  198. return null;
  199. }
  200. public function toBase64($prefix = 'data:image/%s;base64,')
  201. {
  202. $filename = tempnam('tmp', 'base64');
  203. $prefix = sprintf($prefix, $this->getExt());
  204. $result = $this->saveTo($filename);
  205. if (!$result) {
  206. return false;
  207. }
  208. $content = file_get_contents($filename);
  209. $base64 = base64_encode($content);
  210. unlink($filename);
  211. return $prefix . $base64;
  212. }
  213. private function getCropDestPoint()
  214. {
  215. $s_width = $this->imageinfo[0];
  216. $s_height = $this->imageinfo[1];
  217. $dst_x = $dst_y = 0;
  218. if ($this->crop_width == '0' || $this->crop_width > $s_width) {
  219. $this->crop_width = $s_width;
  220. }
  221. if ($this->crop_height == '0' || $this->crop_height > $s_height) {
  222. $this->crop_height = $s_height;
  223. }
  224. switch ($this->crop_position) {
  225. case 0:
  226. case 1:
  227. $dst_x = 0;
  228. $dst_y = 0;
  229. break;
  230. case 2:
  231. $dst_x = ($s_width - $this->crop_width) / 2;
  232. $dst_y = 0;
  233. break;
  234. case 3:
  235. $dst_x = $s_width - $this->crop_width;
  236. $dst_y = 0;
  237. break;
  238. case 4:
  239. $dst_x = 0;
  240. $dst_y = ($s_height - $this->crop_height) / 2;
  241. break;
  242. case 5:
  243. $dst_x = ($s_width - $this->crop_width) / 2;
  244. $dst_y = ($s_height - $this->crop_height) / 2;
  245. break;
  246. case 6:
  247. $dst_x = $s_width - $this->crop_width;
  248. $dst_y = ($s_height - $this->crop_height) / 2;
  249. break;
  250. case 7:
  251. $dst_x = 0;
  252. $dst_y = $s_height - $this->crop_height;
  253. break;
  254. case 8:
  255. $dst_x = ($s_width - $this->crop_width) / 2;
  256. $dst_y = $s_height - $this->crop_height;
  257. break;
  258. case 9:
  259. $dst_x = $s_width - $this->crop_width;
  260. $dst_y = $s_height - $this->crop_height;
  261. break;
  262. default:
  263. $dst_x = 0;
  264. $dst_y = 0;
  265. }
  266. if ($this->crop_width == $s_width) {
  267. $dst_x = 0;
  268. }
  269. if ($this->crop_height == $s_height) {
  270. $dst_y = 0;
  271. }
  272. return array(intval($dst_x), intval($dst_y));
  273. }
  274. }