api.pixelcraft.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. <?php
  2. /**
  3. * PixelCraft is a lightweight PHP library designed for easy image processing using the GD lib.
  4. * With PixelCraft, you can perform basic image operations such as resizing, cropping, drawing of watermarks, and format conversion.
  5. * The library is characterized by its user-friendly interface and minimal code footprint,
  6. * allowing for quick and efficient image processing in PHP projects.
  7. *
  8. * @package PixelCraft
  9. * @author Rostyslav Haitkulov <info@ubilling.net.ua>
  10. * @see https://github.com/nightflyza/PixelCraft
  11. * @license MIT
  12. */
  13. class PixelCraft {
  14. /**
  15. * Contains image copy to perform some magic on it
  16. *
  17. * @var GDimage
  18. */
  19. protected $image = '';
  20. /**
  21. * Contains image rendering quality
  22. *
  23. * @var int
  24. */
  25. protected $quality = -1;
  26. /**
  27. * Contains loaded image mime type
  28. *
  29. * @var string
  30. */
  31. protected $imageType = '';
  32. /**
  33. * Contains loaded image original width
  34. *
  35. * @var int
  36. */
  37. protected $imageWidth = 0;
  38. /**
  39. * Contains loaded image original height
  40. *
  41. * @var int
  42. */
  43. protected $imageHeight = 0;
  44. /**
  45. * Contains array of custom RGB colors palette as name=>color
  46. *
  47. * @var array
  48. */
  49. protected $colorPalette = array();
  50. /**
  51. * Contains already allocated image colors as name=>colorInt
  52. *
  53. * @var array
  54. */
  55. protected $colorsAllocated = array();
  56. /**
  57. * TTF font path
  58. *
  59. * @var string
  60. */
  61. protected $font = 'skins/OpenSans-Regular.ttf';
  62. /**
  63. * Font size in pt.
  64. *
  65. * @var int
  66. */
  67. protected $fontSize = 10;
  68. /**
  69. * Drawing line width in px
  70. *
  71. * @var int
  72. */
  73. protected $lineWidth = 1;
  74. /**
  75. * Contains watermark image copy
  76. *
  77. * @var GDimage
  78. */
  79. protected $watermark = '';
  80. /**
  81. * Schweigen im wald
  82. */
  83. public function __construct() {
  84. $this->setDefaultColors();
  85. }
  86. /**
  87. * Sets few default colors to palette
  88. *
  89. * @return void
  90. */
  91. protected function setDefaultColors() {
  92. $this->addColor('white', 255, 255, 255);
  93. $this->addColor('black', 0, 0, 0);
  94. $this->addColor('red', 255, 0, 0);
  95. $this->addColor('green', 0, 255, 0);
  96. $this->addColor('blue', 0, 0, 255);
  97. $this->addColor('yellow', 255, 255, 0);
  98. $this->addColor('grey', 85, 85, 85);
  99. }
  100. /**
  101. * Sets current instance image save/render quality
  102. *
  103. * @param int $quality Deafult -1 means IJG quality value for jpeg or default zlib for png
  104. *
  105. * @return void
  106. */
  107. public function setQuality($quality) {
  108. if (is_int($quality)) {
  109. $this->quality = $quality;
  110. }
  111. }
  112. /**
  113. * Set TTF font path
  114. *
  115. * @param string $font TTF font path
  116. *
  117. * @return void
  118. */
  119. public function setFont($font) {
  120. $this->font = $font;
  121. }
  122. /**
  123. * Set font size in pt.
  124. *
  125. * @param int $fontSize Font size in pt.
  126. *
  127. * @return void
  128. */
  129. public function setFontSize($fontSize) {
  130. $this->fontSize = $fontSize;
  131. }
  132. /**
  133. * Returns current image width
  134. *
  135. * @return int
  136. */
  137. public function getImageWidth() {
  138. return ($this->imageWidth);
  139. }
  140. /**
  141. * Returns current image height
  142. *
  143. * @return int
  144. */
  145. public function getImageHeight() {
  146. return ($this->imageHeight);
  147. }
  148. /**
  149. * Returns loaded image type
  150. *
  151. * @return string
  152. */
  153. public function getImageType() {
  154. return ($this->imageType);
  155. }
  156. /**
  157. * Returns specified image
  158. *
  159. * @param string $filePath
  160. *
  161. * @return array
  162. */
  163. protected function getImageParams($filePath) {
  164. return (@getimagesize($filePath));
  165. }
  166. /**
  167. * Creates or replaces RGB color in palette
  168. *
  169. * @param string $colorName
  170. * @param int $r
  171. * @param int $g
  172. * @param int $b
  173. *
  174. * @return void
  175. */
  176. public function addColor($colorName, $r, $g, $b) {
  177. $this->colorPalette[$colorName] = array('r' => $r, 'g' => $g, 'b' => $b);
  178. }
  179. /**
  180. * Checks is some image valid?
  181. *
  182. * @param string $filePath
  183. * @param array $imageParams
  184. *
  185. * @return bool
  186. */
  187. public function isImageValid($filePath = '', $imageParams = array()) {
  188. $result = false;
  189. if (empty($imageParams) and !empty($filePath)) {
  190. $imageParams = $this->getImageParams($filePath);
  191. }
  192. if (is_array($imageParams)) {
  193. if (isset($imageParams['mime'])) {
  194. if (strpos($imageParams['mime'], 'image/') !== false) {
  195. $result = true;
  196. }
  197. }
  198. }
  199. return ($result);
  200. }
  201. /**
  202. * Returns image type name
  203. *
  204. * @param string $filePath
  205. * @param array $imageParams
  206. *
  207. * @return string
  208. */
  209. protected function detectImageType($filePath = '', $imageParams = array()) {
  210. $result = '';
  211. if ($this->isImageValid($filePath, $imageParams)) {
  212. if (empty($imageParams) and !empty($filePath)) {
  213. $imageParams = $this->getImageParams($filePath);
  214. }
  215. if (is_array($imageParams)) {
  216. $imageType = $imageParams['mime'];
  217. $imageType = str_replace('image/', '', $imageType);
  218. $result = $imageType;
  219. }
  220. }
  221. return ($result);
  222. }
  223. /**
  224. * Loads some image into protected property from file
  225. *
  226. * @param string $fileName readable image file path
  227. * @return bool
  228. */
  229. protected function loadImageFile($filePath, $propertyName = 'image') {
  230. $result = false;
  231. $imageParams = $this->getImageParams($filePath);
  232. $imageType = $this->detectImageType('', $imageParams);
  233. if (!empty($imageType)) {
  234. $loaderFunctionName = 'imagecreatefrom' . $imageType;
  235. if (function_exists($loaderFunctionName)) {
  236. $this->$propertyName = $loaderFunctionName($filePath);
  237. //setting loaded image base props
  238. if ($this->$propertyName != false) {
  239. if ($propertyName == 'image') {
  240. $this->imageWidth = $imageParams[0];
  241. $this->imageHeight = $imageParams[1];
  242. $this->imageType = $imageType;
  243. }
  244. $result = true;
  245. }
  246. } else {
  247. throw new Exception('EX_NOT_SUPPORTED_FILETYPE:' . $imageType);
  248. }
  249. }
  250. return ($result);
  251. }
  252. /**
  253. * Loads some image into protected property from file
  254. *
  255. * @param string $fileName readable image file path
  256. * @return bool
  257. */
  258. public function loadImage($filePath) {
  259. $result = $this->loadImageFile($filePath, 'image');
  260. return ($result);
  261. }
  262. /**
  263. * Loads a instance image from an base64 encoded image string.
  264. *
  265. * @param string $encodedImage The encoded image string.
  266. * @param string $propertyName The name of the property to store the image in. Default is 'image'.
  267. *
  268. * @return bool Returns true if the base image is successfully loaded, false otherwise.
  269. */
  270. public function loadBaseImage($encodedImage, $propertyName = 'image') {
  271. $result = false;
  272. if (!empty($encodedImage)) {
  273. $decodedImage = base64_decode($encodedImage);
  274. if ($decodedImage) {
  275. $imageParams = getimagesizefromstring($decodedImage);
  276. $imageType = $this->detectImageType('', $imageParams);
  277. if (!empty($imageType)) {
  278. $this->$propertyName = imagecreatefromstring($decodedImage);
  279. if ($this->$propertyName != false) {
  280. if ($propertyName == 'image') {
  281. $this->imageWidth = $imageParams[0];
  282. $this->imageHeight = $imageParams[1];
  283. $this->imageType = $imageType;
  284. }
  285. $result = true;
  286. }
  287. }
  288. }
  289. }
  290. return ($result);
  291. }
  292. /**
  293. * Loads some watermark image into protected property from file
  294. *
  295. * @param string $fileName readable image file path
  296. * @return bool
  297. */
  298. public function loadWatermark($filePath) {
  299. $result = $this->loadImageFile($filePath, 'watermark');
  300. if ($result) {
  301. imagealphablending($this->watermark, false);
  302. }
  303. return ($result);
  304. }
  305. /**
  306. * Renders current instance image into browser or specified file path
  307. *
  308. * @param string $fileName writable file path with name or null to browser rendering
  309. * @param string $type image type to save, like jpeg, png, gif...
  310. *
  311. * @return bool
  312. */
  313. public function saveImage($fileName = null, $type = 'png') {
  314. $result = false;
  315. if ($this->image) {
  316. $saveFunctionName = 'image' . $type;
  317. if (function_exists($saveFunctionName)) {
  318. //custom header on browser output
  319. if (!$fileName) {
  320. header('Content-Type: image/' . $type);
  321. }
  322. if ($type == 'jpeg' or $type == 'png') {
  323. if ($type == 'png') {
  324. imagesavealpha($this->image, true);
  325. }
  326. $result = $saveFunctionName($this->image, $fileName, $this->quality);
  327. } else {
  328. $result = $saveFunctionName($this->image, $fileName);
  329. }
  330. //memory free
  331. imagedestroy($this->image);
  332. //droppin image props
  333. $this->imageWidth = 0;
  334. $this->imageHeight = 0;
  335. $this->imageType = '';
  336. //nothing else matters
  337. if ($fileName === false) {
  338. die();
  339. }
  340. } else {
  341. throw new Exception('EX_NOT_SUPPORTED_FILETYPE:' . $type);
  342. }
  343. } else {
  344. throw new Exception('EX_VOID_IMAGE');
  345. }
  346. return ($result);
  347. }
  348. /**
  349. * Returns current instance image as base64 encoded text
  350. *
  351. * @param string $type image mime type
  352. * @param bool $htmlData data ready to embed as img src HTML base64 data (data URI scheme)
  353. *
  354. * @return void
  355. */
  356. public function getImageBase($type = 'png', $htmlData = false) {
  357. $result = '';
  358. if ($this->image) {
  359. $saveFunctionName = 'image' . $type;
  360. if (function_exists($saveFunctionName)) {
  361. ob_start();
  362. if ($type == 'jpeg' or $type == 'png') {
  363. if ($type == 'png') {
  364. imagesavealpha($this->image, true);
  365. }
  366. }
  367. $result = $saveFunctionName($this->image, null, $this->quality);
  368. //memory free
  369. imagedestroy($this->image);
  370. //droppin image props
  371. $this->imageWidth = 0;
  372. $this->imageHeight = 0;
  373. $this->imageType = '';
  374. $imageBody = ob_get_contents();
  375. ob_end_clean();
  376. if (!empty($imageBody)) {
  377. $result = base64_encode($imageBody);
  378. // print($imageBody);
  379. }
  380. //optional html embed data
  381. if ($htmlData) {
  382. $result = 'data:image/' . $type . ';charset=utf-8;base64,' . $result;
  383. }
  384. } else {
  385. throw new Exception('EX_NOT_SUPPORTED_FILETYPE:' . $type);
  386. }
  387. } else {
  388. throw new Exception('EX_VOID_IMAGE');
  389. }
  390. return ($result);
  391. }
  392. /**
  393. * Renders current instance image into browser
  394. *
  395. * @param string $type
  396. *
  397. * @return void
  398. */
  399. public function renderImage($type = 'png') {
  400. $this->saveImage(null, $type);
  401. }
  402. /**
  403. * Creates new empty true-color image
  404. *
  405. * @return void
  406. */
  407. public function createImage($width, $height) {
  408. $this->image = imagecreatetruecolor($width, $height);
  409. $this->imageWidth = $width;
  410. $this->imageHeight = $height;
  411. }
  412. /**
  413. * Scales image to some scale
  414. *
  415. * @param float $scale something like 0.5 for 50% or 2 for 2x scale
  416. *
  417. * @return void
  418. */
  419. public function scale($scale) {
  420. if ($this->imageWidth and $this->imageHeight) {
  421. if ($scale != 1) {
  422. $nWidth = $this->imageWidth * $scale;
  423. $nHeight = $this->imageHeight * $scale;
  424. $imageCopy = imagecreatetruecolor($nWidth, $nHeight);
  425. imagealphablending($imageCopy, false);
  426. imagesavealpha($imageCopy, true);
  427. imagecopyresized($imageCopy, $this->image, 0, 0, 0, 0, $nWidth, $nHeight, $this->imageWidth, $this->imageHeight);
  428. $this->image = $imageCopy;
  429. $this->imageWidth = $nWidth;
  430. $this->imageHeight = $nHeight;
  431. }
  432. }
  433. }
  434. /**
  435. * Resizes image to some new dimensions
  436. *
  437. * @param int $width
  438. * @param int $height
  439. *
  440. * @return void
  441. */
  442. public function resize($width, $height) {
  443. if ($this->imageWidth and $this->imageHeight) {
  444. $imageResized = imagescale($this->image, $width, $height);
  445. $this->image = $imageResized;
  446. $this->imageWidth = $width;
  447. $this->imageHeight = $height;
  448. }
  449. }
  450. /**
  451. * Crops image to new dimensions starting from 0x0
  452. *
  453. * @return void
  454. */
  455. public function crop($width, $height) {
  456. if ($this->imageWidth and $this->imageHeight) {
  457. $imageCropped = imagecrop($this->image, array('x' => 0, 'y' => 0, 'width' => $width, 'height' => $height));
  458. $this->image = $imageCropped;
  459. $this->imageWidth = $width;
  460. $this->imageHeight = $height;
  461. }
  462. }
  463. /**
  464. * Crops image to selected region by coords
  465. *
  466. * @return void
  467. */
  468. public function cropRegion($x1, $y1, $x2, $y2) {
  469. if ($this->imageWidth and $this->imageHeight) {
  470. $imageCropped = imagecrop($this->image, array('x' => $x1, 'y' => $y1, 'width' => $x2, 'height' => $y2));
  471. $this->image = $imageCropped;
  472. $this->imageWidth = $x2;
  473. $this->imageHeight = $y2;
  474. }
  475. }
  476. /**
  477. * Allocates and returns some image color by its name
  478. *
  479. * @param string $colorName
  480. *
  481. * @return int
  482. */
  483. protected function allocateColor($colorName) {
  484. $result = 0;
  485. if (isset($this->colorPalette[$colorName])) {
  486. $colorData = $this->colorPalette[$colorName];
  487. if (isset($this->colorsAllocated[$colorName])) {
  488. $result = $this->colorsAllocated[$colorName];
  489. } else {
  490. $result = imagecolorallocate($this->image, $colorData['r'], $colorData['g'], $colorData['b']);
  491. $this->colorsAllocated[$colorName] = $result;
  492. }
  493. } else {
  494. throw new Exception('EX_COLOR_NOT_EXISTS:' . $colorName);
  495. }
  496. return ($result);
  497. }
  498. /**
  499. * Fills image with some color from palette
  500. *
  501. * @param string $colorName
  502. *
  503. * @return void
  504. */
  505. public function fill($colorName) {
  506. imagefill($this->image, 0, 0, $this->allocateColor($colorName));
  507. }
  508. /**
  509. * Draws pixel on X/Y coords with some color from palette
  510. *
  511. * @param int y
  512. * @param int x
  513. * @param string $colorName
  514. *
  515. * @return void
  516. */
  517. public function drawPixel($x, $y, $colorName) {
  518. imagesetpixel($this->image, $x, $y, $this->allocateColor($colorName));
  519. }
  520. /**
  521. * Prints some text string at specified X/Y coords with default font
  522. *
  523. * @param int $x
  524. * @param int $y
  525. * @param string $text
  526. * @param string $colorName
  527. * @param int $size=1
  528. * @param bool $vertical
  529. *
  530. * @return void
  531. */
  532. public function drawString($x, $y, $text, $colorName, $size = 1, $vertical = false) {
  533. if (!empty($text)) {
  534. if ($vertical) {
  535. imagestringup($this->image, $size, $x, $y, $text, $this->allocateColor($colorName));
  536. } else {
  537. imagestring($this->image, $size, $x, $y, $text, $this->allocateColor($colorName));
  538. }
  539. }
  540. }
  541. /**
  542. * Write text to the image using TrueType fonts
  543. *
  544. * @param int $x
  545. * @param int $y
  546. * @param string $text
  547. * @param string $colorName
  548. *
  549. * @return void
  550. */
  551. public function drawText($x, $y, $text, $colorName) {
  552. if (!empty($text)) {
  553. imagettftext($this->image, $this->fontSize, 0, $x, $y, $this->allocateColor($colorName), $this->font, $text);
  554. }
  555. }
  556. /**
  557. * Returns font size that fits into image width
  558. *
  559. * @param int $fontSize font size that required to fit text
  560. * @param string $text text data that required to fit
  561. * @param int $padding text padding in px
  562. *
  563. * @return int
  564. */
  565. protected function guessFontSize($fontSize, $text, $padding) {
  566. $box = imageftbbox($fontSize, 0, $this->font, $text);
  567. $boxWidth = $box[4] - $box[6];
  568. $imageWidth = $this->imageWidth - ($padding * 2);
  569. if ($boxWidth > $imageWidth) {
  570. $fontSize = $fontSize - 1;
  571. return $this->guessFontSize($fontSize, $text, $padding);
  572. }
  573. return ($fontSize);
  574. }
  575. /**
  576. * Write single text to the image using TrueType font with auto size selection
  577. *
  578. * @param int $y
  579. * @param int $padding
  580. * @param string $text
  581. * @param string $colorName
  582. * @param string $outlineColor
  583. *
  584. * @return void
  585. */
  586. public function drawTextAutoSize($y, $padding = 10, $text = '', $colorName = '', $outlineColor = '') {
  587. if (!empty($text)) {
  588. $defaultFontSize = 40;
  589. $border = 1;
  590. $x = $padding;
  591. $colorName = (empty($colorName)) ? 'white' : $colorName;
  592. $outlineColor = (empty($outlineColor)) ? 'black' : $outlineColor;
  593. //guessing font size
  594. $fontSize = $this->guessFontSize($defaultFontSize, $text, $padding);
  595. //drawing outline if required
  596. if ($outlineColor) {
  597. for ($c1 = ($x - abs($border)); $c1 <= ($x + abs($border)); $c1++) {
  598. for ($c2 = ($y - abs($border)); $c2 <= ($y + abs($border)); $c2++) {
  599. imagettftext($this->image, $fontSize, 0, $c1, $c2, $this->allocateColor($outlineColor), $this->font, $text);
  600. }
  601. }
  602. }
  603. //and text with selected color
  604. imagettftext($this->image, $fontSize, 0, $x, $y, $this->allocateColor($colorName), $this->font, $text);
  605. }
  606. }
  607. /**
  608. * Set the thickness for line drawing in px
  609. *
  610. * @param int $lineWidth
  611. *
  612. * @return void
  613. */
  614. public function setLineWidth($lineWidth) {
  615. $this->lineWidth = $lineWidth;
  616. imagesetthickness($this->image, $this->lineWidth);
  617. }
  618. /**
  619. * Draws filled rectangle
  620. *
  621. * @param int $x1
  622. * @param int $y1
  623. * @param int $x2
  624. * @param int $y2
  625. * @param string $colorName
  626. *
  627. * @return void
  628. */
  629. public function drawRectangle($x1, $y1, $x2, $y2, $colorName) {
  630. if (isset($this->colorPalette[$colorName])) {
  631. $colorData = $this->colorPalette[$colorName];
  632. if (isset($this->colorsAllocated[$colorName])) {
  633. $drawingColor = $this->colorsAllocated[$colorName];
  634. } else {
  635. $drawingColor = imagecolorallocate($this->image, $colorData['r'], $colorData['g'], $colorData['b']);
  636. $this->colorsAllocated[$colorName] = $drawingColor;
  637. }
  638. imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $drawingColor);
  639. } else {
  640. throw new Exception('EX_COLOR_NOT_EXISTS:' . $colorName);
  641. }
  642. }
  643. /**
  644. * Draws a line
  645. *
  646. * @param int $x1
  647. * @param int $y1
  648. * @param int $x2
  649. * @param int $y2
  650. * @param string $colorName
  651. *
  652. * @return void
  653. */
  654. public function drawLine($x1, $y1, $x2, $y2, $colorName) {
  655. imageline($this->image, $x1, $y1, $x2, $y2, $this->allocateColor($colorName));
  656. }
  657. /**
  658. * Puts preloaded watermark on base image
  659. *
  660. * @param int $stretch=true
  661. * @param int $x=0
  662. * @param int $y=0
  663. *
  664. * @return void
  665. */
  666. public function drawWatermark($stretch = true, $x = 0, $y = 0) {
  667. imagealphablending($this->watermark, false);
  668. $watermarkWidth = imagesx($this->watermark);
  669. $watermarkHeight = imagesy($this->watermark);
  670. if ($stretch) {
  671. imagecopyresampled($this->image, $this->watermark, $x, $y, 0, 0, $this->imageWidth, $this->imageHeight, $watermarkWidth, $watermarkHeight);
  672. } else {
  673. imagecopy($this->image, $this->watermark, $x, $y, 0, 0, $watermarkWidth, $watermarkHeight);
  674. }
  675. }
  676. /**
  677. * Applies pixelation filter
  678. *
  679. * @param int $blockSize
  680. * @param bool $smooth
  681. *
  682. * @return void
  683. */
  684. public function pixelate($blockSize, $smooth = true) {
  685. imagefilter($this->image, IMG_FILTER_PIXELATE, $blockSize, $smooth);
  686. }
  687. /**
  688. * Applies image filters set to current instance base image
  689. *
  690. * @param array|int $filterSet must contains array of filters as index=>(IMAGE_FILTER=>argsArray)
  691. *
  692. * @return void
  693. */
  694. public function imageFilters($filterSet = array()) {
  695. if (!empty($filterSet)) {
  696. foreach ($filterSet as $eachFilterIdx => $eachFilterData) {
  697. if (is_array($eachFilterData)) {
  698. foreach ($eachFilterData as $eachFilter => $eachFilterArgs/* */) {
  699. if (is_array($eachFilterArgs)) {
  700. $filterArgsTmp = array();
  701. $filterArgsTmp[] = $this->image;
  702. $filterArgsTmp[] = $eachFilter;
  703. foreach ($eachFilterArgs as $io => $each) {
  704. $filterArgsTmp[] = $each;
  705. }
  706. //not using just "..." arg unpack operator here due PHP <5.6 compat
  707. call_user_func_array('imagefilter', $filterArgsTmp);
  708. } else {
  709. imagefilter($this->image, $eachFilter, $eachFilterArgs);
  710. }
  711. }
  712. }
  713. }
  714. }
  715. }
  716. /**
  717. * Returns RGB values for some specified image pixel as r/g/b/a(lpha)
  718. *
  719. * @return array
  720. */
  721. public function getPixelColor($x, $y) {
  722. $result = array();
  723. $rgb = imagecolorat($this->image, $x, $y);
  724. $components = imagecolorsforindex($this->image, $rgb);
  725. $result['r'] = $components['red'];
  726. $result['g'] = $components['green'];
  727. $result['b'] = $components['blue'];
  728. $result['a'] = $components['alpha'];
  729. return ($result);
  730. }
  731. /**
  732. * Converts RGB components array into hex string
  733. *
  734. * @param array $rgb RGB/RGBa components array
  735. *
  736. * @return string
  737. */
  738. public function rgbToHex($rgb) {
  739. $result = '';
  740. if (!empty($rgb)) {
  741. $result = sprintf("#%02x%02x%02x", $rgb['r'], $rgb['g'], $rgb['b']);
  742. }
  743. return ($result);
  744. }
  745. /**
  746. * Converts hex color string to RGB components array
  747. *
  748. * @param string $hex hex string as RRGGBB
  749. *
  750. * @return array
  751. */
  752. public function hexToRgb($hex) {
  753. $result = '';
  754. if (!empty($hex)) {
  755. $hex = str_replace('#', '', $hex);
  756. $r = hexdec(substr($hex, 0, 2));
  757. $g = hexdec(substr($hex, 2, 2));
  758. $b = hexdec(substr($hex, 4, 2));
  759. $result = array('r' => $r, 'g' => $g, 'b' => $b);
  760. }
  761. return ($result);
  762. }
  763. /**
  764. * Calculates the brightness value of an RGB color.
  765. *
  766. * @param array $rgb The RGB color values as an associative array with keys 'r', 'g', and 'b'.
  767. *
  768. * @return int The brightness value of the RGB color.
  769. */
  770. public function rgbToBrightness($rgb) {
  771. $result = round(($rgb['r'] + $rgb['g'] + $rgb['b']) / 3);
  772. return ($result);
  773. }
  774. /**
  775. * Returns color map for current intance image as array(y,x)=>color
  776. *
  777. * @param bool $hex returns map values as rrggbb hex values or raw rgba components
  778. *
  779. * @return array
  780. */
  781. public function getColorMap($hex = true) {
  782. $result = array();
  783. for ($x = 0; $x < $this->imageWidth; $x++) {
  784. for ($y = 0; $y < $this->imageHeight; $y++) {
  785. $rgb = $this->getPixelColor($x, $y);
  786. if ($hex) {
  787. $result[$y][$x] = $this->rgbToHex($rgb);
  788. } else {
  789. $result[$y][$x] = $rgb;
  790. }
  791. }
  792. }
  793. return ($result);
  794. }
  795. /**
  796. * Calculates the brightness of a pixel at the specified coordinates.
  797. *
  798. * @param int $x The x-coordinate of the pixel.
  799. * @param int $y The y-coordinate of the pixel.
  800. *
  801. * @return int The brightness value of the pixel.
  802. */
  803. public function getPixelBrightness($x, $y) {
  804. $result = false;
  805. $pixelColor = $this->getPixelColor($x, $y);
  806. $result = $this->rgbToBrightness($pixelColor);
  807. return ($result);
  808. }
  809. }