image.class.php 6.7 KB

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