transcoder.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCoreSamples Samples
  7. * @{
  8. */
  9. /**
  10. * @page samples
  11. * @section transcoder_images Transcoder Images
  12. *
  13. * This sample shows how transcoder can be used for the folder with images.
  14. *
  15. * @code
  16. *
  17. * CREATE TABLE IF NOT EXISTS `bx_resizer_files` (
  18. * `id` int(11) NOT NULL AUTO_INCREMENT,
  19. * `profile_id` int(10) unsigned NOT NULL,
  20. * `remote_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  21. * `path` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  22. * `file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  23. * `mime_type` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
  24. * `ext` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  25. * `size` int(11) NOT NULL,
  26. * `added` int(11) NOT NULL,
  27. * `modified` int(11) NOT NULL,
  28. * `private` int(11) NOT NULL,
  29. * PRIMARY KEY (`id`),
  30. * UNIQUE KEY `remote_id` (`remote_id`)
  31. * );
  32. *
  33. * INSERT INTO `sys_objects_storage` (`object`, `engine`, `params`, `token_life`, `cache_control`, `levels`, `table_files`, `ext_mode`, `ext_allow`, `ext_deny`, `quota_size`, `current_size`, `quota_number`, `current_number`, `max_file_size`, `ts`) VALUES
  34. * ('resizer', 'Local', '', 360, 84000, 0, 'bx_resizer_files', 'deny-allow', '', 'exe,com,bat,pif,scr', 1000000000, 0, 100, 0, 300000000, 0);
  35. *
  36. * INSERT INTO `sys_objects_transcoder` (`object`, `storage_object`, `source_type`, `source_params`, `private`, `atime_tracking`, `atime_pruning`, `ts`) VALUES
  37. * ('sample', 'resizer', 'Folder', 'a:1:{s:4:"path";s:12:"samples/img/";}', 'no', 0, 0, 0);
  38. *
  39. * INSERT INTO `sys_transcoder_filters` (`transcoder_object`, `filter`, `filter_params`, `order`) VALUES
  40. * ('sample', 'Resize', 'a:3:{s:1:"w";i:100;s:1:"h";i:100;s:13:"square_resize";i:0;}', 1),
  41. * ('sample', 'Grayscale', '', 2);
  42. *
  43. * @endcode
  44. *
  45. */
  46. require_once('./../inc/header.inc.php');
  47. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  48. $oTemplate = BxDolTemplate::getInstance();
  49. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  50. $oTemplate->setPageHeader ("Sample transcoder");
  51. $oTemplate->setPageContent ('page_main_code', PageCompMainCode());
  52. $oTemplate->getPageCode();
  53. /**
  54. * page code function
  55. */
  56. function PageCompMainCode()
  57. {
  58. ob_start();
  59. $iProfileId = 123;
  60. $oTranscoder = BxDolTranscoderImage::getObjectInstance('sample');
  61. if (!$oTranscoder)
  62. die('Transcoder object isn\'t defined');
  63. echo "registerHandlers: [" . $oTranscoder->registerHandlers() . "] <hr />\n";
  64. echo "
  65. <script>
  66. $(document).ready(function () {
  67. $('img').on('load', function () {
  68. $(this).after(' <span>size: <b>' + $(this).width() + 'x' + $(this).height() + '</b></span>')
  69. });
  70. });
  71. </script>
  72. ";
  73. $oStorage = $oTranscoder->getStorage();
  74. if (isset($_POST['delete'])) {
  75. foreach ($_POST['file_id'] as $iFileId) {
  76. $bRet = $oStorage->deleteFile($iFileId, $iProfileId);
  77. if ($bRet)
  78. echo "<hr />deleted file id: " . $iFileId . "<hr />";
  79. else
  80. echo "<hr />file deleting error: " . $oStorage->getErrorString() . "<hr />";
  81. }
  82. } else {
  83. $sPath = BX_DIRECTORY_PATH_ROOT . 'samples/img/';
  84. $h = opendir($sPath);
  85. while (false !== ($sFile = readdir($h))) {
  86. if ('.' == $sFile[0] || !is_file($sPath . $sFile))
  87. continue;
  88. $sUrl = $oTranscoder->getFileUrl($sFile);
  89. echo $sFile . ' : <img src="' . $sUrl . '" /> <br /> ' . $sUrl . ' <hr />';
  90. }
  91. }
  92. $a = $oStorage->getFilesAll();
  93. echo "<hr /> <h2>Files List:</h2>";
  94. echo '<form method="POST">';
  95. foreach ($a as $r)
  96. echo '<input type="checkbox" name="file_id[]" value="' . $r['id'] . '" /> ' . $r['file_name'] . '<br />';
  97. echo '<input type="submit" name="delete" value="Delete" />';
  98. echo '</form>';
  99. $s = ob_get_clean();
  100. return DesignBoxContent("Sample transcoder", $s, BX_DB_PADDING_DEF);
  101. }