plugin.php 5.7 KB

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