BxDolTranscoderImage.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. * - crop_resize - crop image to destination size with filling whole area of destination size.
  63. * - force_type - always change type of the image to the specified type: jpg, png, gif.
  64. * - Grayscale - make image grayscale, there is no parameters for this filter
  65. *
  66. *
  67. * Automatic deletetion of associated data is supported - in the case if original or transcoded file is deleted,
  68. * but you need to register alert handlers, just call registerHandlers () function to register handler (for example, during module installation)
  69. * and call unregisterHandlers () function to unregister handlers (for example, during module uninstallation)
  70. *
  71. *
  72. * Example of usage:
  73. * @code
  74. * bx_import('BxDolTranscoderImage');
  75. * $oTranscoder = BxDolTranscoderImage::getObjectInstance('bx_images_thumb'); // change images transcode object name to your own
  76. * $oTranscoder->registerHandlers(); // make sure to call it only once! before the first usage, no need to call it every time
  77. * $sTranscodedImageUrl = $oTranscoder->getFileUrl('my_dog.jpg'); // the name of file, in the case of 'Folder' storage type this is file name
  78. * echo 'My dog : <img src="' . $sUrl . '" />'; // transcoded(resized and/or grayscaled) image will be shown, according to the specified filters
  79. * @endcode
  80. *
  81. */
  82. class BxDolTranscoderImage extends BxDolTranscoder implements iBxDolFactoryObject
  83. {
  84. protected function __construct($aObject, $oStorage)
  85. {
  86. parent::__construct($aObject, $oStorage);
  87. $this->_oDb = new BxDolTranscoderImageQuery($aObject, false);
  88. $this->_sQueueTable = $this->_oDb->getQueueTable();
  89. }
  90. /**
  91. * Get file url when file isn't transcoded yet
  92. */
  93. public function getFileUrlNotReady($mixedHandler)
  94. {
  95. return BX_DOL_URL_ROOT . 'image_transcoder.php?o=' . $this->_aObject['object'] . '&h=' . $mixedHandler . '&dpx=' . $this->getDevicePixelRatio() . '&t=' . time();
  96. }
  97. protected function applyFilter_Grayscale ($sFile, $aParams)
  98. {
  99. bx_import ('BxDolImageResize');
  100. $o = BxDolImageResize::getInstance();
  101. $o->removeCropOptions ();
  102. $this->_checkForceType ($o, $aParams);
  103. if (IMAGE_ERROR_SUCCESS == $o->grayscale($sFile))
  104. return true;
  105. return false;
  106. }
  107. protected function applyFilter_Resize ($sFile, $aParams)
  108. {
  109. bx_import ('BxDolImageResize');
  110. $o = BxDolImageResize::getInstance();
  111. $o->removeCropOptions ();
  112. if (isset($aParams['w']) && isset($aParams['h']))
  113. $o->setSize ($aParams['w'] * $this->getDevicePixelRatio(), $aParams['h'] * $this->getDevicePixelRatio());
  114. if (isset($aParams['crop_resize']) && $aParams['crop_resize']) {
  115. $o->setAutoCrop (true);
  116. } elseif (isset($aParams['square_resize']) && $aParams['square_resize']) {
  117. $o->setSquareResize (true);
  118. } else {
  119. $o->setSquareResize (false);
  120. }
  121. $this->_checkForceType ($o, $aParams);
  122. if (IMAGE_ERROR_SUCCESS == $o->resize($sFile))
  123. return true;
  124. return false;
  125. }
  126. protected function _checkForceType ($oImageProcessor, $aParams)
  127. {
  128. if (empty($aParams['force_type']))
  129. $aParams['force_type'] = false;
  130. switch ($aParams['force_type']) {
  131. case 'jpeg':
  132. case 'jpg':
  133. $oImageProcessor->setOutputType(IMAGE_TYPE_JPG);
  134. break;
  135. case 'png':
  136. $oImageProcessor->setOutputType(IMAGE_TYPE_PNG);
  137. break;
  138. case 'gif':
  139. $oImageProcessor->setOutputType(IMAGE_TYPE_GIF);
  140. break;
  141. default:
  142. $oImageProcessor->setOutputType(false);
  143. break;
  144. }
  145. }
  146. }
  147. /** @} */