/app/bundles/CoreBundle/Assets/js/libraries/ckeditor/filemanager/connectors/php/inc/wideimage/lib/TrueColorImage.php

https://gitlab.com/mautic-master/mautic · PHP · 218 lines · 118 code · 12 blank · 88 comment · 13 complexity · 232832fda592ca30637a8c23ed516b40 MD5 · raw file

  1. <?php
  2. /**
  3. * @author Gasper Kozak
  4. * @copyright 2007-2011
  5. This file is part of WideImage.
  6. WideImage is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation; either version 2.1 of the License, or
  9. (at your option) any later version.
  10. WideImage is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with WideImage; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. **/
  18. /**
  19. * A class for truecolor image objects
  20. *
  21. * @package WideImage
  22. */
  23. class WideImage_TrueColorImage extends WideImage_Image
  24. {
  25. /**
  26. * Creates the object
  27. *
  28. * @param resource $handle
  29. */
  30. function __construct($handle)
  31. {
  32. parent::__construct($handle);
  33. $this->alphaBlending(false);
  34. $this->saveAlpha(true);
  35. }
  36. /**
  37. * Factory method that creates a true-color image object
  38. *
  39. * @param int $width
  40. * @param int $height
  41. * @return WideImage_TrueColorImage
  42. */
  43. static function create($width, $height)
  44. {
  45. if ($width * $height <= 0 || $width < 0)
  46. throw new WideImage_InvalidImageDimensionException("Can't create an image with dimensions [$width, $height].");
  47. return new WideImage_TrueColorImage(imagecreatetruecolor($width, $height));
  48. }
  49. function doCreate($width, $height)
  50. {
  51. return self::create($width, $height);
  52. }
  53. function isTrueColor()
  54. {
  55. return true;
  56. }
  57. /**
  58. * Sets alpha blending mode via imagealphablending()
  59. *
  60. * @param bool $mode
  61. * @return bool
  62. */
  63. function alphaBlending($mode)
  64. {
  65. return imagealphablending($this->handle, $mode);
  66. }
  67. /**
  68. * Toggle if alpha channel should be saved with the image via imagesavealpha()
  69. *
  70. * @param bool $on
  71. * @return bool
  72. */
  73. function saveAlpha($on)
  74. {
  75. return imagesavealpha($this->handle, $on);
  76. }
  77. /**
  78. * Allocates a color and returns its index
  79. *
  80. * This method accepts either each component as an integer value,
  81. * or an associative array that holds the color's components in keys
  82. * 'red', 'green', 'blue', 'alpha'.
  83. *
  84. * @param mixed $R
  85. * @param int $G
  86. * @param int $B
  87. * @param int $A
  88. * @return int
  89. */
  90. function allocateColorAlpha($R, $G = null, $B = null, $A = null)
  91. {
  92. if (is_array($R))
  93. return imageColorAllocateAlpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']);
  94. else
  95. return imageColorAllocateAlpha($this->handle, $R, $G, $B, $A);
  96. }
  97. /**
  98. * @see WideImage_Image#asPalette($nColors, $dither, $matchPalette)
  99. */
  100. function asPalette($nColors = 255, $dither = null, $matchPalette = true)
  101. {
  102. $nColors = intval($nColors);
  103. if ($nColors < 1)
  104. $nColors = 1;
  105. elseif ($nColors > 255)
  106. $nColors = 255;
  107. if ($dither === null)
  108. $dither = $this->isTransparent();
  109. $temp = $this->copy();
  110. imagetruecolortopalette($temp->handle, $dither, $nColors);
  111. if ($matchPalette == true && function_exists('imagecolormatch'))
  112. imagecolormatch($this->handle, $temp->handle);
  113. // The code below isn't working properly; it corrupts transparency on some palette->tc->palette conversions.
  114. // Why is this code here?
  115. /*
  116. if ($this->isTransparent())
  117. {
  118. $trgb = $this->getTransparentColorRGB();
  119. $tci = $temp->getClosestColor($trgb);
  120. $temp->setTransparentColor($tci);
  121. }
  122. /**/
  123. $temp->releaseHandle();
  124. return new WideImage_PaletteImage($temp->handle);
  125. }
  126. /**
  127. * Returns the index of the color that best match the given color components
  128. *
  129. * This method accepts either each component as an integer value,
  130. * or an associative array that holds the color's components in keys
  131. * 'red', 'green', 'blue', 'alpha'.
  132. *
  133. * @param mixed $R Red component value or an associative array
  134. * @param int $G Green component
  135. * @param int $B Blue component
  136. * @param int $A Alpha component
  137. * @return int The color index
  138. */
  139. function getClosestColorAlpha($R, $G = null, $B = null, $A = null)
  140. {
  141. if (is_array($R))
  142. return imagecolorclosestalpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']);
  143. else
  144. return imagecolorclosestalpha($this->handle, $R, $G, $B, $A);
  145. }
  146. /**
  147. * Returns the index of the color that exactly match the given color components
  148. *
  149. * This method accepts either each component as an integer value,
  150. * or an associative array that holds the color's components in keys
  151. * 'red', 'green', 'blue', 'alpha'.
  152. *
  153. * @param mixed $R Red component value or an associative array
  154. * @param int $G Green component
  155. * @param int $B Blue component
  156. * @param int $A Alpha component
  157. * @return int The color index
  158. */
  159. function getExactColorAlpha($R, $G = null, $B = null, $A = null)
  160. {
  161. if (is_array($R))
  162. return imagecolorexactalpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']);
  163. else
  164. return imagecolorexactalpha($this->handle, $R, $G, $B, $A);
  165. }
  166. /**
  167. * @see WideImage_Image#getChannels()
  168. */
  169. function getChannels()
  170. {
  171. $args = func_get_args();
  172. if (count($args) == 1 && is_array($args[0]))
  173. $args = $args[0];
  174. return WideImage_OperationFactory::get('CopyChannelsTrueColor')->execute($this, $args);
  175. }
  176. /**
  177. * (non-PHPdoc)
  178. * @see WideImage_Image#copyNoAlpha()
  179. */
  180. function copyNoAlpha()
  181. {
  182. $prev = $this->saveAlpha(false);
  183. $result = WideImage_Image::loadFromString($this->asString('png'));
  184. $this->saveAlpha($prev);
  185. //$result->releaseHandle();
  186. return $result;
  187. }
  188. /**
  189. * (non-PHPdoc)
  190. * @see WideImage_Image#asTrueColor()
  191. */
  192. function asTrueColor()
  193. {
  194. return $this->copy();
  195. }
  196. }