image.class.php 6.7 KB

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