PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ezimage/classes/ezimageinterface.php

https://github.com/StephanBoganskyXrow/ezpublish
PHP | 815 lines | 510 code | 61 blank | 244 comment | 59 complexity | 0841861273e5e9d65a120d506e4d0959 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. //
  3. // Definition of eZImageInterface class
  4. //
  5. // Created on: <03-Oct-2002 15:05:09 amos>
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2011 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*! \file
  31. */
  32. /*! \defgroup eZImageObject Image object and layer handling */
  33. /*!
  34. \class eZImageInterface ezimageinterface.php
  35. \brief Base interface for all image object and layer classes
  36. */
  37. class eZImageInterface
  38. {
  39. function eZImageInterface( $imageObjectRef = null, $imageObject = null, $width = false, $height = false )
  40. {
  41. $this->ImageObjectRef = $imageObjectRef;
  42. $this->ImageObject = $imageObject;
  43. $this->Width = $width;
  44. $this->Height = $height;
  45. $this->AlternativeText = '';
  46. $this->IsTrueColor = null;
  47. $this->Palette = array();
  48. $this->PaletteIndex = array();
  49. $this->StoredPath = false;
  50. $this->Font = null;
  51. $this->IsProcessed = false;
  52. }
  53. /*!
  54. \return true if the image is true color. True color images behave differently from palette based
  55. and GD has problems with mixing the two types.
  56. */
  57. function isTruecolor()
  58. {
  59. return $this->IsTrueColor;
  60. }
  61. /*!
  62. \private
  63. \return a map array which maps from an attribute name to a member variable.
  64. Used by attributes, hasAttribute and attribute.
  65. */
  66. function attributeMemberMap()
  67. {
  68. return array( 'filepath' => 'StoredPath',
  69. 'filename' => 'StoredFile',
  70. 'width' => 'Width',
  71. 'height' => 'Height',
  72. 'alternative_text' => 'AlternativeText' );
  73. }
  74. /*!
  75. \private
  76. \return a map array which maps from an attribute name to a member function.
  77. Used by attributes, hasAttribute and attribute.
  78. */
  79. function attributeFunctionMap()
  80. {
  81. return array( 'imagepath' => 'imagePath',
  82. 'has_size' => 'hasSize' );
  83. }
  84. /*!
  85. \return an array with attribute names which this object supports.
  86. */
  87. function attributes()
  88. {
  89. return array_merge( array_keys( eZImageInterface::attributeMemberMap() ),
  90. array_keys( eZImageInterface::attributeFunctionMap() ) );
  91. }
  92. /*!
  93. \return true if the attribute \a $name exists.
  94. */
  95. function hasAttribute( $name )
  96. {
  97. $attributeMemberMap = eZImageInterface::attributeMemberMap();
  98. if ( isset( $attributeMemberMap[$name] ) )
  99. return true;
  100. $attributeFunctionMap = eZImageInterface::attributeFunctionMap();
  101. if ( isset( $attributeFunctionMap[$name] ) )
  102. return true;
  103. return false;
  104. }
  105. /*!
  106. \return the attribute with name \a $name or \c null if the attribute does not exist.
  107. */
  108. function attribute( $name )
  109. {
  110. $attributeMemberMap = eZImageInterface::attributeMemberMap();
  111. if ( isset( $attributeMemberMap[$name] ) )
  112. {
  113. $member = $attributeMemberMap[$name];
  114. if ( isset( $this->$member ) )
  115. return $this->$member;
  116. eZDebug::writeWarning( 'The member variable $member was not found for attribute $name', __METHOD__ );
  117. return null;
  118. }
  119. $attributeFunctionMap = eZImageInterface::attributeFunctionMap();
  120. if ( isset( $attributeFunctionMap[$name] ) )
  121. {
  122. $function = $attributeFunctionMap[$name];
  123. if ( method_exists( $this, $function ) )
  124. return $this->$function();
  125. eZDebug::writeWarning( 'The member function $function was not found for attribute $name', __METHOD__ );
  126. return null;
  127. }
  128. eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
  129. return null;
  130. }
  131. /*!
  132. \return true if the image object has been processed, this means that
  133. image has been rendered.
  134. */
  135. function isProcessed()
  136. {
  137. return $this->IsProcessed;
  138. }
  139. /*!
  140. \return true if the width and height of the image has been set.
  141. */
  142. function &hasSize()
  143. {
  144. $hasSize = ( $this->Width !== false and $this->Height !== false );
  145. return $hasSize;
  146. }
  147. /*!
  148. \return the path to the image file including the file.
  149. */
  150. function &imagePath()
  151. {
  152. $imagePath = $this->StoredPath . '/' . $this->StoredFile;
  153. return $imagePath;
  154. }
  155. /*!
  156. Sets the alternative text to \a $text, it will be used for describing the
  157. image and can be used by browsers that cannot view images.
  158. */
  159. function setAlternativeText( $text )
  160. {
  161. $this->AlternativeText = $text;
  162. }
  163. /*!
  164. \return the alternative text for the image.
  165. \sa setAlternativeText
  166. */
  167. function alternativeText()
  168. {
  169. return $this->AlternativeText;
  170. }
  171. /*!
  172. \protected
  173. Registers the GD image object \a $image for destruction upon script end.
  174. This makes sure that image resources are cleaned up after use.
  175. \return a reference for the image which can be used in unregisterImage later on. Returns false if resource can't be registered.
  176. */
  177. static function registerImage( $image )
  178. {
  179. if ( !is_resource( $image ) or get_resource_type( $image ) != 'gd' )
  180. return false;
  181. $imageObjectRef = md5( microtime() );
  182. $createdImageArray =& $GLOBALS['eZImageCreatedArray'];
  183. if ( !is_array( $createdImageArray ) )
  184. {
  185. $createdImageArray = array();
  186. register_shutdown_function( 'eZGlobalImageCleanupFunction' );
  187. }
  188. $createdImageArray[$imageObjectRef] = $image;
  189. return $imageObjectRef;
  190. }
  191. /*!
  192. Tries to unregister the image with reference \a $imageRef
  193. */
  194. static function unregisterImage( $imageRef )
  195. {
  196. $createdImageArray =& $GLOBALS['eZImageCreatedArray'];
  197. if ( !is_array( $createdImageArray ) )
  198. return;
  199. if ( !isset( $createdImageArray[$imageRef] ) )
  200. return;
  201. unset( $createdImageArray[$imageRef] );
  202. }
  203. /*!
  204. Cleans up all registered images.
  205. */
  206. static function cleanupRegisteredImages()
  207. {
  208. $createdImageArray =& $GLOBALS['eZImageCreatedArray'];
  209. if ( !is_array( $createdImageArray ) )
  210. return;
  211. foreach ( array_keys( $createdImageArray ) as $createImageKey )
  212. {
  213. $createdImage = $createdImageArray[$createImageKey];
  214. if ( is_resource( $createdImage ) and get_resource_type( $createdImage ) == 'gd' )
  215. ImageDestroy( $createdImage );
  216. }
  217. }
  218. /*!
  219. Tries to load the PNG image from the path \a $storedPath and file \a $storedFile into
  220. the current image object.
  221. \return true if succesful.
  222. */
  223. function loadPNG( $storedPath, $storedFile )
  224. {
  225. if ( !function_exists( 'ImageCreateFromPNG' ) )
  226. return false;
  227. $this->ImageObject = ImageCreateFromPNG( $storedPath . '/' . $storedFile );
  228. if ( $this->ImageObject )
  229. {
  230. $this->ImageObjectRef = eZImageInterface::registerImage( $this->ImageObject );
  231. return true;
  232. }
  233. return false;
  234. }
  235. /*!
  236. Tries to load the JPEG image from the path \a $storedPath and file \a $storedFile into
  237. the current image object.
  238. \return true if succesful.
  239. */
  240. function loadJPEG( $storedPath, $storedFile )
  241. {
  242. $this->ImageObject = ImageCreateFromJPEG( $storedPath . '/' . $storedFile );
  243. if ( $this->ImageObject )
  244. {
  245. $this->ImageObjectRef = eZImageInterface::registerImage( $this->ImageObject );
  246. return true;
  247. }
  248. return false;
  249. }
  250. /*!
  251. Tries to load the GIF image from the path \a $storedPath and file \a $storedFile into
  252. the current image object.
  253. \return true if succesful.
  254. */
  255. function loadGIF( $storedPath, $storedFile )
  256. {
  257. if ( !function_exists( 'ImageCreateFromGIF' ) )
  258. return false;
  259. $this->ImageObject = ImageCreateFromGIF( $storedPath . '/' . $storedFile );
  260. if ( $this->ImageObject )
  261. {
  262. $this->ImageObjectRef = eZImageInterface::registerImage( $this->ImageObject );
  263. return true;
  264. }
  265. return false;
  266. }
  267. /*!
  268. Tries to load the stored image set by setStoredFile().
  269. If the stored type is not set it will try all formats until one succeeds.
  270. \return true if succesful.
  271. */
  272. function load()
  273. {
  274. if ( $this->ImageObject !== null and
  275. $this->ImageObjectRef !== null )
  276. return;
  277. switch( $this->StoredType )
  278. {
  279. case 'png':
  280. {
  281. return $this->loadPNG( $this->StoredPath, $this->StoredFile );
  282. } break;
  283. case 'jpg':
  284. {
  285. return $this->loadJPEG( $this->StoredPath, $this->StoredFile );
  286. } break;
  287. case 'gif':
  288. {
  289. return $this->loadGIF( $this->StoredPath, $this->StoredFile );
  290. } break;
  291. default:
  292. {
  293. if ( @$this->loadPNG( $this->StoredPath, $this->StoredFile ) )
  294. return true;
  295. else if ( @$this->loadJPEG( $this->StoredPath, $this->StoredFile ) )
  296. return true;
  297. else if ( @$this->loadGIF( $this->StoredPath, $this->StoredFile ) )
  298. return true;
  299. eZDebug::writeError( 'Image format not supported: ' . $this->StoredType, __METHOD__ );
  300. }
  301. }
  302. return false;
  303. }
  304. /*!
  305. Cleans up the current image object if it is set.
  306. */
  307. function destroy()
  308. {
  309. if ( $this->ImageObjectRef === null )
  310. return;
  311. if ( is_resource( $this->ImageObject ) and get_resource_type( $this->ImageObject ) == 'gd' )
  312. ImageDestroy( $this->ImageObject );
  313. eZImageInterface::unregisterImage( $this->ImageObjectRef );
  314. unset( $this->ImageObject );
  315. $this->ImageObject = null;
  316. $this->ImageObjectRef = null;
  317. }
  318. /*!
  319. \return the current image object, if \a $createMissing is true if will
  320. run the image processing to make sure it is created.
  321. Returns \c null if no image is available.
  322. \sa imageObjectInternal
  323. */
  324. function imageObject( $createMissing = true )
  325. {
  326. if ( $this->ImageObject === null or
  327. $this->ImageObjectRef === null )
  328. {
  329. if ( $createMissing )
  330. {
  331. if ( $this->StoredFile != '' )
  332. {
  333. $this->process();
  334. }
  335. }
  336. }
  337. return $this->ImageObject;
  338. }
  339. /*!
  340. \protected
  341. \return the current image object, will create an empty image object if \a $createMissing
  342. is true and the image object is not already created.
  343. \sa imageObject
  344. */
  345. function imageObjectInternal( $createMissing = true )
  346. {
  347. if ( $this->ImageObject === null or
  348. $this->ImageObjectRef === null )
  349. {
  350. if ( $createMissing )
  351. $this->create( $this->Width, $this->Height );
  352. }
  353. return $this->ImageObject;
  354. }
  355. /*!
  356. Makes sure the image object is processed and rendered.
  357. Calls processImage() which is implemented by all descendants of this class to do the real work.
  358. */
  359. function process()
  360. {
  361. if ( $this->processImage() )
  362. $this->IsProcessed = true;
  363. }
  364. /*!
  365. \virtual
  366. Tries to render an image onto the image object, each inheriting class must override this to do
  367. somethign sensible. By default it will try to load the stored image if one is set.
  368. \return true if the image was succesfully processed.
  369. */
  370. function processImage()
  371. {
  372. if ( $this->StoredFile == '' )
  373. return true;
  374. $fileArray = array( $this->StoredPath, $this->StoredFile );
  375. $filePath = eZDir::path( $fileArray );
  376. $imageinfo = getimagesize( $filePath );
  377. if ( $imageinfo )
  378. {
  379. $width = $imageinfo[0];
  380. $height = $imageinfo[1];
  381. if ( $this->load() )
  382. {
  383. $this->Width = $width;
  384. $this->Height = $height;
  385. return true;
  386. }
  387. else
  388. eZDebug::writeWarning( "Image failed to load '$filePath'", __METHOD__ );
  389. }
  390. else
  391. eZDebug::writeWarning( "No image info could be extracted from '$filePath'", __METHOD__ );
  392. return false;
  393. }
  394. /*!
  395. Stores the current image object to disk, the image is stored in the path \a $filePath with filename \a $fileName.
  396. The parameter \a $type determines the image format, supported are \c png and \c jpg.
  397. \return true if the image was stored correctly.
  398. */
  399. function store( $fileName, $filePath, $type )
  400. {
  401. if ( !$this->IsProcessed )
  402. $this->process();
  403. $imageObject = $this->imageObject();
  404. switch( $type )
  405. {
  406. case 'png':
  407. {
  408. if ( !file_exists( $filePath ) )
  409. {
  410. eZDir::mkdir( $filePath, false, true );
  411. }
  412. $fileFullPath = eZDir::path( array( $filePath, $fileName ) );
  413. ImagePNG( $imageObject, $fileFullPath );
  414. $this->StoredPath = $filePath;
  415. $this->StoredFile = $fileName;
  416. $this->StoredType = $type;
  417. return true;
  418. } break;
  419. case 'jpg':
  420. {
  421. if ( !file_exists( $filePath ) )
  422. {
  423. eZDir::mkdir( $filePath, false, true );
  424. }
  425. ImageJPEG( $imageObject, eZDir::path( array( $filePath, $fileName ) ) );
  426. $this->StoredPath = $filePath;
  427. $this->StoredFile = $fileName;
  428. $this->StoredType = $type;
  429. return true;
  430. } break;
  431. default:
  432. {
  433. eZDebug::writeError( 'Image format not supported: ' . $type, __METHOD__ );
  434. }
  435. }
  436. return false;
  437. }
  438. /*!
  439. \static
  440. \return true if GD2 is installed.
  441. */
  442. static function hasGD2()
  443. {
  444. $imageINI = eZINI::instance( 'image.ini' );
  445. return $imageINI->variable( 'GDSettings', 'HasGD2' ) == 'true';
  446. // $testGD = get_extension_funcs( "gd" ); // Grab function list
  447. // if ( !$testGD )
  448. // {
  449. // // echo "GD not even installed.";
  450. // return false;
  451. // }
  452. // if ( in_array( "imagegd2",
  453. // $testGD ) )
  454. // return true;
  455. // return false;
  456. }
  457. /*!
  458. \static
  459. \private
  460. Creates an image with size \a $width and \a $height using GD and returns it.
  461. */
  462. static function createImage( $width, $height, &$useTruecolor )
  463. {
  464. if ( $useTruecolor === null )
  465. {
  466. // print( "has GD2='" . eZImageInterface::hasGD2() . "'<br/>" );
  467. $useTruecolor = eZImageInterface::hasGD2();
  468. }
  469. if ( $useTruecolor and
  470. !function_exists( 'ImageCreateTrueColor' ) )
  471. {
  472. eZDebug::writeWarning( 'Function ImageCreateTrueColor does not exist, cannot create true color images', __METHOD__ );
  473. $useTruecolor = false;
  474. }
  475. if ( $useTruecolor )
  476. $imageObject = ImageCreateTrueColor( $width, $height );
  477. else
  478. $imageObject = ImageCreate( $width, $height );
  479. return $imageObject;
  480. }
  481. /*!
  482. Creates a new image object with width \a $width and height \a $height.
  483. \a $useTruecolor determines the type of image, if \c true it will be truecolor,
  484. if \c false it will be palette based or if \c null it will create it depending on
  485. the GD version. GD 2 will get truecolor while < 2 will get palette based.
  486. */
  487. function create( $width, $height, $useTruecolor = null )
  488. {
  489. if ( $this->ImageObject !== null and
  490. $this->ImageObjectRef !== null )
  491. {
  492. $this->destroy();
  493. }
  494. unset( $this->ImageObject );
  495. $this->ImageObject = eZImageInterface::createImage( $width, $height, $useTruecolor );
  496. $this->Width = $width;
  497. $this->Height = $height;
  498. // eZDebug::writeDebug( $this->ImageObject, 'create' );
  499. $this->IsTrueColor = $useTruecolor;
  500. $this->ImageObjectRef = eZImageInterface::registerImage( $this->ImageObject );
  501. }
  502. /*!
  503. Copies the image from \a $image as the current image object.
  504. */
  505. function __clone()
  506. {
  507. $this->cloneImage( $this->imageObject(), $this->width(), $this->height(),
  508. $this->isTruecolor() );
  509. }
  510. /*!
  511. Clones the image object \a $imageObject with width \a $width, height \a $height
  512. and truecolor settings \a $useTruecolor.
  513. */
  514. function cloneImage( $imageObject, $width, $height, $useTruecolor = null )
  515. {
  516. if ( $this->ImageObject !== null and
  517. $this->ImageObjectRef !== null )
  518. {
  519. $this->destroy();
  520. }
  521. $this->ImageObject = eZImageInterface::createImage( $width, $height, $useTruecolor );
  522. $this->IsTrueColor = $useTruecolor;
  523. $this->ImageObjectRef = eZImageInterface::registerImage( $this->ImageObject );
  524. ImageCopy( $this->ImageObject, $imageObject, 0, 0, 0, 0, $width, $height );
  525. }
  526. /*!
  527. \return the current width of the image or \a false if no size has been set.
  528. */
  529. function width()
  530. {
  531. return $this->Width;
  532. }
  533. /*!
  534. \return the current height of the image or \a false if no size has been set.
  535. */
  536. function height()
  537. {
  538. return $this->Height;
  539. }
  540. /*!
  541. Sets the width of the image to \a $w.
  542. */
  543. function setWidth( $w )
  544. {
  545. $this->Width = $w;
  546. }
  547. /*!
  548. Sets the height of the image to \a $h.
  549. */
  550. function setHeight( $h )
  551. {
  552. $this->Height = $h;
  553. }
  554. /*!
  555. Sets the path, file and type of the stored file.
  556. These settings will be used by load().
  557. */
  558. function setStoredFile( $file, $path, $type )
  559. {
  560. $this->StoredFile = $file;
  561. $this->StoredPath = $path;
  562. $this->StoredType = $type;
  563. }
  564. /*!
  565. Sets the current font object to \a $font.
  566. */
  567. function setFont( $font )
  568. {
  569. $this->Font = $font;
  570. }
  571. /*!
  572. \return the current font object or \c null if not font object has been set.
  573. */
  574. function font()
  575. {
  576. return $this->Font;
  577. }
  578. /*!
  579. Copies the image \a $imageObject with size \a $sourceWidth and \a $sourceHeight
  580. and position \a $sourceX and \a $sourceY onto the destination image \a $destinationImageObject
  581. at position \a $destinationX and \a $destinationY.
  582. */
  583. function copyImage( $destinationImageObject, $imageObject,
  584. $destinationX, $destinationY,
  585. $sourceWidth, $sourceHeight, $sourceX = 0, $sourceY = 0 )
  586. {
  587. ImageCopy( $destinationImageObject, $imageObject,
  588. $destinationX, $destinationY,
  589. $sourceX, $sourceY, $sourceWidth, $sourceHeight );
  590. }
  591. /*!
  592. Merges the image \a $imageObject with size \a $sourceWidth and \a $sourceHeight
  593. and position \a $sourceX and \a $sourceY with the destination image \a $destinationImageObject
  594. at position \a $destinationX and \a $destinationY.
  595. The merged image is placed on the \a $destinationImageObject.
  596. \param $transparency determines how transparent the source image is. 0 is the same as copyImage
  597. and 100 is the same is no copy is made.
  598. */
  599. function mergeImage( $destinationImageObject, $imageObject,
  600. $destinationX, $destinationY,
  601. $sourceWidth, $sourceHeight, $sourceX = 0, $sourceY = 0,
  602. $transparency = 0 )
  603. {
  604. $percent = 100 - $transparency;
  605. ImageCopyMerge( $destinationImageObject, $imageObject,
  606. $destinationX, $destinationY,
  607. $sourceX, $sourceY, $sourceWidth, $sourceHeight,
  608. $percent );
  609. }
  610. /*!
  611. Alpha blends the image \a $imageObject with size \a $sourceWidth and \a $sourceHeight
  612. and position \a $sourceX and \a $sourceY onto the destination image \a $destinationImageObject
  613. at position \a $destinationX and \a $destinationY.
  614. \note This required GD2 and uses color 0 (black) for blending.
  615. */
  616. function blendImage( $destinationImageObject, $imageObject,
  617. $destinationX, $destinationY,
  618. $sourceWidth, $sourceHeight, $sourceX = 0, $sourceY = 0 )
  619. {
  620. ImageAlphaBlending( $destinationImageObject, true );
  621. ImageCopy( $destinationImageObject, $imageObject,
  622. $destinationX, $destinationY,
  623. $sourceX, $sourceY, $sourceWidth, $sourceHeight );
  624. }
  625. function merge( $imageObject, $x, $y, $width, $height )
  626. {
  627. if ( $this->ImageObject === null or
  628. $this->ImageObjectRef === null )
  629. return false;
  630. // ImageAlphaBlending( $this->ImageObject, true );
  631. imagecolortransparent( $imageObject, 0 );
  632. // ImageCopy( $this->ImageObject, $imageObject, $x, $y, 0, 0, $width, $height );
  633. ImageCopyMerge( $this->ImageObject, $imageObject, $x, $y, 0, 0, $width, $height, 50 );
  634. }
  635. /*!
  636. Clears the image object with color \a $color.
  637. If \a $color is not specified it will use the first color set.
  638. */
  639. function clear( $color = false )
  640. {
  641. if ( $color === false )
  642. {
  643. if ( count( $this->PaletteIndex ) > 0)
  644. $color = $this->PaletteIndex[0];
  645. else
  646. $color = 'bgcol';
  647. }
  648. if ( is_string( $color ) )
  649. $color = $this->color( $color );
  650. ImageFilledRectangle( $this->ImageObject, 0, 0, $this->Width, $this->Height, $color );
  651. }
  652. /*!
  653. Allocates the color \a $red, \a $green and \a $blue with name \a $name and returns it.
  654. Will return the palette index for palette based images and the color value for true color.
  655. */
  656. function allocateColor( $name, $red, $green, $blue )
  657. {
  658. if ( isset( $this->Palette[$name] ) )
  659. {
  660. eZDebug::writeError( 'Color already defined: ' . $name, __METHOD__ );
  661. return null;
  662. }
  663. $red = max( 0, min( 255, $red ) );
  664. $green = max( 0, min( 255, $green ) );
  665. $blue = max( 0, min( 255, $blue ) );
  666. $color = ImageColorAllocate( $this->ImageObject, $red, $green, $blue );
  667. $this->Palette[$name] = $color;
  668. $this->PaletteIndex[] = $color;
  669. return $color;
  670. }
  671. /*!
  672. \return the color for the name \a $name.
  673. */
  674. function color( $name )
  675. {
  676. if ( !isset( $this->Palette[$name] ) )
  677. {
  678. eZDebug::writeError( 'Color not defined: ' . $name, __METHOD__ );
  679. return null;
  680. }
  681. return $this->Palette[$name];
  682. }
  683. /*!
  684. \return the color used for text drawing.
  685. */
  686. function textColor()
  687. {
  688. return $this->TextColor;
  689. }
  690. /*!
  691. Sets the color used for text drawing to \a $textColor.
  692. */
  693. function setTextColor( $textColor )
  694. {
  695. $this->TextColor = $textColor;
  696. }
  697. /*!
  698. Draws the text \a $text using the font \a $font and color \a $textColor
  699. at position \a $x and \a $y with angle \a $angle.
  700. If \a $imageObject is specified it will use that for drawing instead of
  701. the current image.
  702. */
  703. function drawText( &$font, $textColor, $text, $x, $y, $angle,
  704. $imageObject = null )
  705. {
  706. if ( !$font )
  707. {
  708. eZDebug::writeWarning( 'Cannot render text, no font is set', __METHOD__ );
  709. return false;
  710. }
  711. if ( !$textColor )
  712. {
  713. eZDebug::writeWarning( 'Cannot render text, no text color is set', __METHOD__ );
  714. return false;
  715. }
  716. if ( is_string( $textColor ) )
  717. $textColor = $this->color( $textColor );
  718. if ( $textColor === null )
  719. {
  720. eZDebug::writeWarning( 'Cannot render text, invalid text color', __METHOD__ );
  721. return false;
  722. }
  723. $x += $font->xAdjustment();
  724. $y += $font->yAdjustment();
  725. if ( $imageObject === null )
  726. $imageObject = $this->ImageObject;
  727. ImageTTFText( $imageObject, $font->pointSize(), $angle, $x, $y,
  728. $textColor, $font->realFile(), $text );
  729. }
  730. /// \privatesection
  731. public $Width;
  732. public $Height;
  733. public $Font;
  734. public $ImageObject;
  735. public $ImageObjectRef;
  736. public $StoredFile;
  737. public $StoredPath;
  738. public $StoredType;
  739. public $PaletteIndex;
  740. public $Palette;
  741. public $AlternativeText;
  742. public $IsTrueColor;
  743. public $IsProcessed;
  744. }
  745. /*!
  746. Global function for eZImageInterface. It will be called at the end of the script execution
  747. and will cleanup all images left behind.
  748. */
  749. function eZGlobalImageCleanupFunction()
  750. {
  751. eZImageInterface::cleanupRegisteredImages();
  752. }
  753. ?>