BxDolTranscoderImage.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * @page objects
  11. * @section transcoder_images Images Transcoder
  12. * @ref BxDolTranscoderImage
  13. */
  14. /**
  15. * This class transcodes images on the fly.
  16. * Transcoded image is saved in the specified storage engine and next time ready image is served.
  17. *
  18. *
  19. * To add image transcoder object add record to 'sys_objects_transcoder' table:
  20. * - object - name of the transcoder object, in the format: vendor prefix, underscore, module prefix, underscore, image size name; for example: bx_images_thumb.
  21. * - storage_object - name of the storage object to store transcoded data, the specified storage object need to be created too, @see BxDolStorage.
  22. * - source_type - type of the source, where is source image is taken from, available options 'Storage' and 'Folder' for now.
  23. * - source_params - source_type params, each source_type can have own set of params, please read futher for more info about particular source_types, serialized array of params is stored here.
  24. * - private - how to store transcoded data:
  25. * - no - store transcoded data publicly.
  26. * - yes - store transcoded data privately.
  27. * - auto - detect automatically, not supported for 'Folder' source type.
  28. * - atime_tracking - track last access time to the transcoded data, allowed values 0 - disables or 1 - enabled.
  29. * - atime_pruning - prune transcoded images by last access time, if last access time of the image is older than atime_pruning seconds - it is deleted, it works when atime_tracking is enabled
  30. * - ts - unix timestamp of the last change of transcoder parameters, if transcoded image is older than this value - image is deleted and transcoded again.
  31. *
  32. *
  33. * Then you need to add image filters to 'sys_transcoder_filters' table:
  34. * - transcoder_object - name of the transcoded object to apply filter to.
  35. * - filter - filter name, please read futher for available filters.
  36. * - filter_params - serialized array of filter params, please read futher for particular filters params.
  37. * - order - if there are several filters for one object, they will be applied in this order.
  38. *
  39. *
  40. * 'Folder' source types:
  41. * This source type is some folder with original images for the transcoding, the identifier of the image (handler) is file name.
  42. * The params are the following:
  43. * - path - path to the folder with original images
  44. * This source type has some limitation:
  45. * - automatic detection of private files is not supported
  46. * - transcoded file is not automaticlaly deleted/renewed if original file is changed
  47. *
  48. *
  49. * 'Storage' source type:
  50. * The source of original files is Storage engine, the identifier of the image (handler) is file id.
  51. * The params are the following:
  52. * - object - name of the Storage object
  53. *
  54. *
  55. * Available filters:
  56. * - Resize - this filter resizes original image, the parameters are the following:
  57. * - w - width of resulted image.
  58. * - h - height of resulted image.
  59. * - square_resize - make resulted image square, even of original image is not square, 'w' and 'h' parameters must be the same.
  60. * - crop_resize - crop image to destination size with filling whole area of destination size.
  61. * - force_type - always change type of the image to the specified type: jpg, png, gif.
  62. * - Grayscale - make image grayscale, there is no parameters for this filter
  63. *
  64. *
  65. * Automatic deletetion of associated data is supported - in the case if original or transcoded file is deleted,
  66. * but you need to register alert handlers, just call registerHandlers () function to register handler (for example, during module installation)
  67. * and call unregisterHandlers () function to unregister handlers (for example, during module uninstallation)
  68. *
  69. *
  70. * Example of usage:
  71. * @code
  72. * $oTranscoder = BxDolTranscoderImage::getObjectInstance('bx_images_thumb'); // change images transcode object name to your own
  73. * $oTranscoder->registerHandlers(); // make sure to call it only once! before the first usage, no need to call it every time
  74. * $sTranscodedImageUrl = $oTranscoder->getFileUrl('my_dog.jpg'); // the name of file, in the case of 'Folder' storage type this is file name
  75. * echo 'My dog : <img src="' . $sUrl . '" />'; // transcoded(resized and/or grayscaled) image will be shown, according to the specified filters
  76. * @endcode
  77. *
  78. */
  79. class BxDolTranscoderImage extends BxDolTranscoder implements iBxDolFactoryObject
  80. {
  81. protected function __construct($aObject, $oStorage)
  82. {
  83. parent::__construct($aObject, $oStorage);
  84. $this->_oDb = new BxDolTranscoderImageQuery($aObject, false);
  85. $this->_sQueueTable = $this->_oDb->getQueueTable();
  86. }
  87. /**
  88. * Get file url when file isn't transcoded yet
  89. */
  90. public function getFileUrlNotReady($mixedHandler)
  91. {
  92. return BX_DOL_URL_ROOT . 'image_transcoder.php?o=' . $this->_aObject['object'] . '&h=' . $mixedHandler . '&dpx=' . $this->getDevicePixelRatio() . '&t=' . time();
  93. }
  94. public function isFileReady ($mixedHandler, $isCheckOutdated = true)
  95. {
  96. if (isAdmin() && false !== $this->getFilterParams('ResizeVar')) { // only operators can apply new image size
  97. $aTranscodedFileData = $this->_oDb->getTranscodedFileData ($mixedHandler);
  98. $x = $this->getCustomResizeDimension ('x');
  99. $y = $this->getCustomResizeDimension ('y');
  100. // if new sizes are provided - delete old image, so new one will be created
  101. if (($x && (!isset($aTranscodedFileData['x']) || $x != $aTranscodedFileData['x'])) || ($y && (!isset($aTranscodedFileData['y']) || $y != $aTranscodedFileData['y']))) {
  102. if (!($iFileId = $this->_oDb->getFileIdByHandler($mixedHandler)))
  103. return false;
  104. if (!($aFile = $this->_oStorage->getFile($iFileId)))
  105. return false;
  106. if (!$this->_oStorage->deleteFile($aFile['id']))
  107. return false;
  108. }
  109. }
  110. return parent::isFileReady ($mixedHandler, $isCheckOutdated);
  111. }
  112. protected function getCustomResizeDimension ($sName)
  113. {
  114. $i = (int)bx_get($sName);
  115. if ($i > 2048) $i = 2048;
  116. if ($i && $i < 16) $i = 16;
  117. return $i;
  118. }
  119. public function transcode ($mixedHandler, $iProfileId = 0)
  120. {
  121. if (!($bRet = parent::transcode ($mixedHandler, $iProfileId)))
  122. return $bRet;
  123. $x = $this->getCustomResizeDimension ('x');
  124. $y = $this->getCustomResizeDimension ('y');
  125. if ($x || $y)
  126. $this->_oDb->updateTranscodedFileData($mixedHandler, array('x' => $x, 'y' => $y));
  127. return $bRet;
  128. }
  129. protected function applyFilter_Grayscale ($sFile, $aParams)
  130. {
  131. $o = BxDolImageResize::getInstance();
  132. $o->removeCropOptions ();
  133. $this->_checkForceImageType ($o, $aParams);
  134. if (IMAGE_ERROR_SUCCESS == $o->grayscale($sFile))
  135. return true;
  136. return false;
  137. }
  138. protected function applyFilter_ResizeVar ($sFile, $aParams)
  139. {
  140. $aParams['w'] = $this->getCustomResizeDimension ('x');
  141. $aParams['h'] = $this->getCustomResizeDimension ('y');
  142. if (!$aParams['w'])
  143. unset($aParams['w']);
  144. if (!$aParams['h'])
  145. unset($aParams['h']);
  146. if (!$aParams['w'] && !$aParams['h'])
  147. return true;
  148. return $this->applyFilter_Resize ($sFile, $aParams);
  149. }
  150. }
  151. /** @} */