GD.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. <?php
  2. namespace Gregwar\Image\Adapter;
  3. use Gregwar\Image\Image;
  4. use Gregwar\Image\ImageColor;
  5. class GD extends Common
  6. {
  7. public static $gdTypes = array(
  8. 'jpeg' => \IMG_JPG,
  9. 'gif' => \IMG_GIF,
  10. 'png' => \IMG_PNG,
  11. );
  12. protected function loadResource($resource)
  13. {
  14. parent::loadResource($resource);
  15. imagesavealpha($this->resource, true);
  16. }
  17. /**
  18. * Gets the width and the height for writing some text.
  19. */
  20. public static function TTFBox($font, $text, $size, $angle = 0)
  21. {
  22. $box = imagettfbbox($size, $angle, $font, $text);
  23. return array(
  24. 'width' => abs($box[2] - $box[0]),
  25. 'height' => abs($box[3] - $box[5]),
  26. );
  27. }
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. if (!(extension_loaded('gd') && function_exists('gd_info'))) {
  32. throw new \RuntimeException('You need to install GD PHP Extension to use this library');
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getName()
  39. {
  40. return 'GD';
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function fillBackground($background = 0xffffff)
  46. {
  47. $w = $this->width();
  48. $h = $this->height();
  49. $n = imagecreatetruecolor($w, $h);
  50. imagefill($n, 0, 0, ImageColor::gdAllocate($this->resource, $background));
  51. imagecopyresampled($n, $this->resource, 0, 0, 0, 0, $w, $h, $w, $h);
  52. imagedestroy($this->resource);
  53. $this->resource = $n;
  54. return $this;
  55. }
  56. /**
  57. * Do the image resize.
  58. *
  59. * @return $this
  60. */
  61. protected function doResize($bg, $target_width, $target_height, $new_width, $new_height)
  62. {
  63. $width = $this->width();
  64. $height = $this->height();
  65. $n = imagecreatetruecolor($target_width, $target_height);
  66. if ($bg != 'transparent') {
  67. imagefill($n, 0, 0, ImageColor::gdAllocate($this->resource, $bg));
  68. } else {
  69. imagealphablending($n, false);
  70. $color = ImageColor::gdAllocate($this->resource, 'transparent');
  71. imagefill($n, 0, 0, $color);
  72. imagesavealpha($n, true);
  73. }
  74. imagecopyresampled($n, $this->resource, ($target_width - $new_width) / 2, ($target_height - $new_height) / 2, 0, 0, $new_width, $new_height, $width, $height);
  75. imagedestroy($this->resource);
  76. $this->resource = $n;
  77. return $this;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function crop($x, $y, $width, $height)
  83. {
  84. $destination = imagecreatetruecolor($width, $height);
  85. imagealphablending($destination, false);
  86. imagesavealpha($destination, true);
  87. imagecopy($destination, $this->resource, 0, 0, $x, $y, $this->width(), $this->height());
  88. imagedestroy($this->resource);
  89. $this->resource = $destination;
  90. return $this;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function negate()
  96. {
  97. imagefilter($this->resource, IMG_FILTER_NEGATE);
  98. return $this;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function brightness($brightness)
  104. {
  105. imagefilter($this->resource, IMG_FILTER_BRIGHTNESS, $brightness);
  106. return $this;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function contrast($contrast)
  112. {
  113. imagefilter($this->resource, IMG_FILTER_CONTRAST, $contrast);
  114. return $this;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function grayscale()
  120. {
  121. imagefilter($this->resource, IMG_FILTER_GRAYSCALE);
  122. return $this;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function emboss()
  128. {
  129. imagefilter($this->resource, IMG_FILTER_EMBOSS);
  130. return $this;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function smooth($p)
  136. {
  137. imagefilter($this->resource, IMG_FILTER_SMOOTH, $p);
  138. return $this;
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function sharp()
  144. {
  145. imagefilter($this->resource, IMG_FILTER_MEAN_REMOVAL);
  146. return $this;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function edge()
  152. {
  153. imagefilter($this->resource, IMG_FILTER_EDGEDETECT);
  154. return $this;
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function colorize($red, $green, $blue)
  160. {
  161. imagefilter($this->resource, IMG_FILTER_COLORIZE, $red, $green, $blue);
  162. return $this;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function sepia()
  168. {
  169. imagefilter($this->resource, IMG_FILTER_GRAYSCALE);
  170. imagefilter($this->resource, IMG_FILTER_COLORIZE, 100, 50, 0);
  171. return $this;
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function gaussianBlur($blurFactor = 1)
  177. {
  178. $blurFactor = round($blurFactor); // blurFactor has to be an integer
  179. $originalWidth = $this->width();
  180. $originalHeight = $this->height();
  181. $smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor));
  182. $smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor));
  183. // for the first run, the previous image is the original input
  184. $prevImage = $this->resource;
  185. $prevWidth = $originalWidth;
  186. $prevHeight = $originalHeight;
  187. // scale way down and gradually scale back up, blurring all the way
  188. for ($i = 0; $i < $blurFactor; ++$i) {
  189. // determine dimensions of next image
  190. $nextWidth = $smallestWidth * pow(2, $i);
  191. $nextHeight = $smallestHeight * pow(2, $i);
  192. // resize previous image to next size
  193. $nextImage = imagecreatetruecolor($nextWidth, $nextHeight);
  194. imagecopyresized($nextImage, $prevImage, 0, 0, 0, 0,
  195. $nextWidth, $nextHeight, $prevWidth, $prevHeight);
  196. // apply blur filter
  197. imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);
  198. // now the new image becomes the previous image for the next step
  199. $prevImage = $nextImage;
  200. $prevWidth = $nextWidth;
  201. $prevHeight = $nextHeight;
  202. }
  203. // scale back to original size and blur one more time
  204. imagecopyresized($this->resource, $nextImage,
  205. 0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight);
  206. imagefilter($this->resource, IMG_FILTER_GAUSSIAN_BLUR);
  207. // clean up
  208. imagedestroy($prevImage);
  209. return $this;
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function merge(Image $other, $x = 0, $y = 0, $width = null, $height = null)
  215. {
  216. $other = clone $other;
  217. $other->init();
  218. $other->applyOperations();
  219. imagealphablending($this->resource, true);
  220. if (null == $width) {
  221. $width = $other->width();
  222. }
  223. if (null == $height) {
  224. $height = $other->height();
  225. }
  226. imagecopyresampled($this->resource, $other->getAdapter()->getResource(), $x, $y, 0, 0, $width, $height, $width, $height);
  227. return $this;
  228. }
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function rotate($angle, $background = 0xffffff)
  233. {
  234. $this->resource = imagerotate($this->resource, $angle, ImageColor::gdAllocate($this->resource, $background));
  235. imagealphablending($this->resource, true);
  236. imagesavealpha($this->resource, true);
  237. return $this;
  238. }
  239. /**
  240. * {@inheritdoc}
  241. */
  242. public function fill($color = 0xffffff, $x = 0, $y = 0)
  243. {
  244. imagealphablending($this->resource, false);
  245. imagefill($this->resource, $x, $y, ImageColor::gdAllocate($this->resource, $color));
  246. return $this;
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. public function write($font, $text, $x = 0, $y = 0, $size = 12, $angle = 0, $color = 0x000000, $align = 'left')
  252. {
  253. imagealphablending($this->resource, true);
  254. if ($align != 'left') {
  255. $sim_size = self::TTFBox($font, $text, $size, $angle);
  256. if ($align == 'center') {
  257. $x -= $sim_size['width'] / 2;
  258. }
  259. if ($align == 'right') {
  260. $x -= $sim_size['width'];
  261. }
  262. }
  263. imagettftext($this->resource, $size, $angle, $x, $y, ImageColor::gdAllocate($this->resource, $color), $font, $text);
  264. return $this;
  265. }
  266. /**
  267. * {@inheritdoc}
  268. */
  269. public function rectangle($x1, $y1, $x2, $y2, $color, $filled = false)
  270. {
  271. if ($filled) {
  272. imagefilledrectangle($this->resource, $x1, $y1, $x2, $y2, ImageColor::gdAllocate($this->resource, $color));
  273. } else {
  274. imagerectangle($this->resource, $x1, $y1, $x2, $y2, ImageColor::gdAllocate($this->resource, $color));
  275. }
  276. return $this;
  277. }
  278. /**
  279. * {@inheritdoc}
  280. */
  281. public function roundedRectangle($x1, $y1, $x2, $y2, $radius, $color, $filled = false)
  282. {
  283. if ($color) {
  284. $color = ImageColor::gdAllocate($this->resource, $color);
  285. }
  286. if ($filled == true) {
  287. imagefilledrectangle($this->resource, $x1 + $radius, $y1, $x2 - $radius, $y2, $color);
  288. imagefilledrectangle($this->resource, $x1, $y1 + $radius, $x1 + $radius - 1, $y2 - $radius, $color);
  289. imagefilledrectangle($this->resource, $x2 - $radius + 1, $y1 + $radius, $x2, $y2 - $radius, $color);
  290. imagefilledarc($this->resource, $x1 + $radius, $y1 + $radius, $radius * 2, $radius * 2, 180, 270, $color, IMG_ARC_PIE);
  291. imagefilledarc($this->resource, $x2 - $radius, $y1 + $radius, $radius * 2, $radius * 2, 270, 360, $color, IMG_ARC_PIE);
  292. imagefilledarc($this->resource, $x1 + $radius, $y2 - $radius, $radius * 2, $radius * 2, 90, 180, $color, IMG_ARC_PIE);
  293. imagefilledarc($this->resource, $x2 - $radius, $y2 - $radius, $radius * 2, $radius * 2, 360, 90, $color, IMG_ARC_PIE);
  294. } else {
  295. imageline($this->resource, $x1 + $radius, $y1, $x2 - $radius, $y1, $color);
  296. imageline($this->resource, $x1 + $radius, $y2, $x2 - $radius, $y2, $color);
  297. imageline($this->resource, $x1, $y1 + $radius, $x1, $y2 - $radius, $color);
  298. imageline($this->resource, $x2, $y1 + $radius, $x2, $y2 - $radius, $color);
  299. imagearc($this->resource, $x1 + $radius, $y1 + $radius, $radius * 2, $radius * 2, 180, 270, $color);
  300. imagearc($this->resource, $x2 - $radius, $y1 + $radius, $radius * 2, $radius * 2, 270, 360, $color);
  301. imagearc($this->resource, $x1 + $radius, $y2 - $radius, $radius * 2, $radius * 2, 90, 180, $color);
  302. imagearc($this->resource, $x2 - $radius, $y2 - $radius, $radius * 2, $radius * 2, 360, 90, $color);
  303. }
  304. return $this;
  305. }
  306. /**
  307. * {@inheritdoc}
  308. */
  309. public function line($x1, $y1, $x2, $y2, $color = 0x000000)
  310. {
  311. imageline($this->resource, $x1, $y1, $x2, $y2, ImageColor::gdAllocate($this->resource, $color));
  312. return $this;
  313. }
  314. /**
  315. * {@inheritdoc}
  316. */
  317. public function ellipse($cx, $cy, $width, $height, $color = 0x000000, $filled = false)
  318. {
  319. if ($filled) {
  320. imagefilledellipse($this->resource, $cx, $cy, $width, $height, ImageColor::gdAllocate($this->resource, $color));
  321. } else {
  322. imageellipse($this->resource, $cx, $cy, $width, $height, ImageColor::gdAllocate($this->resource, $color));
  323. }
  324. return $this;
  325. }
  326. /**
  327. * {@inheritdoc}
  328. */
  329. public function circle($cx, $cy, $r, $color = 0x000000, $filled = false)
  330. {
  331. return $this->ellipse($cx, $cy, $r, $r, ImageColor::gdAllocate($this->resource, $color), $filled);
  332. }
  333. /**
  334. * {@inheritdoc}
  335. */
  336. public function polygon(array $points, $color, $filled = false)
  337. {
  338. if ($filled) {
  339. imagefilledpolygon($this->resource, $points, count($points) / 2, ImageColor::gdAllocate($this->resource, $color));
  340. } else {
  341. imagepolygon($this->resource, $points, count($points) / 2, ImageColor::gdAllocate($this->resource, $color));
  342. }
  343. return $this;
  344. }
  345. /**
  346. * {@inheritdoc}
  347. */
  348. public function flip($flipVertical, $flipHorizontal)
  349. {
  350. if (!$flipVertical && !$flipHorizontal) {
  351. return $this;
  352. }
  353. if (function_exists('imageflip')) {
  354. if ($flipVertical && $flipHorizontal) {
  355. $flipMode = \IMG_FLIP_BOTH;
  356. } elseif ($flipVertical && !$flipHorizontal) {
  357. $flipMode = \IMG_FLIP_VERTICAL;
  358. } elseif (!$flipVertical && $flipHorizontal) {
  359. $flipMode = \IMG_FLIP_HORIZONTAL;
  360. }
  361. imageflip($this->resource, $flipMode);
  362. } else {
  363. $width = $this->width();
  364. $height = $this->height();
  365. $src_x = 0;
  366. $src_y = 0;
  367. $src_width = $width;
  368. $src_height = $height;
  369. if ($flipVertical) {
  370. $src_y = $height - 1;
  371. $src_height = -$height;
  372. }
  373. if ($flipHorizontal) {
  374. $src_x = $width - 1;
  375. $src_width = -$width;
  376. }
  377. $imgdest = imagecreatetruecolor($width, $height);
  378. imagealphablending($imgdest, false);
  379. imagesavealpha($imgdest, true);
  380. if (imagecopyresampled($imgdest, $this->resource, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height)) {
  381. imagedestroy($this->resource);
  382. $this->resource = $imgdest;
  383. }
  384. }
  385. return $this;
  386. }
  387. /**
  388. * {@inheritdoc}
  389. */
  390. public function width()
  391. {
  392. if (null === $this->resource) {
  393. $this->init();
  394. }
  395. return imagesx($this->resource);
  396. }
  397. /**
  398. * {@inheritdoc}
  399. */
  400. public function height()
  401. {
  402. if (null === $this->resource) {
  403. $this->init();
  404. }
  405. return imagesy($this->resource);
  406. }
  407. protected function createImage($width, $height)
  408. {
  409. $this->resource = imagecreatetruecolor($width, $height);
  410. }
  411. protected function createImageFromData($data)
  412. {
  413. $this->resource = @imagecreatefromstring($data);
  414. }
  415. /**
  416. * Converts the image to true color.
  417. */
  418. protected function convertToTrueColor()
  419. {
  420. if (!imageistruecolor($this->resource)) {
  421. if (function_exists('imagepalettetotruecolor')) {
  422. // Available in PHP 5.5
  423. imagepalettetotruecolor($this->resource);
  424. } else {
  425. $transparentIndex = imagecolortransparent($this->resource);
  426. $w = $this->width();
  427. $h = $this->height();
  428. $img = imagecreatetruecolor($w, $h);
  429. imagecopy($img, $this->resource, 0, 0, 0, 0, $w, $h);
  430. if ($transparentIndex != -1) {
  431. $width = $this->width();
  432. $height = $this->height();
  433. imagealphablending($img, false);
  434. imagesavealpha($img, true);
  435. for ($x = 0; $x < $width; ++$x) {
  436. for ($y = 0; $y < $height; ++$y) {
  437. if (imagecolorat($this->resource, $x, $y) == $transparentIndex) {
  438. imagesetpixel($img, $x, $y, 127 << 24);
  439. }
  440. }
  441. }
  442. }
  443. $this->resource = $img;
  444. }
  445. }
  446. imagesavealpha($this->resource, true);
  447. }
  448. /**
  449. * {@inheritdoc}
  450. */
  451. public function saveGif($file)
  452. {
  453. $transColor = imagecolorallocatealpha($this->resource, 255, 255, 255, 127);
  454. imagecolortransparent($this->resource, $transColor);
  455. imagegif($this->resource, $file);
  456. return $this;
  457. }
  458. /**
  459. * {@inheritdoc}
  460. */
  461. public function savePng($file)
  462. {
  463. imagepng($this->resource, $file);
  464. return $this;
  465. }
  466. /**
  467. * {@inheritdoc}
  468. */
  469. public function saveJpeg($file, $quality)
  470. {
  471. imagejpeg($this->resource, $file, $quality);
  472. return $this;
  473. }
  474. /**
  475. * Try to open the file using jpeg.
  476. */
  477. protected function openJpeg($file)
  478. {
  479. $this->resource = @imagecreatefromjpeg($file);
  480. }
  481. /**
  482. * Try to open the file using gif.
  483. */
  484. protected function openGif($file)
  485. {
  486. $this->resource = @imagecreatefromgif($file);
  487. }
  488. /**
  489. * Try to open the file using PNG.
  490. */
  491. protected function openPng($file)
  492. {
  493. $this->resource = @imagecreatefrompng($file);
  494. }
  495. /**
  496. * Does this adapter supports type ?
  497. */
  498. protected function supports($type)
  499. {
  500. return imagetypes() & self::$gdTypes[$type];
  501. }
  502. protected function getColor($x, $y)
  503. {
  504. return imagecolorat($this->resource, $x, $y);
  505. }
  506. /**
  507. * {@inheritdoc}
  508. */
  509. public function enableProgressive()
  510. {
  511. imageinterlace($this->resource, 1);
  512. return $this;
  513. }
  514. }