BxDolTranscoderImage.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolTranscoder');
  10. bx_import('BxDolTranscoderImageQuery');
  11. /**
  12. * @page objects
  13. * @section transcoder_images Images Transcoder
  14. * @ref BxDolTranscoderImage
  15. */
  16. /**
  17. * This class transcodes images on the fly.
  18. * Transcoded image is saved in the specified storage engine and next time ready image is served.
  19. *
  20. *
  21. * To add image transcoder object add record to 'sys_objects_transcoder' table:
  22. * - object - name of the transcoder object, in the format: vendor prefix, underscore, module prefix, underscore, image size name; for example: bx_images_thumb.
  23. * - storage_object - name of the storage object to store transcoded data, the specified storage object need to be created too, @see BxDolStorage.
  24. * - source_type - type of the source, where is source image is taken from, available options 'Storage' and 'Folder' for now.
  25. * - 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.
  26. * - private - how to store transcoded data:
  27. * - no - store transcoded data publicly.
  28. * - yes - store transcoded data privately.
  29. * - auto - detect automatically, not supported for 'Folder' source type.
  30. * - atime_tracking - track last access time to the transcoded data, allowed values 0 - disables or 1 - enabled.
  31. * - 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
  32. * - ts - unix timestamp of the last change of transcoder parameters, if transcoded image is older than this value - image is deleted and transcoded again.
  33. *
  34. *
  35. * Then you need to add image filters to 'sys_transcoder_filters' table:
  36. * - transcoder_object - name of the transcoded object to apply filter to.
  37. * - filter - filter name, please read futher for available filters.
  38. * - filter_params - serialized array of filter params, please read futher for particular filters params.
  39. * - order - if there are several filters for one object, they will be applied in this order.
  40. *
  41. *
  42. * 'Folder' source types:
  43. * This source type is some folder with original images for the transcoding, the identifier of the image (handler) is file name.
  44. * The params are the following:
  45. * - path - path to the folder with original images
  46. * This source type has some limitation:
  47. * - automatic detection of private files is not supported
  48. * - transcoded file is not automaticlaly deleted/renewed if original file is changed
  49. *
  50. *
  51. * 'Storage' source type:
  52. * The source of original files is Storage engine, the identifier of the image (handler) is file id.
  53. * The params are the following:
  54. * - object - name of the Storage object
  55. *
  56. *
  57. * Available filters:
  58. * - Resize - this filter resizes original image, the parameters are the following:
  59. * - w - width of resulted image.
  60. * - h - height of resulted image.
  61. * - square_resize - make resulted image square, even of original image is not square, 'w' and 'h' parameters must be the same.
  62. * - force_type - always change type of the image to the specified type: jpg, png, gif.
  63. * - Grayscale - make image grayscale, there is no parameters for this filter
  64. *
  65. *
  66. * Automatic deletetion of associated data is supported - in the case if original or transcoded file is deleted,
  67. * but you need to register alert handlers, just call registerHandlers () function to register handler (for example, during module installation)
  68. * and call unregisterHandlers () function to unregister handlers (for example, during module uninstallation)
  69. *
  70. *
  71. * Example of usage:
  72. * @code
  73. * bx_import('BxDolTranscoderImage');
  74. * $oTranscoder = BxDolTranscoderImage::getObjectInstance('bx_images_thumb'); // change images transcode object name to your own
  75. * $oTranscoder->registerHandlers(); // make sure to call it only once! before the first usage, no need to call it every time
  76. * $sTranscodedImageUrl = $oTranscoder->getFileUrl('my_dog.jpg'); // the name of file, in the case of 'Folder' storage type this is file name
  77. * echo 'My dog : <img src="' . $sUrl . '" />'; // transcoded(resized and/or grayscaled) image will be shown, according to the specified filters
  78. * @endcode
  79. *
  80. */
  81. class BxDolTranscoderImage extends BxDolTranscoder implements iBxDolFactoryObject
  82. {
  83. protected function __construct($aObject, $oStorage)
  84. {
  85. parent::__construct($aObject, $oStorage);
  86. $this->_oDb = new BxDolTranscoderImageQuery($aObject, false);
  87. $this->_sQueueTable = $this->_oDb->getQueueTable();
  88. }
  89. /**
  90. * Get file url when file isn't transcoded yet
  91. */
  92. public function getFileUrlNotReady($mixedHandler)
  93. {
  94. return BX_DOL_URL_ROOT . 'image_transcoder.php?o=' . $this->_aObject['object'] . '&h=' . $mixedHandler . '&dpx=' . $this->getDevicePixelRatio() . '&t=' . time();
  95. }
  96. protected function applyFilter_Grayscale ($sFile, $aParams)
  97. {
  98. bx_import ('BxDolImageResize');
  99. $o = BxDolImageResize::getInstance();
  100. $o->removeCropOptions ();
  101. $this->_checkForceType ($o, $aParams);
  102. if (IMAGE_ERROR_SUCCESS == $o->grayscale($sFile))
  103. return true;
  104. return false;
  105. }
  106. protected function applyFilter_Resize ($sFile, $aParams)
  107. {
  108. bx_import ('BxDolImageResize');
  109. $o = BxDolImageResize::getInstance();
  110. $o->removeCropOptions ();
  111. if (isset($aParams['w']) && isset($aParams['h']))
  112. $o->setSize ($aParams['w'] * $this->getDevicePixelRatio(), $aParams['h'] * $this->getDevicePixelRatio());
  113. if (isset($aParams['crop_resize']) && $aParams['crop_resize']) {
  114. $o->setAutoCrop (true);
  115. } elseif (isset($aParams['square_resize']) && $aParams['square_resize']) {
  116. $o->setSquareResize (true);
  117. } else {
  118. $o->setSquareResize (false);
  119. }
  120. $this->_checkForceType ($o, $aParams);
  121. if (IMAGE_ERROR_SUCCESS == $o->resize($sFile))
  122. return true;
  123. return false;
  124. }
  125. protected function _checkForceType ($oImageProcessor, $aParams)
  126. {
  127. if (empty($aParams['force_type']))
  128. $aParams['force_type'] = false;
  129. switch ($aParams['force_type']) {
  130. case 'jpeg':
  131. case 'jpg':
  132. $oImageProcessor->setOutputType(IMAGE_TYPE_JPG);
  133. break;
  134. case 'png':
  135. $oImageProcessor->setOutputType(IMAGE_TYPE_PNG);
  136. break;
  137. case 'gif':
  138. $oImageProcessor->setOutputType(IMAGE_TYPE_GIF);
  139. break;
  140. default:
  141. $oImageProcessor->setOutputType(false);
  142. break;
  143. }
  144. }
  145. }
  146. /** @} */