GD.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <?php
  2. namespace Gregwar\Image\Adapter;
  3. use Gregwar\Image\ImageColor;
  4. use Gregwar\Image\Image;
  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 merge(Image $other, $x = 0, $y = 0, $width = null, $height = null)
  177. {
  178. $other = clone $other;
  179. $other->init();
  180. $other->applyOperations();
  181. imagealphablending($this->resource, true);
  182. if (null == $width) {
  183. $width = $other->width();
  184. }
  185. if (null == $height) {
  186. $height = $other->height();
  187. }
  188. imagecopyresampled($this->resource, $other->getAdapter()->getResource(), $x, $y, 0, 0, $width, $height, $width, $height);
  189. return $this;
  190. }
  191. /**
  192. * @inheritdoc
  193. */
  194. public function rotate($angle, $background = 0xffffff)
  195. {
  196. $this->resource = imagerotate($this->resource, $angle, ImageColor::gdAllocate($this->resource, $background));
  197. imagealphablending($this->resource, true);
  198. imagesavealpha($this->resource, true);
  199. return $this;
  200. }
  201. /**
  202. * @inheritdoc
  203. */
  204. public function fill($color = 0xffffff, $x = 0, $y = 0)
  205. {
  206. imagealphablending($this->resource, false);
  207. imagefill($this->resource, $x, $y, ImageColor::gdAllocate($this->resource, $color));
  208. return $this;
  209. }
  210. /**
  211. * @inheritdoc
  212. */
  213. public function write($font, $text, $x = 0, $y = 0, $size = 12, $angle = 0, $color = 0x000000, $align = 'left')
  214. {
  215. imagealphablending($this->resource, true);
  216. if ($align != 'left') {
  217. $sim_size = self::TTFBox($font, $text, $size, $angle);
  218. if ($align == 'center') {
  219. $x -= $sim_size['width'] / 2;
  220. }
  221. if ($align == 'right') {
  222. $x -= $sim_size['width'];
  223. }
  224. }
  225. imagettftext($this->resource, $size, $angle, $x, $y, ImageColor::gdAllocate($this->resource, $color), $font, $text);
  226. return $this;
  227. }
  228. /**
  229. * @inheritdoc
  230. */
  231. public function rectangle($x1, $y1, $x2, $y2, $color, $filled = false)
  232. {
  233. if ($filled) {
  234. imagefilledrectangle($this->resource, $x1, $y1, $x2, $y2, ImageColor::gdAllocate($this->resource, $color));
  235. } else {
  236. imagerectangle($this->resource, $x1, $y1, $x2, $y2, ImageColor::gdAllocate($this->resource, $color));
  237. }
  238. return $this;
  239. }
  240. /**
  241. * @inheritdoc
  242. */
  243. public function roundedRectangle($x1, $y1, $x2, $y2, $radius, $color, $filled = false) {
  244. if ($color) {
  245. $color = ImageColor::gdAllocate($this->resource, $color);
  246. }
  247. if ($filled == true) {
  248. imagefilledrectangle($this->resource, $x1+$radius, $y1, $x2-$radius, $y2, $color);
  249. imagefilledrectangle($this->resource, $x1, $y1+$radius, $x1+$radius-1, $y2-$radius, $color);
  250. imagefilledrectangle($this->resource, $x2-$radius+1, $y1+$radius, $x2, $y2-$radius, $color);
  251. imagefilledarc($this->resource,$x1+$radius, $y1+$radius, $radius*2, $radius*2, 180 , 270, $color, IMG_ARC_PIE);
  252. imagefilledarc($this->resource,$x2-$radius, $y1+$radius, $radius*2, $radius*2, 270 , 360, $color, IMG_ARC_PIE);
  253. imagefilledarc($this->resource,$x1+$radius, $y2-$radius, $radius*2, $radius*2, 90 , 180, $color, IMG_ARC_PIE);
  254. imagefilledarc($this->resource,$x2-$radius, $y2-$radius, $radius*2, $radius*2, 360 , 90, $color, IMG_ARC_PIE);
  255. } else {
  256. imageline($this->resource, $x1+$radius, $y1, $x2-$radius, $y1, $color);
  257. imageline($this->resource, $x1+$radius, $y2, $x2-$radius, $y2, $color);
  258. imageline($this->resource, $x1, $y1+$radius, $x1, $y2-$radius, $color);
  259. imageline($this->resource, $x2, $y1+$radius, $x2, $y2-$radius, $color);
  260. imagearc($this->resource,$x1+$radius, $y1+$radius, $radius*2, $radius*2, 180 , 270, $color);
  261. imagearc($this->resource,$x2-$radius, $y1+$radius, $radius*2, $radius*2, 270 , 360, $color);
  262. imagearc($this->resource,$x1+$radius, $y2-$radius, $radius*2, $radius*2, 90 , 180, $color);
  263. imagearc($this->resource,$x2-$radius, $y2-$radius, $radius*2, $radius*2, 360 , 90, $color);
  264. }
  265. return $this;
  266. }
  267. /**
  268. * @inheritdoc
  269. */
  270. public function line($x1, $y1, $x2, $y2, $color = 0x000000)
  271. {
  272. imageline($this->resource, $x1, $y1, $x2, $y2, ImageColor::gdAllocate($this->resource, $color));
  273. return $this;
  274. }
  275. /**
  276. * @inheritdoc
  277. */
  278. public function ellipse($cx, $cy, $width, $height, $color = 0x000000, $filled = false)
  279. {
  280. if ($filled) {
  281. imagefilledellipse($this->resource, $cx, $cy, $width, $height, ImageColor::gdAllocate($this->resource, $color));
  282. } else {
  283. imageellipse($this->resource, $cx, $cy, $width, $height, ImageColor::gdAllocate($this->resource, $color));
  284. }
  285. return $this;
  286. }
  287. /**
  288. * @inheritdoc
  289. */
  290. public function circle($cx, $cy, $r, $color = 0x000000, $filled = false)
  291. {
  292. return $this->ellipse($cx, $cy, $r, $r, ImageColor::gdAllocate($this->resource, $color), $filled);
  293. }
  294. /**
  295. * @inheritdoc
  296. */
  297. public function polygon(array $points, $color, $filled = false)
  298. {
  299. if ($filled) {
  300. imagefilledpolygon($this->resource, $points, count($points)/2, ImageColor::gdAllocate($this->resource, $color));
  301. } else {
  302. imagepolygon($this->resource, $points, count($points)/2, ImageColor::gdAllocate($this->resource, $color));
  303. }
  304. return $this;
  305. }
  306. /**
  307. * @inheritdoc
  308. */
  309. public function flip($flipVertical, $flipHorizontal) {
  310. if (!$flipVertical && !$flipHorizontal) {
  311. return $this;
  312. }
  313. if (function_exists('imageflip')) {
  314. if ($flipVertical && $flipHorizontal) {
  315. $flipMode = \IMG_FLIP_BOTH;
  316. } else if ($flipVertical && !$flipHorizontal) {
  317. $flipMode = \IMG_FLIP_VERTICAL;
  318. } else if (!$flipVertical && $flipHorizontal) {
  319. $flipMode = \IMG_FLIP_HORIZONTAL;
  320. }
  321. imageflip($this->resource, $flipMode);
  322. } else {
  323. $width = $this->width();
  324. $height = $this->height();
  325. $src_x = 0;
  326. $src_y = 0;
  327. $src_width = $width;
  328. $src_height = $height;
  329. if ($flipVertical) {
  330. $src_y = $height -1;
  331. $src_height = -$height;
  332. }
  333. if ($flipHorizontal) {
  334. $src_x = $width -1;
  335. $src_width = -$width;
  336. }
  337. $imgdest = imagecreatetruecolor ($width, $height);
  338. imagealphablending($imgdest, false);
  339. imagesavealpha($imgdest, true);
  340. if (imagecopyresampled($imgdest, $this->resource, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height)) {
  341. imagedestroy($this->resource);
  342. $this->resource = $imgdest;
  343. }
  344. }
  345. return $this;
  346. }
  347. /**
  348. * @inheritdoc
  349. */
  350. public function width()
  351. {
  352. if (null === $this->resource) {
  353. $this->init();
  354. }
  355. return imagesx($this->resource);
  356. }
  357. /**
  358. * @inheritdoc
  359. */
  360. public function height()
  361. {
  362. if (null === $this->resource) {
  363. $this->init();
  364. }
  365. return imagesy($this->resource);
  366. }
  367. protected function createImage($width, $height)
  368. {
  369. $this->resource = imagecreatetruecolor($width, $height);
  370. }
  371. protected function createImageFromData($data)
  372. {
  373. $this->resource = @imagecreatefromstring($data);
  374. }
  375. /**
  376. * Converts the image to true color
  377. */
  378. protected function convertToTrueColor()
  379. {
  380. if (!imageistruecolor($this->resource)) {
  381. if (function_exists('imagepalettetotruecolor')) {
  382. // Available in PHP 5.5
  383. imagepalettetotruecolor($this->resource);
  384. } else {
  385. $transparentIndex = imagecolortransparent($this->resource);
  386. $w = $this->width();
  387. $h = $this->height();
  388. $img = imagecreatetruecolor($w, $h);
  389. imagecopy($img, $this->resource, 0, 0, 0, 0, $w, $h);
  390. if ($transparentIndex != -1) {
  391. $width = $this->width();
  392. $height = $this->height();
  393. imagealphablending($img, false);
  394. imagesavealpha($img, true);
  395. for ($x=0; $x<$width; $x++) {
  396. for ($y=0; $y<$height; $y++) {
  397. if (imagecolorat($this->resource, $x, $y) == $transparentIndex) {
  398. imagesetpixel($img, $x, $y, 127 << 24);
  399. }
  400. }
  401. }
  402. }
  403. $this->resource = $img;
  404. }
  405. }
  406. imagesavealpha($this->resource, true);
  407. }
  408. /**
  409. * @inheritdoc
  410. */
  411. public function saveGif($file)
  412. {
  413. $transColor = imagecolorallocatealpha($this->resource, 255, 255, 255, 127);
  414. imagecolortransparent($this->resource, $transColor);
  415. imagegif($this->resource, $file);
  416. return $this;
  417. }
  418. /**
  419. * @inheritdoc
  420. */
  421. public function savePng($file)
  422. {
  423. imagepng($this->resource, $file);
  424. return $this;
  425. }
  426. /**
  427. * @inheritdoc
  428. */
  429. public function saveJpeg($file, $quality)
  430. {
  431. imagejpeg($this->resource, $file, $quality);
  432. return $this;
  433. }
  434. /**
  435. * Try to open the file using jpeg
  436. *
  437. */
  438. protected function openJpeg($file)
  439. {
  440. $this->resource = @imagecreatefromjpeg($file);
  441. }
  442. /**
  443. * Try to open the file using gif
  444. */
  445. protected function openGif($file)
  446. {
  447. $this->resource = @imagecreatefromgif($file);
  448. }
  449. /**
  450. * Try to open the file using PNG
  451. */
  452. protected function openPng($file)
  453. {
  454. $this->resource = @imagecreatefrompng($file);
  455. }
  456. /**
  457. * Does this adapter supports type ?
  458. */
  459. protected function supports($type)
  460. {
  461. return (imagetypes() & self::$gdTypes[$type]);
  462. }
  463. protected function getColor($x, $y)
  464. {
  465. return imagecolorat($this->resource, $x, $y);
  466. }
  467. /**
  468. * @inheritdoc
  469. */
  470. public function enableProgressive(){
  471. imageinterlace($this->resource, 1);
  472. return $this;
  473. }
  474. }