plugin.php 4.3 KB

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