/Admin/plugins/tinymce/plugins/moxiemanager/plugins/AutoFormat/Plugin.php

https://gitlab.com/hoanghung.dev/aloads · PHP · 280 lines · 192 code · 43 blank · 45 comment · 38 complexity · a1c89f4ef4a38d953aa8a699e42714ad MD5 · raw file

  1. <?php
  2. /**
  3. * Plugin.php
  4. *
  5. * Copyright 2003-2013, Moxiecode Systems AB, All rights reserved.
  6. */
  7. /**
  8. * ....
  9. *
  10. * Format parameters:
  11. * %f - Filename.
  12. * %e - Extension.
  13. * %w - Image width.
  14. * %h - Image height.
  15. * %tw - Target width.
  16. * %th - Target height.
  17. * %ow - Original width.
  18. * %oh - Original height.
  19. *
  20. * Example: 320x240|gif=%f_%w_%h.gif,320x240=%f_%w_%h.%e
  21. *
  22. */
  23. class MOXMAN_AutoFormat_Plugin implements MOXMAN_IPlugin {
  24. public function init() {
  25. MOXMAN::getPluginManager()->get("core")->bind("FileAction", "onFileAction", $this);
  26. }
  27. public function onFileAction(MOXMAN_Core_FileActionEventArgs $args) {
  28. switch ($args->getAction()) {
  29. case MOXMAN_Core_FileActionEventArgs::ADD:
  30. if (!isset($args->getData()->format) && !isset($args->getData()->thumb)) {
  31. $this->applyFormat($args->getFile());
  32. }
  33. break;
  34. case MOXMAN_Core_FileActionEventArgs::DELETE:
  35. if (!isset($args->getData()->format) && !isset($args->getData()->thumb)) {
  36. $this->removeFormat($args->getFile());
  37. }
  38. break;
  39. }
  40. }
  41. /**
  42. * Applies formats to an image.
  43. *
  44. * @param MOXMAN_Vfs_IFile $file File to generate images for.
  45. */
  46. public function applyFormat(MOXMAN_Vfs_IFile $file) {
  47. if (!$file->exists() || !MOXMAN_Media_ImageAlter::canEdit($file)) {
  48. return;
  49. }
  50. $config = $file->getConfig();
  51. $format = $config->get("autoformat.rules", "");
  52. $quality = $config->get("autoformat.jpeg_quality", 90);
  53. // @codeCoverageIgnoreStart
  54. if (!$format) {
  55. return;
  56. }
  57. // @codeCoverageIgnoreEnd
  58. $chunks = preg_split('/,/', $format, 0, PREG_SPLIT_NO_EMPTY);
  59. $imageInfo = MOXMAN_Media_MediaInfo::getInfo($file);
  60. $width = $imageInfo["width"];
  61. $height = $imageInfo["height"];
  62. foreach ($chunks as $chunk) {
  63. $parts = explode('=', $chunk);
  64. $actions = array();
  65. $fileName = preg_replace('/\..+$/', '', $file->getName());
  66. $extension = preg_replace('/^.+\./', '', $file->getName());
  67. $targetWidth = $newWidth = $width;
  68. $targetHeight = $newHeight = $height;
  69. $items = explode('|', $parts[0]);
  70. foreach ($items as $item) {
  71. switch ($item) {
  72. case "gif":
  73. case "jpg":
  74. case "jpeg":
  75. case "png":
  76. $extension = $item;
  77. break;
  78. default:
  79. $matches = array();
  80. if (preg_match('/\s?([0-9|\*]+)\s?x([0-9|\*]+)\s?/', $item, $matches)) {
  81. $actions[] = "resize";
  82. $targetWidth = $matches[1];
  83. $targetHeight = $matches[2];
  84. if ($targetWidth == '*') {
  85. // Width is omitted
  86. $targetWidth = floor($width / ($height / $targetHeight));
  87. }
  88. if ($targetHeight == '*') {
  89. // Height is omitted
  90. $targetHeight = floor($height / ($width / $targetWidth));
  91. }
  92. }
  93. }
  94. }
  95. // Scale it
  96. if ($targetWidth != $width || $targetHeight != $height) {
  97. $scale = min($targetWidth / $width, $targetHeight / $height);
  98. $newWidth = $scale > 1 ? $width : floor($width * $scale);
  99. $newHeight = $scale > 1 ? $height : floor($height * $scale);
  100. }
  101. // Build output path
  102. $outPath = $parts[1];
  103. $outPath = str_replace("%f", $fileName, $outPath);
  104. $outPath = str_replace("%e", $extension, $outPath);
  105. $outPath = str_replace("%ow", "" . $width, $outPath);
  106. $outPath = str_replace("%oh", "" . $height, $outPath);
  107. $outPath = str_replace("%tw", "" . $targetWidth, $outPath);
  108. $outPath = str_replace("%th", "" . $targetHeight, $outPath);
  109. $outPath = str_replace("%w", "" . $newWidth, $outPath);
  110. $outPath = str_replace("%h", "" . $newHeight, $outPath);
  111. $outFile = MOXMAN::getFileSystemManager()->getFile($file->getParent(), $outPath);
  112. // Make dirs
  113. $parents = array();
  114. $parent = $outFile->getParentFile();
  115. while ($parent) {
  116. if ($parent->exists()) {
  117. break;
  118. }
  119. $parents[] = $parent;
  120. $parent = $parent->getParentFile();
  121. }
  122. for ($i = count($parents) - 1; $i >= 0; $i--) {
  123. $parents[$i]->mkdir();
  124. $args = new MOXMAN_Core_FileActionEventArgs(MOXMAN_Core_FileActionEventArgs::ADD, $parents[$i]);
  125. $args->getData()->format = true;
  126. MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
  127. }
  128. if (count($actions) > 0) {
  129. foreach ($actions as $action) {
  130. switch ($action) {
  131. case 'resize':
  132. $imageAlter = new MOXMAN_Media_ImageAlter();
  133. $tempFilePath = MOXMAN::getFileSystemManager()->getLocalTempPath($file);
  134. $imageAlter->load($file->exportTo($tempFilePath));
  135. $imageAlter->resize($newWidth, $newHeight);
  136. $outFileTempPath = MOXMAN::getFileSystemManager()->getLocalTempPath($outFile);
  137. $imageAlter->save($outFileTempPath, $quality);
  138. $outFile->importFrom($outFileTempPath);
  139. $args = new MOXMAN_Core_FileActionEventArgs(MOXMAN_Core_FileActionEventArgs::ADD, $outFile);
  140. $args->getData()->format = true;
  141. MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
  142. break;
  143. }
  144. }
  145. } else {
  146. $imageAlter = new MOXMAN_Media_ImageAlter();
  147. $tempFilePath = MOXMAN::getFileSystemManager()->getLocalTempPath($file);
  148. $imageAlter->load($file->exportTo($tempFilePath));
  149. $outFileTempPath = MOXMAN::getFileSystemManager()->getLocalTempPath($outFile);
  150. $imageAlter->save($outFileTempPath, $quality);
  151. $outFile->importFrom($outFileTempPath);
  152. $args = new MOXMAN_Core_FileActionEventArgs(MOXMAN_Core_FileActionEventArgs::ADD, $outFile);
  153. $args->getData()->format = true;
  154. MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args);
  155. }
  156. }
  157. }
  158. /**
  159. * Removes formats from an image.
  160. *
  161. * @param MOXMAN_Vfs_IFile $file File to generate images for.
  162. */
  163. public function removeFormat(MOXMAN_Vfs_IFile $file) {
  164. if (!$file->exists()) {
  165. return;
  166. }
  167. $config = $file->getConfig();
  168. $format = $config->get("autoformat.rules", "");
  169. if ($config->get("autoformat.delete_format_images", true) === false) {
  170. return;
  171. }
  172. // @codeCoverageIgnoreStart
  173. if (!$format) {
  174. return;
  175. }
  176. // @codeCoverageIgnoreEnd
  177. $chunks = preg_split('/,/', $format, 0, PREG_SPLIT_NO_EMPTY);
  178. $imageInfo = MOXMAN_Media_MediaInfo::getInfo($file);
  179. $width = $imageInfo["width"];
  180. $height = $imageInfo["height"];
  181. foreach ($chunks as $chunk) {
  182. $parts = explode('=', $chunk);
  183. $fileName = preg_replace('/\..+$/', '', $file->getName());
  184. $extension = preg_replace('/^.+\./', '', $file->getName());
  185. $targetWidth = $newWidth = $width;
  186. $targetHeight = $newHeight = $height;
  187. $items = explode('|', $parts[0]);
  188. foreach ($items as $item) {
  189. switch ($item) {
  190. case "gif":
  191. case "jpg":
  192. case "jpeg":
  193. case "png":
  194. $extension = $item;
  195. break;
  196. default:
  197. $matches = array();
  198. if (preg_match('/\s?([0-9|\*]+)\s?x([0-9|\*]+)\s?/', $item, $matches)) {
  199. $targetWidth = $matches[1];
  200. $targetHeight = $matches[2];
  201. if ($targetWidth == '*') {
  202. // Width is omitted
  203. $targetWidth = floor($width / ($height / $targetHeight));
  204. }
  205. if ($targetHeight == '*') {
  206. // Height is omitted
  207. $targetHeight = floor($height / ($width / $targetWidth));
  208. }
  209. }
  210. }
  211. }
  212. // Scale it
  213. if ($targetWidth != $width || $targetHeight != $height) {
  214. $scale = min($targetWidth / $width, $targetHeight / $height);
  215. $newWidth = $scale > 1 ? $width : floor($width * $scale);
  216. $newHeight = $scale > 1 ? $height : floor($height * $scale);
  217. }
  218. // Build output path
  219. $outPath = $parts[1];
  220. $outPath = str_replace("%f", $fileName, $outPath);
  221. $outPath = str_replace("%e", $extension, $outPath);
  222. $outPath = str_replace("%ow", "" . $width, $outPath);
  223. $outPath = str_replace("%oh", "" . $height, $outPath);
  224. $outPath = str_replace("%tw", "" . $targetWidth, $outPath);
  225. $outPath = str_replace("%th", "" . $targetHeight, $outPath);
  226. $outPath = str_replace("%w", "" . $newWidth, $outPath);
  227. $outPath = str_replace("%h", "" . $newHeight, $outPath);
  228. $outFile = MOXMAN::getFileSystemManager()->getFile($file->getParent(), $outPath);
  229. if ($outFile->exists()) {
  230. $outFile->delete();
  231. }
  232. }
  233. }
  234. }
  235. // Add plugin
  236. MOXMAN::getPluginManager()->add("autoformat", new MOXMAN_AutoFormat_Plugin());
  237. ?>