plugin.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * elFinder Plugin AutoResize
  4. * Auto resize on file upload.
  5. * ex. binding, configure on connector options
  6. * $opts = array(
  7. * 'bind' => array(
  8. * 'upload.presave' => array(
  9. * 'Plugin.AutoResize.onUpLoadPreSave'
  10. * )
  11. * ),
  12. * // global configure (optional)
  13. * 'plugin' => array(
  14. * 'AutoResize' => array(
  15. * 'enable' => true, // For control by volume driver
  16. * 'maxWidth' => 1024, // Path to Water mark image
  17. * 'maxHeight' => 1024, // Margin right pixel
  18. * 'quality' => 95, // JPEG image save quality
  19. * 'preserveExif' => false, // Preserve EXIF data (Imagick only)
  20. * 'forceEffect' => false, // For change quality or make progressive JPEG of small images
  21. * 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP, // Target image formats ( bit-field )
  22. * 'offDropWith' => null, // Enabled by default. To disable it if it is dropped with pressing the meta key
  23. * // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value
  24. * // In case of using any key, specify it as an array
  25. * 'onDropWith' => null // Disabled by default. To enable it if it is dropped with pressing the meta key
  26. * // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value
  27. * // In case of using any key, specify it as an array
  28. * )
  29. * ),
  30. * // each volume configure (optional)
  31. * 'roots' => array(
  32. * array(
  33. * 'driver' => 'LocalFileSystem',
  34. * 'path' => '/path/to/files/',
  35. * 'URL' => 'http://localhost/to/files/'
  36. * 'plugin' => array(
  37. * 'AutoResize' => array(
  38. * 'enable' => true, // For control by volume driver
  39. * 'maxWidth' => 1024, // Path to Water mark image
  40. * 'maxHeight' => 1024, // Margin right pixel
  41. * 'quality' => 95, // JPEG image save quality
  42. * 'preserveExif' => false, // Preserve EXIF data (Imagick only)
  43. * 'forceEffect' => false, // For change quality or make progressive JPEG of small images
  44. * 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP, // Target image formats ( bit-field )
  45. * 'offDropWith' => null, // Enabled by default. To disable it if it is dropped with pressing the meta key
  46. * // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value
  47. * // In case of using any key, specify it as an array
  48. * 'onDropWith' => null // Disabled by default. To enable it if it is dropped with pressing the meta key
  49. * // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value
  50. * // In case of using any key, specify it as an array
  51. * )
  52. * )
  53. * )
  54. * )
  55. * );
  56. *
  57. * @package elfinder
  58. * @author Naoki Sawada
  59. * @license New BSD
  60. */
  61. class elFinderPluginAutoResize extends elFinderPlugin
  62. {
  63. public function __construct($opts)
  64. {
  65. $defaults = array(
  66. 'enable' => true, // For control by volume driver
  67. 'maxWidth' => 1024, // Path to Water mark image
  68. 'maxHeight' => 1024, // Margin right pixel
  69. 'quality' => 95, // JPEG image save quality
  70. 'preserveExif' => false, // Preserve EXIF data (Imagick only)
  71. 'forceEffect' => false, // For change quality or make progressive JPEG of small images
  72. 'targetType' => IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP, // Target image formats ( bit-field )
  73. 'offDropWith' => null, // To disable it if it is dropped with pressing the meta key
  74. // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value
  75. // In case of using any key, specify it as an array
  76. 'disableWithContentSaveId' => true // Disable on URL upload with post data "contentSaveId"
  77. );
  78. $this->opts = array_merge($defaults, $opts);
  79. }
  80. public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume)
  81. {
  82. if (!$src) {
  83. return false;
  84. }
  85. $opts = $this->getCurrentOpts($volume);
  86. if (!$this->iaEnabled($opts, $elfinder)) {
  87. return false;
  88. }
  89. $imageType = null;
  90. $srcImgInfo = null;
  91. if (extension_loaded('fileinfo') && function_exists('mime_content_type')) {
  92. $mime = mime_content_type($src);
  93. if (substr($mime, 0, 5) !== 'image') {
  94. return false;
  95. }
  96. }
  97. if (extension_loaded('exif') && function_exists('exif_imagetype')) {
  98. $imageType = exif_imagetype($src);
  99. if ($imageType === false) {
  100. return false;
  101. }
  102. } else {
  103. $srcImgInfo = getimagesize($src);
  104. if ($srcImgInfo === false) {
  105. return false;
  106. }
  107. $imageType = $srcImgInfo[2];
  108. }
  109. // check target image type
  110. $imgTypes = array(
  111. IMAGETYPE_GIF => IMG_GIF,
  112. IMAGETYPE_JPEG => IMG_JPEG,
  113. IMAGETYPE_PNG => IMG_PNG,
  114. IMAGETYPE_BMP => IMG_WBMP,
  115. IMAGETYPE_WBMP => IMG_WBMP
  116. );
  117. if (!isset($imgTypes[$imageType]) || !($opts['targetType'] & $imgTypes[$imageType])) {
  118. return false;
  119. }
  120. if (!$srcImgInfo) {
  121. $srcImgInfo = getimagesize($src);
  122. }
  123. if ($opts['forceEffect'] || $srcImgInfo[0] > $opts['maxWidth'] || $srcImgInfo[1] > $opts['maxHeight']) {
  124. return $this->resize($volume, $src, $srcImgInfo, $opts['maxWidth'], $opts['maxHeight'], $opts['quality'], $opts['preserveExif']);
  125. }
  126. return false;
  127. }
  128. private function resize($volume, $src, $srcImgInfo, $maxWidth, $maxHeight, $jpgQuality, $preserveExif)
  129. {
  130. $zoom = min(($maxWidth / $srcImgInfo[0]), ($maxHeight / $srcImgInfo[1]));
  131. $width = round($srcImgInfo[0] * $zoom);
  132. $height = round($srcImgInfo[1] * $zoom);
  133. $unenlarge = true;
  134. $checkAnimated = true;
  135. return $volume->imageUtil('resize', $src, compact('width', 'height', 'jpgQuality', 'preserveExif', 'unenlarge', 'checkAnimated'));
  136. }
  137. }