image.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. $image = $this->image();
  82. imagealphablending($image, false);
  83. imagesavealpha($image, true);
  84. if (is_null($real_quality)) {
  85. $saved = $func($image, $path);
  86. } else {
  87. if (!$this->isGif()) {
  88. $saved = $func($image, $path, $real_quality);
  89. }
  90. }
  91. $this->destroy();
  92. return $saved ? $path : $saved;
  93. }
  94. private function realQuality($quality = null) {
  95. if (is_null($quality)) {
  96. return null;
  97. }
  98. $quality = min($quality, 100);
  99. if ($this->isJPEG()) {
  100. return $quality * 0.75;
  101. }
  102. if ($this->isPng()) {
  103. return round(abs((100 - $quality) / 11.111111));
  104. }
  105. return null;
  106. }
  107. protected function handle() {
  108. if (!function_exists('gd_info')) {
  109. return false;
  110. }
  111. $this->image = $this->createResource();
  112. if (!$this->image) {
  113. return false;
  114. }
  115. $this->imageinfo = getimagesize($this->src);
  116. $actions = array_unique($this->actions);
  117. $src_image = $this->image;
  118. foreach ($actions as $action) {
  119. $method = 'do' . ucfirst($action);
  120. $src_image = $this->{$method}($src_image);
  121. }
  122. $this->image = $src_image;
  123. return true;
  124. }
  125. protected function doCrop($src_image) {
  126. list($dst_x, $dst_y) = $this->getCropDestPoint();
  127. if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
  128. $new_image = imagecrop($src_image, array('x' => $dst_x, 'y' => $dst_y, 'width' => $this->crop_width, 'height' => $this->crop_height));
  129. imagedestroy($src_image);
  130. } else {
  131. $new_image = $this->modify($src_image, $this->crop_width,
  132. $this->crop_height, $this->crop_width, $this->crop_height, 0, 0, $dst_x, $dst_y);
  133. }
  134. $this->imageinfo[0] = $this->crop_width;
  135. $this->imageinfo[1] = $this->crop_height;
  136. return $new_image;
  137. }
  138. protected function doResize($src_image) {
  139. $newimage = $this->modify($src_image, $this->resize_width, $this->resize_height,
  140. $this->imageinfo[0], $this->imageinfo[1]);
  141. $this->imageinfo[0] = $this->resize_width;
  142. $this->imageinfo[1] = $this->resize_height;
  143. return $newimage;
  144. }
  145. protected function modify($src_image, $width, $height, $src_width,
  146. $src_height, $dst_x = 0, $dst_y = 0, $src_x = 0, $src_y = 0) {
  147. $image = imagecreatetruecolor($width, $height);
  148. imagealphablending($image, false);
  149. imagesavealpha($image, true);
  150. imagecopyresampled($image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $width, $height,
  151. $src_width, $src_height);
  152. imagedestroy($src_image);
  153. return $image;
  154. }
  155. private function image() {
  156. return $this->image;
  157. }
  158. private function destroy() {
  159. if ($this->image) {
  160. imagedestroy($this->image);
  161. }
  162. }
  163. private function createResource() {
  164. if (file_exists($this->src) && !is_readable($this->src)) {
  165. return null;
  166. }
  167. if ($this->isPng()) {
  168. return imagecreatefrompng($this->src);
  169. }
  170. if ($this->isJPEG()) {
  171. return imagecreatefromjpeg($this->src);
  172. }
  173. if ($this->isGif()) {
  174. return imagecreatefromgif($this->src);
  175. }
  176. return null;
  177. }
  178. public function toBase64($prefix = 'data:image/%s;base64,') {
  179. $filename = tempnam('tmp', 'base64');
  180. $prefix = sprintf($prefix, $this->getExt());
  181. $result = $this->saveTo($filename);
  182. if (!$result) {
  183. return false;
  184. }
  185. $content = file_get_contents($filename);
  186. $base64 = base64_encode($content);
  187. unlink($filename);
  188. return $prefix . $base64;
  189. }
  190. private function getCropDestPoint() {
  191. $s_width = $this->imageinfo[0];
  192. $s_height = $this->imageinfo[1];
  193. $dst_x = $dst_y = 0;
  194. if ($this->crop_width == '0' || $this->crop_width > $s_width) {
  195. $this->crop_width = $s_width;
  196. }
  197. if ($this->crop_height == '0' || $this->crop_height > $s_height) {
  198. $this->crop_height = $s_height;
  199. }
  200. switch ($this->crop_position) {
  201. case 0:
  202. case 1:
  203. $dst_x = 0;
  204. $dst_y = 0;
  205. break;
  206. case 2:
  207. $dst_x = ($s_width - $this->crop_width) / 2;
  208. $dst_y = 0;
  209. break;
  210. case 3:
  211. $dst_x = $s_width - $this->crop_width;
  212. $dst_y = 0;
  213. break;
  214. case 4:
  215. $dst_x = 0;
  216. $dst_y = ($s_height - $this->crop_height) / 2;
  217. break;
  218. case 5:
  219. $dst_x = ($s_width - $this->crop_width) / 2;
  220. $dst_y = ($s_height - $this->crop_height) / 2;
  221. break;
  222. case 6:
  223. $dst_x = $s_width - $this->crop_width;
  224. $dst_y = ($s_height - $this->crop_height) / 2;
  225. break;
  226. case 7:
  227. $dst_x = 0;
  228. $dst_y = $s_height - $this->crop_height;
  229. break;
  230. case 8:
  231. $dst_x = ($s_width - $this->crop_width) / 2;
  232. $dst_y = $s_height - $this->crop_height;
  233. break;
  234. case 9:
  235. $dst_x = $s_width - $this->crop_width;
  236. $dst_y = $s_height - $this->crop_height;
  237. break;
  238. default:
  239. $dst_x = 0;
  240. $dst_y = 0;
  241. }
  242. if ($this->crop_width == $s_width) {
  243. $dst_x = 0;
  244. }
  245. if ($this->crop_height == $s_height) {
  246. $dst_y = 0;
  247. }
  248. return array(intval($dst_x), intval($dst_y));
  249. }
  250. }