/pimcore/models/Asset/Image/Thumbnail/Config.php

https://github.com/ngocanh/pimcore · PHP · 379 lines · 202 code · 52 blank · 125 comment · 34 complexity · 7585188f34a5e845694e429119772ca0 MD5 · raw file

  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.pimcore.org/license
  11. *
  12. * @copyright Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
  13. * @license http://www.pimcore.org/license New BSD License
  14. */
  15. class Asset_Image_Thumbnail_Config {
  16. /**
  17. * format of array:
  18. * array(
  19. array(
  20. "method" => "myName",
  21. "arguments" =>
  22. array(
  23. "width" => 345,
  24. "height" => 200
  25. )
  26. )
  27. * )
  28. *
  29. * @var array
  30. */
  31. public $items = array();
  32. /**
  33. * @var string
  34. */
  35. public $name = "";
  36. /**
  37. * @var string
  38. */
  39. public $description = "";
  40. /**
  41. * @var string
  42. */
  43. public $format = "SOURCE";
  44. /**
  45. * @var mixed
  46. */
  47. public $quality = 100;
  48. /**
  49. * @static
  50. * @param $name
  51. * @return Asset_Image_Thumbnail_Config
  52. */
  53. public static function getByName ($name) {
  54. $pipe = new self();
  55. $pipe->setName($name);
  56. if(!$pipe->load()) {
  57. throw new Exception("thumbnail definition : " . $name . " does not exist");
  58. }
  59. return $pipe;
  60. }
  61. /**
  62. * @static
  63. * @return string
  64. */
  65. public static function getWorkingDir () {
  66. $dir = PIMCORE_CONFIGURATION_DIRECTORY . "/imagepipelines";
  67. if(!is_dir($dir)) {
  68. mkdir($dir);
  69. }
  70. return $dir;
  71. }
  72. /**
  73. * @return void
  74. */
  75. public function save () {
  76. $arrayConfig = object2array($this);
  77. $items = $arrayConfig["items"];
  78. $arrayConfig["items"] = array("item" => $items);
  79. $config = new Zend_Config($arrayConfig);
  80. $writer = new Zend_Config_Writer_Xml(array(
  81. "config" => $config,
  82. "filename" => $this->getConfigFile()
  83. ));
  84. $writer->write();
  85. return true;
  86. }
  87. /**
  88. * @return void
  89. */
  90. public function load () {
  91. $configXml = new Zend_Config_Xml($this->getConfigFile());
  92. $configArray = $configXml->toArray();
  93. if(array_key_exists("items",$configArray) && is_array($configArray["items"]["item"])) {
  94. if(array_key_exists("method",$configArray["items"]["item"])) {
  95. $configArray["items"] = array($configArray["items"]["item"]);
  96. } else {
  97. $configArray["items"] = $configArray["items"]["item"];
  98. }
  99. } else {
  100. $configArray["items"] = array("item" => array());
  101. }
  102. foreach ($configArray as $key => $value) {
  103. $setter = "set" . ucfirst($key);
  104. if(method_exists($this, $setter)) {
  105. $this->$setter($value);
  106. }
  107. }
  108. return true;
  109. }
  110. /**
  111. * @return void
  112. */
  113. public function delete() {
  114. if(is_file($this->getConfigFile())) {
  115. unlink($this->getConfigFile());
  116. }
  117. }
  118. /**
  119. * @return string
  120. */
  121. protected function getConfigFile () {
  122. return self::getWorkingDir() . "/" . $this->getName() . ".xml";
  123. }
  124. /**
  125. * @param $name
  126. * @param $parameters
  127. * @return bool
  128. */
  129. public function addItem ($name, $parameters) {
  130. $this->items[] = array(
  131. "method" => $name,
  132. "arguments" => $parameters
  133. );
  134. return true;
  135. }
  136. /**
  137. * @param $name
  138. * @param $parameters
  139. * @return bool
  140. */
  141. public function addItemAt ($position, $name, $parameters) {
  142. array_splice($this->items, $position, 0, array(array(
  143. "method" => $name,
  144. "arguments" => $parameters
  145. )));
  146. return true;
  147. }
  148. /**
  149. * @return void
  150. */
  151. public function resetItems () {
  152. $this->items = array();
  153. }
  154. /**
  155. * @param string $description
  156. */
  157. public function setDescription($description)
  158. {
  159. $this->description = $description;
  160. }
  161. /**
  162. * @return string
  163. */
  164. public function getDescription()
  165. {
  166. return $this->description;
  167. }
  168. /**
  169. * @param array $items
  170. */
  171. public function setItems($items)
  172. {
  173. $this->items = $items;
  174. }
  175. /**
  176. * @return array
  177. */
  178. public function getItems()
  179. {
  180. return $this->items;
  181. }
  182. /**
  183. * @param string $name
  184. */
  185. public function setName($name)
  186. {
  187. $this->name = $name;
  188. }
  189. /**
  190. * @return string
  191. */
  192. public function getName()
  193. {
  194. return $this->name;
  195. }
  196. /**
  197. * @param string $format
  198. */
  199. public function setFormat($format)
  200. {
  201. $this->format = $format;
  202. }
  203. /**
  204. * @return string
  205. */
  206. public function getFormat()
  207. {
  208. return $this->format;
  209. }
  210. /**
  211. * @param mixed $quality
  212. */
  213. public function setQuality($quality)
  214. {
  215. if($quality) {
  216. $this->quality = $quality;
  217. }
  218. }
  219. /**
  220. * @return mixed
  221. */
  222. public function getQuality()
  223. {
  224. return $this->quality;
  225. }
  226. /**
  227. * @static
  228. * @param $config
  229. * @return Asset_Image_Thumbnail_Config
  230. */
  231. public static function getByArrayConfig ($config) {
  232. $pipe = new Asset_Image_Thumbnail_Config();
  233. if($config["format"]) {
  234. $pipe->setFormat($config["format"]);
  235. }
  236. if($config["quality"]) {
  237. $pipe->setQuality($config["quality"]);
  238. }
  239. if($config["items"]) {
  240. $pipe->setItems($config["items"]);
  241. }
  242. // set name
  243. $hash = md5(serialize($config));
  244. $pipe->setName("auto_" . $hash);
  245. return $pipe;
  246. }
  247. /**
  248. * This is just for compatibility, this method will be removed with the next major release
  249. * @depricated
  250. * @static
  251. * @param $config
  252. * @return Asset_Image_Thumbnail_Config
  253. */
  254. public static function getByLegacyConfig ($config) {
  255. $pipe = new Asset_Image_Thumbnail_Config();
  256. $hash = md5(serialize($config));
  257. $pipe->setName("auto_" . $hash);
  258. if($config["format"]) {
  259. $pipe->setFormat($config["format"]);
  260. }
  261. if($config["quality"]) {
  262. $pipe->setQuality($config["quality"]);
  263. }
  264. /*if ($config["cropPercent"]) {
  265. $pipe->addItem("cropPercent", array(
  266. "width" => $config["cropWidth"],
  267. "height" => $config["cropHeight"],
  268. "y" => $config["cropTop"],
  269. "x" => $config["cropLeft"]
  270. ));
  271. }*/
  272. if ($config["cover"]) {
  273. $pipe->addItem("cover", array(
  274. "width" => $config["width"],
  275. "height" => $config["height"],
  276. "positioning" => "center"
  277. ));
  278. }
  279. else if ($config["contain"]) {
  280. $pipe->addItem("contain", array(
  281. "width" => $config["width"],
  282. "height" => $config["height"]
  283. ));
  284. }
  285. else if ($config["frame"]) {
  286. $pipe->addItem("frame", array(
  287. "width" => $config["width"],
  288. "height" => $config["height"]
  289. ));
  290. }
  291. else if ($config["aspectratio"]) {
  292. if ($config["height"] > 0 && $config["width"] > 0) {
  293. $pipe->addItem("contain", array(
  294. "width" => $config["width"],
  295. "height" => $config["height"]
  296. ));
  297. }
  298. else if ($config["height"] > 0) {
  299. $pipe->addItem("scaleByHeight", array(
  300. "height" => $config["height"]
  301. ));
  302. }
  303. else {
  304. $pipe->addItem("scaleByWidth", array(
  305. "width" => $config["width"]
  306. ));
  307. }
  308. }
  309. else {
  310. if(empty($config["width"]) && !empty($config["height"])) {
  311. $pipe->addItem("scaleByHeight", array(
  312. "height" => $config["height"]
  313. ));
  314. } else if (!empty($config["width"]) && empty($config["height"])) {
  315. $pipe->addItem("scaleByWidth", array(
  316. "width" => $config["width"]
  317. ));
  318. } else {
  319. $pipe->addItem("resize", array(
  320. "width" => $config["width"],
  321. "height" => $config["height"]
  322. ));
  323. }
  324. }
  325. return $pipe;
  326. }
  327. }