/core/media/image/text.class.php

https://github.com/konfirm/konsolidate · PHP · 272 lines · 164 code · 27 blank · 81 comment · 24 complexity · 68117966b05f5bf4b82f351455253a05 MD5 · raw file

  1. <?php
  2. /*
  3. * ________ ___
  4. * / / /\ /\ Konsolidate
  5. * ____/ /___/ \/ \
  6. * / /\ / http://www.konsolidate.nl
  7. * /___ ___/ \ /
  8. * \ / /\ \ / \ Class: CoreMediaImageText
  9. * \/___/ \___\/ \ Tier: Core
  10. * \ \ /\ \ /\ / Module: Media/Image/Text
  11. * \___\/ \___\/ \/
  12. * \ \ / $Rev$
  13. * \___ ___\/ $Author$
  14. * \ \ / $Date$
  15. * \___\/
  16. */
  17. /**
  18. * Add text to images
  19. * @name CoreMediaImageText
  20. * @type class
  21. * @package Konsolidate
  22. * @author Rogier Spieker <rogier@konsolidate.nl>
  23. */
  24. class CoreMediaImageText extends Konsolidate
  25. {
  26. /**
  27. * Style information
  28. * @name _focalLength
  29. * @type int
  30. * @access protected
  31. */
  32. protected $_style;
  33. /**
  34. * Create a new textlabel
  35. * @name create
  36. * @type method
  37. * @access public
  38. * @return resource image
  39. * @note this method examines the provided input and bridges the call to the most approproate method avaiable
  40. * to handle your request based on the provided parameters. Refer to the method calls of _createFromStyle and _createFromArgument
  41. * for an overview of possible arguments (the calls cannot be mixed, use either one of the calls)
  42. * @see _createFromStyle
  43. * @see _createFromArgument
  44. */
  45. public function create()
  46. {
  47. $aArgument = func_get_args();
  48. if ( count( $aArgument ) > 3 || ( isset( $aArgument[ 2 ] ) && !is_array( $aArgument[ 2 ] ) ) )
  49. return call_user_func_array(
  50. Array(
  51. $this,
  52. "_createFromArgument"
  53. ),
  54. $aArgument
  55. );
  56. return call_user_func_array(
  57. Array(
  58. $this,
  59. "_createFromStyle"
  60. ),
  61. $aArgument
  62. );
  63. }
  64. /**
  65. * Create a new textlabel based on an style property array
  66. * @name _createFromStyle
  67. * @type method
  68. * @access protected
  69. * @param resource image (or null to create a new image)
  70. * @param string text
  71. * @param array style definition
  72. * @return resource image
  73. * @syntax CoreMediaImageText->_createFromStyle( resource image, string text, array style )
  74. * @see create
  75. */
  76. protected function _createFromStyle( $mImage, $sText, $aStyle )
  77. {
  78. $this->_style = $aStyle;
  79. $bAntiAlias = $this->_getStyle( "anti-alias" );
  80. $nSize = (int) $this->_getStyle( "font-size", 10 );
  81. $nAngle = (int) $this->_getStyle( "angle", 0 );
  82. $nWidth = (int) $this->_getStyle( "width" );
  83. $nHeight = (int) $this->_getStyle( "height" );
  84. $nX = (int) $this->_getStyle( "left", 0 );
  85. $nY = (int) $this->_getStyle( "top", 0 );
  86. $sColor = (string) $this->_getStyle( "color", "#000" );
  87. $sBGColor = (string) $this->_getStyle( "background-color", "#fff" );
  88. $sBGImage = (string) $this->_getStyle( "background-image" );
  89. $sBGRepeat = (string) $this->_getStyle( "background-repeat", "repeat" );
  90. $sFont = DOCUMENT_ROOT . $this->_getStyle( "font-family" );
  91. if ( is_null( $bAntiAlias ) )
  92. $bAntiAlias = $nSize > 24;
  93. else
  94. $bAntiAlias = (bool) $bAntiAlias;
  95. $nBaseSize = $bAntiAlias ? $nSize * ( $nSize < 72 ? 4 : 2 ) : $nSize;
  96. $aPoint = imagettfbbox( $nBaseSize, $nAngle, $sFont, $sText );
  97. $nFontX = min( $aPoint[ 0 ], $aPoint[ 6 ] ) * -1;
  98. $nFontY = min( $aPoint[ 5 ], $aPoint[ 7 ] ) * -1;
  99. $nTextHeight = max( $aPoint[ 1 ], $aPoint[ 3 ] ) - min( $aPoint[ 5 ], $aPoint[ 7 ] );
  100. $nTextWidth = max( $aPoint[ 2 ], $aPoint[ 4 ] ) - min( $aPoint[ 0 ], $aPoint[ 6 ] );
  101. $nRatio = $nSize / $nBaseSize;
  102. $nNewWidth = !empty( $nWidth ) ? $nWidth : ceil( ( $nTextWidth + 8 ) * $nRatio );
  103. $nNewHeight = !empty( $nHeight ) ? $nHeight : ceil( ( $nTextHeight + 6 ) * $nRatio );
  104. if ( empty( $nWidth ) )
  105. $nWidth = $nTextWidth + 8;
  106. if ( empty( $nHeight ) )
  107. $nHeight = $nTextHeight + 8;
  108. if ( !empty( $sBGImage ) )
  109. {
  110. $mImage = &$this->call( "../create", $nNewWidth, $nNewHeight, $sBGColor );
  111. $rTile = &$this->call( "../_load", DOCUMENT_ROOT . $sBGImage );
  112. $nTileWidth = imagesx( $rTile );
  113. $nTileHeight = imagesy( $rTile );
  114. if ( strToLower( substr( $sBGRepeat, 0, 6 ) ) == "repeat" )
  115. {
  116. imagesettile( $mImage, $rTile );
  117. imagefilledrectangle( $mImage, 0, 0, strToLower( $sBGRepeat ) == "repeat-y" ? $nTileWidth : $nNewWidth, strToLower( $sBGRepeat ) == "repeat-x" ? $nTileHeight : $nNewHeight, IMG_COLOR_TILED );
  118. }
  119. else
  120. {
  121. $mImage = $this->call( "../copy", $mImage, $rTile );
  122. }
  123. }
  124. elseif ( !is_resource( $mImage ) )
  125. {
  126. if ( $sBGColor == "transparent" || $sBGColor == "none" || empty( $sBGColor ) )
  127. $mImage = &$this->call( "../create", $nNewWidth, $nNewHeight );
  128. else
  129. $mImage = &$this->call( "../create", $nNewWidth, $nNewHeight, $sBGColor );
  130. }
  131. if ( $bAntiAlias )
  132. {
  133. $rImage = $this->call( "../_create", $nTextWidth, $nTextHeight );
  134. $rText = $this->call( "../_create", $nNewWidth, $nNewHeight );
  135. $nTrans = imagecolorallocatealpha( $rImage, 0, 0, 0, 127 );
  136. $nColor = $this->call( "../getColor", $sColor, $rImage );
  137. $nBGColor = $this->call( "../getColor", "#000", $rText );
  138. imagealphablending( $rImage, false );
  139. imagefilledrectangle( $rImage, 0, 0, $nTextWidth, $nTextHeight, $nTrans );
  140. imagealphablending( $rImage, true );
  141. imagettftext( $rImage, $nBaseSize, $nAngle, $nFontX, $nFontY, $nColor, $sFont, $sText );
  142. imagealphablending( $rImage, false );
  143. imagecolortransparent( $rText, $nBGColor );
  144. imagealphablending( $rText, false );
  145. imagecopyresampled( $rText, $rImage, 0, 0, 0, 0, $nNewWidth, $nNewHeight, $nWidth, $nHeight );
  146. imagealphablending( $mImage, true );
  147. imagecopy( $mImage, $rText, $nX, $nY, 0, 0, $nNewWidth, $nNewHeight );
  148. imagealphablending( $mImage, false );
  149. imagedestroy( $rImage );
  150. imagedestroy( $rText );
  151. }
  152. else
  153. {
  154. imagealphablending( $mImage, true );
  155. imagettftext( $mImage, $nSize, $nAngle, $nX, $nFontY + $nY, $this->call( "../getColor", $sColor ), $sFont, $sText );
  156. }
  157. return $mImage;
  158. }
  159. /**
  160. * Create a new textlabel based on an style property array
  161. * @name _create
  162. * @type method
  163. * @access protected
  164. * @param resource image (or null to create a new image)
  165. * @param string text
  166. * @param string fontfilelocation
  167. * @param int textsize (optional, default 10)
  168. * @param int X position (optional, default 0)
  169. * @param int Y position (optional, default 0)
  170. * @param string color (optional, default "#000", black)
  171. * @param string background color (optional, default "#fff", white)
  172. * @param bool antialias (optional, default false for text sizes < 24, true otherwise)
  173. * @param int angle of rotation in degrees (optional, default 0)
  174. * @return resource image
  175. * @syntax CoreMediaImageText->_createFromArgument( resource image, string text, string fontfile [, int textsize [, int X [, int Y [, string color [, string backgroundcolor [, bool antialias [, float angle ] ] ] ] ] ] ] )
  176. * @see create
  177. */
  178. protected function _createFromArgument( $mImage, $sText, $sFont, $nSize=10, $nX=0, $nY=0, $sColor="#000", $sBGColor="#fff", $bAntiAlias=null, $nAngle=0 )
  179. {
  180. if ( is_null( $bAntiAlias ) )
  181. $bAntiAlias = $nSize > 24;
  182. if ( $bAntiAlias === true )
  183. {
  184. $nBaseSize = $nSize * ( $nSize < 72 ? 4 : 2 );
  185. $aPoint = imagettfbbox( $nBaseSize, $nAngle, $sFont, $sText );
  186. $nFontX = min( $aPoint[ 0 ], $aPoint[ 6 ]) * -1;
  187. $nFontY = min( $aPoint[ 5 ], $aPoint[ 7 ]) * -1;
  188. $nHeight = max( $aPoint[ 1 ], $aPoint[ 3 ]) - min( $aPoint[ 5 ], $aPoint[ 7 ] );
  189. $nWidth = max( $aPoint[ 2 ], $aPoint[ 4 ]) - min( $aPoint[ 0 ], $aPoint[ 6 ] );
  190. $nRatio = $nSize / $nBaseSize;
  191. $nNewWidth = ceil( $nWidth * $nRatio );
  192. $nNewHeight = ceil( $nHeight * $nRatio );
  193. $rImage = $this->call( "../_create", $nWidth, $nHeight );
  194. $rText = $this->call( "../_create", $nNewWidth, $nNewHeight );
  195. $nTrans = imagecolorallocatealpha( $rImage, 0, 0, 0, 127 );
  196. $nColor = $this->call( "../getColor", $sColor, $rImage );
  197. $nBGColor = $this->call( "../getColor", "#000", $rText );
  198. if ( !is_resource( $mImage ) )
  199. $mImage = &$this->call( "../create", $nNewWidth + $nX, $nNewHeight + $nY, $sBGColor );
  200. imagealphablending( $rImage, false );
  201. imagefilledrectangle( $rImage, 0, 0, $nWidth, $nHeight, $nTrans );
  202. imagealphablending( $rImage, true );
  203. imagettftext( $rImage, $nBaseSize, $nAngle, $nFontX, $nFontY, $nColor, $sFont, $sText );
  204. imagealphablending( $rImage, false );
  205. imagecolortransparent( $rText, $bkg );
  206. imagealphablending( $rText, false );
  207. imagecopyresampled( $rText, $rImage, 0, 0, 0, 0, $nNewWidth, $nNewHeight, $nWidth, $nHeight );
  208. imagealphablending( $mImage, true );
  209. imagecopy( $mImage, $rText, $nX, $nY, 0, 0, $nNewWidth, $nNewHeight );
  210. imagealphablending( $mImage, false );
  211. imagedestroy( $rImage);
  212. imagedestroy( $rText );
  213. }
  214. else
  215. {
  216. $aPoint = imagettfbbox( $nSize, $nAngle, $sFont, $sText );
  217. $nFontY = min( $aPoint[ 5 ], $aPoint[ 7 ]) * -1;
  218. $nHeight = 2 + ( max( $aPoint[ 1 ], $aPoint[ 3 ] ) - min( $aPoint[ 5 ], $aPoint[ 7 ] ) );
  219. $nWidth = 2 + ( max( $aPoint[ 2 ], $aPoint[ 4 ] ) - min( $aPoint[ 0 ], $aPoint[ 6 ] ) );
  220. if ( !is_resource( $mImage ) )
  221. $mImage = &$this->call( "../create", $nWidth + $nX, $nHeight + $nY, $sBGColor );
  222. else
  223. imagealphablending( $mImage, true );
  224. imagettftext( $mImage, $nSize, $nAngle, $nX, $nFontY + $nY, $this->call( "../getColor", $sColor ), $sFont, $sText );
  225. }
  226. return $mImage;
  227. }
  228. /**
  229. * get style from style definition array
  230. * @name _getStyle
  231. * @type method
  232. * @access protected
  233. * @param string property
  234. * @param string default (optional, default null)
  235. * @return mixed property value
  236. * @syntax CoreMediaImageText->_getStyle( string property [, mixed default ] )
  237. */
  238. protected function _getStyle( $sProperty, $mDefault=null )
  239. {
  240. if ( array_key_exists( $sProperty, $this->_style ) && $this->_style[ $sProperty ] != "auto" )
  241. return $this->_style[ $sProperty ];
  242. return $mDefault;
  243. }
  244. }
  245. ?>