plugin.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * elFinder Plugin AutoRotate
  4. * Auto rotation on file upload of JPEG file by EXIF Orientation.
  5. * ex. binding, configure on connector options
  6. * $opts = array(
  7. * 'bind' => array(
  8. * 'upload.presave' => array(
  9. * 'Plugin.AutoRotate.onUpLoadPreSave'
  10. * )
  11. * ),
  12. * // global configure (optional)
  13. * 'plugin' => array(
  14. * 'AutoRotate' => array(
  15. * 'enable' => true, // For control by volume driver
  16. * 'quality' => 95, // JPEG image save quality
  17. * 'offDropWith' => null, // Enabled by default. To disable it if it is dropped with pressing the meta key
  18. * // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value
  19. * // In case of using any key, specify it as an array
  20. * 'onDropWith' => null // Disabled by default. To enable it if it is dropped with pressing the meta key
  21. * // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value
  22. * // In case of using any key, specify it as an array
  23. * )
  24. * ),
  25. * // each volume configure (optional)
  26. * 'roots' => array(
  27. * array(
  28. * 'driver' => 'LocalFileSystem',
  29. * 'path' => '/path/to/files/',
  30. * 'URL' => 'http://localhost/to/files/'
  31. * 'plugin' => array(
  32. * 'AutoRotate' => array(
  33. * 'enable' => true, // For control by volume driver
  34. * 'quality' => 95, // JPEG image save quality
  35. * 'offDropWith' => null, // Enabled by default. To disable it if it is dropped with pressing the meta key
  36. * // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value
  37. * // In case of using any key, specify it as an array
  38. * 'onDropWith' => null // Disabled by default. To enable it if it is dropped with pressing the meta key
  39. * // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value
  40. * // In case of using any key, specify it as an array
  41. * )
  42. * )
  43. * )
  44. * )
  45. * );
  46. *
  47. * @package elfinder
  48. * @author Naoki Sawada
  49. * @license New BSD
  50. */
  51. class elFinderPluginAutoRotate extends elFinderPlugin
  52. {
  53. public function __construct($opts)
  54. {
  55. $defaults = array(
  56. 'enable' => true, // For control by volume driver
  57. 'quality' => 95, // JPEG image save quality
  58. 'offDropWith' => null, // To disable it if it is dropped with pressing the meta key
  59. // Alt: 8, Ctrl: 4, Meta: 2, Shift: 1 - sum of each value
  60. // In case of using any key, specify it as an array
  61. 'disableWithContentSaveId' => true // Disable on URL upload with post data "contentSaveId"
  62. );
  63. $this->opts = array_merge($defaults, $opts);
  64. }
  65. public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume)
  66. {
  67. $opts = $this->getCurrentOpts($volume);
  68. if (!$this->iaEnabled($opts, $elfinder)) {
  69. return false;
  70. }
  71. $imageType = null;
  72. $srcImgInfo = null;
  73. if (extension_loaded('fileinfo') && function_exists('mime_content_type')) {
  74. $mime = mime_content_type($src);
  75. if (substr($mime, 0, 5) !== 'image') {
  76. return false;
  77. }
  78. }
  79. if (extension_loaded('exif') && function_exists('exif_imagetype')) {
  80. $imageType = exif_imagetype($src);
  81. if ($imageType === false) {
  82. return false;
  83. }
  84. } else {
  85. $srcImgInfo = getimagesize($src);
  86. if ($srcImgInfo === false) {
  87. return false;
  88. }
  89. $imageType = $srcImgInfo[2];
  90. }
  91. // check target image type
  92. if ($imageType !== IMAGETYPE_JPEG) {
  93. return false;
  94. }
  95. if (!$srcImgInfo) {
  96. $srcImgInfo = getimagesize($src);
  97. }
  98. return $this->rotate($volume, $src, $srcImgInfo, $opts['quality']);
  99. }
  100. private function rotate($volume, $src, $srcImgInfo, $quality)
  101. {
  102. if (!function_exists('exif_read_data')) {
  103. return false;
  104. }
  105. $degree = 0;
  106. $exif = exif_read_data($src);
  107. if ($exif && !empty($exif['Orientation'])) {
  108. switch ($exif['Orientation']) {
  109. case 8:
  110. $degree = 270;
  111. break;
  112. case 3:
  113. $degree = 180;
  114. break;
  115. case 6:
  116. $degree = 90;
  117. break;
  118. }
  119. }
  120. $opts = array(
  121. 'degree' => $degree,
  122. 'jpgQuality' => $quality,
  123. 'checkAnimated' => true
  124. );
  125. return $volume->imageUtil('rotate', $src, $opts);
  126. }
  127. }