Guest User

Untitled

a guest
Apr 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. <?php
  2. class ImageManipulator
  3. {
  4. /**
  5. * @var int
  6. */
  7. protected $width;
  8.  
  9. /**
  10. * @var int
  11. */
  12. protected $height;
  13.  
  14. /**
  15. * @var resource
  16. */
  17. protected $image;
  18.  
  19. /**
  20. * Image manipulator constructor
  21. *
  22. * @param string $file OPTIONAL Path to image file or image data as string
  23. * @return void
  24. */
  25. public function __construct($file = null)
  26. {
  27. if (null !== $file) {
  28. if (is_file($file)) {
  29. $this->setImageFile($file);
  30. } else {
  31. $this->setImageString($file);
  32. }
  33. }
  34. }
  35.  
  36. /**
  37. * Set image resource from file
  38. *
  39. * @param string $file Path to image file
  40. * @return ImageManipulator for a fluent interface
  41. * @throws InvalidArgumentException
  42. */
  43. public function setImageFile($file)
  44. {
  45. if (!(is_readable($file) && is_file($file))) {
  46. throw new InvalidArgumentException("Image file $file is not readable");
  47. }
  48.  
  49. if (is_resource($this->image)) {
  50. imagedestroy($this->image);
  51. }
  52.  
  53. list ($this->width, $this->height, $type) = getimagesize($file);
  54.  
  55. switch ($type) {
  56. case IMAGETYPE_GIF :
  57. $this->image = imagecreatefromgif($file);
  58. break;
  59. case IMAGETYPE_JPEG :
  60. $this->image = imagecreatefromjpeg($file);
  61. break;
  62. case IMAGETYPE_PNG :
  63. $this->image = imagecreatefrompng($file);
  64. break;
  65. default :
  66. throw new InvalidArgumentException("Image type $type not supported");
  67. }
  68.  
  69. return $this;
  70. }
  71.  
  72. /**
  73. * Set image resource from string data
  74. *
  75. * @param string $data
  76. * @return ImageManipulator for a fluent interface
  77. * @throws RuntimeException
  78. */
  79. public function setImageString($data)
  80. {
  81. if (is_resource($this->image)) {
  82. imagedestroy($this->image);
  83. }
  84.  
  85. if (!$this->image = imagecreatefromstring($data)) {
  86. throw new RuntimeException('Cannot create image from data string');
  87. }
  88. $this->width = imagesx($this->image);
  89. $this->height = imagesy($this->image);
  90. return $this;
  91. }
  92.  
  93. /**
  94. * Resamples the current image
  95. *
  96. * @param int $width New width
  97. * @param int $height New height
  98. * @param bool $constrainProportions Constrain current image proportions when resizing
  99. * @return ImageManipulator for a fluent interface
  100. * @throws RuntimeException
  101. */
  102. public function resample($width, $height, $constrainProportions = true)
  103. {
  104. if (!is_resource($this->image)) {
  105. throw new RuntimeException('No image set');
  106. }
  107. if ($constrainProportions) {
  108. if ($this->height >= $this->width) {
  109. $width = round($height / $this->height * $this->width);
  110. } else {
  111. $height = round($width / $this->width * $this->height);
  112. }
  113. }
  114. $temp = imagecreatetruecolor($width, $height);
  115. imagecopyresampled($temp, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
  116. return $this->_replace($temp);
  117. }
  118.  
  119. /**
  120. * Enlarge canvas
  121. *
  122. * @param int $width Canvas width
  123. * @param int $height Canvas height
  124. * @param array $rgb RGB colour values
  125. * @param int $xpos X-Position of image in new canvas, null for centre
  126. * @param int $ypos Y-Position of image in new canvas, null for centre
  127. * @return ImageManipulator for a fluent interface
  128. * @throws RuntimeException
  129. */
  130. public function enlargeCanvas($width, $height, array $rgb = array(), $xpos = null, $ypos = null)
  131. {
  132. if (!is_resource($this->image)) {
  133. throw new RuntimeException('No image set');
  134. }
  135.  
  136. $width = max($width, $this->width);
  137. $height = max($height, $this->height);
  138.  
  139. $temp = imagecreatetruecolor($width, $height);
  140. if (count($rgb) == 3) {
  141. $bg = imagecolorallocate($temp, $rgb[0], $rgb[1], $rgb[2]);
  142. imagefill($temp, 0, 0, $bg);
  143. }
  144.  
  145. if (null === $xpos) {
  146. $xpos = round(($width - $this->width) / 2);
  147. }
  148. if (null === $ypos) {
  149. $ypos = round(($height - $this->height) / 2);
  150. }
  151.  
  152. imagecopy($temp, $this->image, (int) $xpos, (int) $ypos, 0, 0, $this->width, $this->height);
  153. return $this->_replace($temp);
  154. }
  155.  
  156. /**
  157. * Crop image
  158. *
  159. * @param int|array $x1 Top left x-coordinate of crop box or array of coordinates
  160. * @param int $y1 Top left y-coordinate of crop box
  161. * @param int $x2 Bottom right x-coordinate of crop box
  162. * @param int $y2 Bottom right y-coordinate of crop box
  163. * @return ImageManipulator for a fluent interface
  164. * @throws RuntimeException
  165. */
  166. public function crop($x1, $y1 = 0, $x2 = 0, $y2 = 0)
  167. {
  168. if (!is_resource($this->image)) {
  169. throw new RuntimeException('No image set');
  170. }
  171. if (is_array($x1) && 4 == count($x1)) {
  172. list($x1, $y1, $x2, $y2) = $x1;
  173. }
  174.  
  175. $x1 = max($x1, 0);
  176. $y1 = max($y1, 0);
  177.  
  178. $x2 = min($x2, $this->width);
  179. $y2 = min($y2, $this->height);
  180.  
  181. $width = $x2 - $x1;
  182. $height = $y2 - $y1;
  183.  
  184. $temp = imagecreatetruecolor($width, $height);
  185. imagecopy($temp, $this->image, 0, 0, $x1, $y1, $width, $height);
  186.  
  187. return $this->_replace($temp);
  188. }
  189.  
  190. /**
  191. * Replace current image resource with a new one
  192. *
  193. * @param resource $res New image resource
  194. * @return ImageManipulator for a fluent interface
  195. * @throws UnexpectedValueException
  196. */
  197. protected function _replace($res)
  198. {
  199. if (!is_resource($res)) {
  200. throw new UnexpectedValueException('Invalid resource');
  201. }
  202. if (is_resource($this->image)) {
  203. imagedestroy($this->image);
  204. }
  205. $this->image = $res;
  206. $this->width = imagesx($res);
  207. $this->height = imagesy($res);
  208. return $this;
  209. }
  210.  
  211. /**
  212. * Save current image to file
  213. *
  214. * @param string $fileName
  215. * @return void
  216. * @throws RuntimeException
  217. */
  218. public function save($fileName, $type = IMAGETYPE_JPEG)
  219. {
  220. $dir = dirname($fileName);
  221. if (!is_dir($dir)) {
  222. if (!mkdir($dir, 0755, true)) {
  223. throw new RuntimeException('Error creating directory ' . $dir);
  224. }
  225. }
  226.  
  227. try {
  228. switch ($type) {
  229. case IMAGETYPE_GIF :
  230. if (!imagegif($this->image, $fileName)) {
  231. throw new RuntimeException;
  232. }
  233. break;
  234. case IMAGETYPE_PNG :
  235. if (!imagepng($this->image, $fileName)) {
  236. throw new RuntimeException;
  237. }
  238. break;
  239. case IMAGETYPE_JPEG :
  240. default :
  241. if (!imagejpeg($this->image, $fileName, 95)) {
  242. throw new RuntimeException;
  243. }
  244. }
  245. } catch (Exception $ex) {
  246. throw new RuntimeException('Error saving image file to ' . $fileName);
  247. }
  248. }
  249.  
  250. /**
  251. * Returns the GD image resource
  252. *
  253. * @return resource
  254. */
  255. public function getResource()
  256. {
  257. return $this->image;
  258. }
  259.  
  260. /**
  261. * Get current image resource width
  262. *
  263. * @return int
  264. */
  265. public function getWidth()
  266. {
  267. return $this->width;
  268. }
  269.  
  270. /**
  271. * Get current image height
  272. *
  273. * @return int
  274. */
  275. public function getHeight()
  276. {
  277. return $this->height;
  278. }
  279. }
Add Comment
Please, Sign In to add comment