plugin.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. if (!$src) {
  68. return false;
  69. }
  70. $opts = $this->getCurrentOpts($volume);
  71. if (!$this->iaEnabled($opts, $elfinder)) {
  72. return false;
  73. }
  74. $imageType = null;
  75. $srcImgInfo = null;
  76. if (extension_loaded('fileinfo') && function_exists('mime_content_type')) {
  77. $mime = mime_content_type($src);
  78. if (substr($mime, 0, 5) !== 'image') {
  79. return false;
  80. }
  81. }
  82. if (extension_loaded('exif') && function_exists('exif_imagetype')) {
  83. $imageType = exif_imagetype($src);
  84. if ($imageType === false) {
  85. return false;
  86. }
  87. } else {
  88. $srcImgInfo = getimagesize($src);
  89. if ($srcImgInfo === false) {
  90. return false;
  91. }
  92. $imageType = $srcImgInfo[2];
  93. }
  94. // check target image type
  95. if ($imageType !== IMAGETYPE_JPEG) {
  96. return false;
  97. }
  98. if (!$srcImgInfo) {
  99. $srcImgInfo = getimagesize($src);
  100. }
  101. return $this->rotate($volume, $src, $srcImgInfo, $opts['quality']);
  102. }
  103. private function rotate($volume, $src, $srcImgInfo, $quality)
  104. {
  105. if (!function_exists('exif_read_data')) {
  106. return false;
  107. }
  108. $degree = 0;
  109. $errlev =error_reporting();
  110. error_reporting($errlev ^ E_WARNING);
  111. $exif = exif_read_data($src);
  112. error_reporting($errlev);
  113. if ($exif && !empty($exif['Orientation'])) {
  114. switch ($exif['Orientation']) {
  115. case 8:
  116. $degree = 270;
  117. break;
  118. case 3:
  119. $degree = 180;
  120. break;
  121. case 6:
  122. $degree = 90;
  123. break;
  124. }
  125. }
  126. if (!$degree) {
  127. return false;
  128. }
  129. $opts = array(
  130. 'degree' => $degree,
  131. 'jpgQuality' => $quality,
  132. 'checkAnimated' => true
  133. );
  134. return $volume->imageUtil('rotate', $src, $opts);
  135. }
  136. }