PageRenderTime 32ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/MediaManager/src/Lib/ElFinder/plugins/AutoResize/plugin.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 201 lines | 130 code | 25 blank | 46 comment | 25 complexity | f27e0e9b7132760fdb108f9b09a000d6 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  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. * 'PluginAutoResize' => 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. * 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP // Target image formats ( bit-field )
  22. * )
  23. * ),
  24. * // each volume configure (optional)
  25. * 'roots' => array(
  26. * array(
  27. * 'driver' => 'LocalFileSystem',
  28. * 'path' => '/path/to/files/',
  29. * 'URL' => 'http://localhost/to/files/'
  30. * 'plugin' => array(
  31. * 'PluginAutoResize' => array(
  32. * 'enable' => true, // For control by volume driver
  33. * 'maxWidth' => 1024, // Path to Water mark image
  34. * 'maxHeight' => 1024, // Margin right pixel
  35. * 'quality' => 95, // JPEG image save quality
  36. * 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP // Target image formats ( bit-field )
  37. * )
  38. * )
  39. * )
  40. * )
  41. * );
  42. *
  43. * @package elfinder
  44. * @author Naoki Sawada
  45. * @license New BSD
  46. */
  47. class elFinderPluginAutoResize {
  48. private $opts = array();
  49. public function __construct($opts) {
  50. $defaults = array(
  51. 'enable' => true, // For control by volume driver
  52. 'maxWidth' => 1024, // Path to Water mark image
  53. 'maxHeight' => 1024, // Margin right pixel
  54. 'quality' => 95, // JPEG image save quality
  55. 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP // Target image formats ( bit-field )
  56. );
  57. $this->opts = array_merge($defaults, $opts);
  58. }
  59. public function onUpLoadPreSave(&$path, &$name, $src, $elfinder, $volume) {
  60. $opts = $this->opts;
  61. $volOpts = $volume->getOptionsPlugin('AutoResize');
  62. if (is_array($volOpts)) {
  63. $opts = array_merge($this->opts, $volOpts);
  64. }
  65. if (! $opts['enable']) {
  66. return false;
  67. }
  68. $srcImgInfo = @getimagesize($src);
  69. if ($srcImgInfo === false) {
  70. return false;
  71. }
  72. // check target image type
  73. $imgTypes = array(
  74. IMAGETYPE_GIF => IMG_GIF,
  75. IMAGETYPE_JPEG => IMG_JPEG,
  76. IMAGETYPE_PNG => IMG_PNG,
  77. IMAGETYPE_WBMP => IMG_WBMP,
  78. );
  79. if (! ($opts['targetType'] & $imgTypes[$srcImgInfo[2]])) {
  80. return false;
  81. }
  82. if ($srcImgInfo[0] > $opts['maxWidth'] || $srcImgInfo[1] > $opts['maxHeight']) {
  83. return $this->resize($src, $srcImgInfo, $opts['maxWidth'], $opts['maxHeight'], $opts['quality']);
  84. }
  85. return false;
  86. }
  87. private function resize($src, $srcImgInfo, $maxWidth, $maxHeight, $quality) {
  88. $zoom = min(($maxWidth/$srcImgInfo[0]),($maxHeight/$srcImgInfo[1]));
  89. $width = round($srcImgInfo[0] * $zoom);
  90. $height = round($srcImgInfo[1] * $zoom);
  91. if (class_exists('Imagick')) {
  92. return $this->resize_imagick($src, $width, $height, $quality);
  93. } else {
  94. return $this->resize_gd($src, $width, $height, $quality, $srcImgInfo);
  95. }
  96. }
  97. private function resize_gd($src, $width, $height, $quality, $srcImgInfo) {
  98. switch ($srcImgInfo['mime']) {
  99. case 'image/gif':
  100. if (@imagetypes() & IMG_GIF) {
  101. $oSrcImg = @imagecreatefromgif($src);
  102. } else {
  103. $ermsg = 'GIF images are not supported';
  104. }
  105. break;
  106. case 'image/jpeg':
  107. if (@imagetypes() & IMG_JPG) {
  108. $oSrcImg = @imagecreatefromjpeg($src) ;
  109. } else {
  110. $ermsg = 'JPEG images are not supported';
  111. }
  112. break;
  113. case 'image/png':
  114. if (@imagetypes() & IMG_PNG) {
  115. $oSrcImg = @imagecreatefrompng($src) ;
  116. } else {
  117. $ermsg = 'PNG images are not supported';
  118. }
  119. break;
  120. case 'image/wbmp':
  121. if (@imagetypes() & IMG_WBMP) {
  122. $oSrcImg = @imagecreatefromwbmp($src);
  123. } else {
  124. $ermsg = 'WBMP images are not supported';
  125. }
  126. break;
  127. default:
  128. $oSrcImg = false;
  129. $ermsg = $srcImgInfo['mime'].' images are not supported';
  130. break;
  131. }
  132. if ($oSrcImg && false != ($tmp = imagecreatetruecolor($width, $height))) {
  133. if (!imagecopyresampled($tmp, $oSrcImg, 0, 0, 0, 0, $width, $height, $srcImgInfo[0], $srcImgInfo[1])) {
  134. return false;
  135. }
  136. switch ($srcImgInfo['mime']) {
  137. case 'image/gif':
  138. imagegif($tmp, $src);
  139. break;
  140. case 'image/jpeg':
  141. imagejpeg($tmp, $src, $quality);
  142. break;
  143. case 'image/png':
  144. if (function_exists('imagesavealpha') && function_exists('imagealphablending')) {
  145. imagealphablending($tmp, false);
  146. imagesavealpha($tmp, true);
  147. }
  148. imagepng($tmp, $src);
  149. break;
  150. case 'image/wbmp':
  151. imagewbmp($tmp, $src);
  152. break;
  153. }
  154. imagedestroy($oSrcImg);
  155. imagedestroy($tmp);
  156. return true;
  157. }
  158. return false;
  159. }
  160. private function resize_imagick($src, $width, $height, $quality) {
  161. try {
  162. $img = new imagick($src);
  163. if (strtoupper($img->getImageFormat()) === 'JPEG') {
  164. $img->setImageCompression(imagick::COMPRESSION_JPEG);
  165. $img->setCompressionQuality($quality);
  166. }
  167. $img->resizeImage($width, $height, Imagick::FILTER_LANCZOS, true);
  168. $result = $img->writeImage($src);
  169. $img->clear();
  170. $img->destroy();
  171. return $result ? true : false;
  172. } catch (Exception $e) {
  173. return false;
  174. }
  175. }
  176. }