PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/programs/widgets/imagecropper.class.php

https://bitbucket.org/cantico/widgets
PHP | 301 lines | 159 code | 32 blank | 110 comment | 0 complexity | 405d0d2c143bc59827574a07521feded MD5 | raw file
  1. <?php
  2. //-------------------------------------------------------------------------
  3. // OVIDENTIA http://www.ovidentia.org
  4. // Ovidentia is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2, or (at your option)
  7. // any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. // See the GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. // USA.
  18. //-------------------------------------------------------------------------
  19. /**
  20. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  21. * @copyright Copyright (c) 2006 by CANTICO ({@link http://www.cantico.fr})
  22. */
  23. //include_once 'base.php';
  24. require_once dirname(__FILE__) . '/inputwidget.class.php';
  25. require_once dirname(__FILE__) . '/containerwidget.class.php';
  26. require_once dirname(__FILE__) . '/image.class.php';
  27. /**
  28. * Constructs a Widget_ColorPicker.
  29. *
  30. * @param string $id The item unique id.
  31. * @return Widget_ColorPicker
  32. */
  33. function Widget_ImageCropper($id = null)
  34. {
  35. return new Widget_ImageCropper($id);
  36. }
  37. /**
  38. * A Widget_ColorPicker is a widget that let the user enter a color hexadecimal code.
  39. * or pick a color throw a dialog
  40. */
  41. class Widget_ImageCropper extends Widget_ContainerWidget implements Widget_Displayable_Interface
  42. {
  43. private $minW = 0;
  44. private $minH = 0;
  45. private $minSelectedW = 0;
  46. private $minSelectedH = 0;
  47. private $ratio = 0;
  48. private $x = null;
  49. private $y = null;
  50. private $width = null;
  51. private $height = null;
  52. private $rotate = null;
  53. private $inputLayout = null;
  54. private $imageWidget = null;
  55. private $visibleLayout = true;
  56. /**
  57. * @param string $id The item unique id.
  58. * @return Widget_ImageCropper
  59. */
  60. public function __construct($id = null)
  61. {
  62. $W = bab_Widgets();
  63. // require_once FUNC_WIDGETS_PHP_PATH . 'flowlayout.class.php';
  64. // require_once FUNC_WIDGETS_PHP_PATH . 'lineedit.class.php';
  65. // require_once FUNC_WIDGETS_PHP_PATH . 'image.class.php';
  66. // require_once FUNC_WIDGETS_PHP_PATH . 'namedcontainer.class.php';
  67. // require_once FUNC_WIDGETS_PHP_PATH . 'hidden.class.php';
  68. bab_Functionality::get('Icons');
  69. $layout = $W->VBoxLayout($id);
  70. $this->inputLayout = $W->NamedContainer();
  71. parent::__construct($id, $layout);
  72. $this->x = $W->LineEdit($this->getId().'_x');
  73. $this->y = $W->LineEdit($this->getId().'_y');
  74. $this->width = $W->LineEdit($this->getId().'_width');
  75. $this->height = $W->LineEdit($this->getId().'_height');
  76. $this->rotate = $W->LineEdit($this->getId().'_rotate');
  77. $this->x->setName('x')->setMetadata('x', $this->x->getId())->setSize(5);
  78. $this->y->setName('y')->setMetadata('y', $this->y->getId())->setSize(5);
  79. $this->width->setName('width')->setMetadata('width', $this->width->getId())->setSize(5);
  80. $this->height->setName('height')->setMetadata('height', $this->height->getId())->setSize(5);
  81. $this->rotate->setName('rotate')->setMetadata('rotate', $this->rotate->getId())->setSize(5);
  82. $this->imageWidget = $W->Image();
  83. $this->imageWidget->setId($this->getId().'_image');
  84. $layout->addItem(
  85. $this->imageWidget->addClass('widget-imagecropper')
  86. )->addItem(
  87. $W->FlowLayout($this->getId().'_buttonlayout')->setHorizontalSpacing(20)->addClass(Func_Icons::ICON_TOP_16)->addItem(
  88. $W->Button(
  89. $this->getId().'_rotateleft'
  90. )->addItem(
  91. $W->VBoxItems()->addClass('icon',Func_Icons::ACTIONS_GO_PREVIOUS)
  92. )->setTitle(widget_translate('Rotate Left'))
  93. )
  94. ->addItem(
  95. $W->Button(
  96. $this->getId().'_rotateright'
  97. )->addItem(
  98. $W->VBoxItems()->addClass('icon',Func_Icons::ACTIONS_GO_NEXT)
  99. )->setTitle(widget_translate('Rotate Right'))
  100. )
  101. )->addItem(
  102. $this->inputLayout->addItem(
  103. $W->FlowLayout($this->getId().'_inputlayout')->setHorizontalSpacing(20)->addItem(
  104. $W->FlowLayout()
  105. ->addItem($this->xLabel = $W->Label(widget_translate('x'). chr(160)))
  106. ->addItem($this->x->setAssociatedLabel($this->xLabel)
  107. )->setVerticalAlign('middle')
  108. )->addItem(
  109. $W->FlowLayout()
  110. ->addItem($this->yLabel = $W->Label(widget_translate('y'). chr(160)))
  111. ->addItem($this->y->setAssociatedLabel($this->yLabel)
  112. )->setVerticalAlign('middle')
  113. )->addItem(
  114. $W->FlowLayout()
  115. ->addItem($this->widthLabel = $W->Label(widget_translate('width'). chr(160)))
  116. ->addItem($this->width->setAssociatedLabel($this->widthLabel)
  117. )->setVerticalAlign('middle')
  118. )->addItem(
  119. $W->FlowLayout()
  120. ->addItem($this->heightLabel = $W->Label(widget_translate('height'). chr(160)))
  121. ->addItem($this->height->setAssociatedLabel($this->heightLabel)
  122. )->setVerticalAlign('middle')
  123. )->addItem(
  124. $W->FlowLayout()
  125. ->addItem($this->rotateLabel = $W->Label(widget_translate('rotate'). chr(160)))
  126. ->addItem($this->rotate->setAssociatedLabel($this->rotateLabel)
  127. )->setVerticalAlign('middle')
  128. )
  129. )
  130. )->setVerticalSpacing(5, 'px');
  131. }
  132. // /**
  133. // * Sets the name of input.
  134. // *
  135. // * @param string $name
  136. // * @return Widget_ImageCropper
  137. // */
  138. // public function setName($name)
  139. // {
  140. // $this->inputLayout->setName($name);
  141. // return $this;
  142. // }
  143. /**
  144. * Sets the image url.
  145. *
  146. * @param string $url
  147. * @return Widget_ImageCropper
  148. */
  149. public function setUrl($url)
  150. {
  151. $this->imageWidget->setUrl($url);
  152. return $this;
  153. }
  154. /**
  155. * Sets the max image cropping size.
  156. *
  157. * @param int $w
  158. * @param int $h
  159. * @return Widget_ImageCropper
  160. */
  161. /*public function setMaxSize($w, $h)
  162. {
  163. $this->maxW = $w;
  164. $this->maxH = $h;
  165. return $this;
  166. }*/
  167. /**
  168. * Sets the min image cropping size.
  169. *
  170. * @param int $w
  171. * @param int $h
  172. * @return Widget_ImageCropper
  173. */
  174. public function setMinSize($w, $h)
  175. {
  176. $this->minW = $w;
  177. $this->minH = $h;
  178. return $this;
  179. }
  180. /**
  181. * Sets the min image cropping size.
  182. *
  183. * @param int $w
  184. * @param int $h
  185. * @return Widget_ImageCropper
  186. */
  187. public function setMinSelectedSize($w, $h)
  188. {
  189. $this->minSelectedW = $w;
  190. $this->minSelectedH = $h;
  191. return $this;
  192. }
  193. /**
  194. * Sets the image cropping ratio.
  195. *
  196. * @param float $ratio
  197. * @return Widget_ImageCropper
  198. */
  199. public function setRatio($ratio)
  200. {
  201. $this->ratio = $ratio;
  202. return $this;
  203. }
  204. /**
  205. * Sets the image input hidden.
  206. *
  207. * @param float $visible
  208. * @return Widget_ImageCropper
  209. */
  210. public function setVisibleLayout($visible = true)
  211. {
  212. $this->visibleLayout = $visible;
  213. return $this;
  214. }
  215. /**
  216. * Sets the image cropping selection.
  217. *
  218. * @param int $x
  219. * @param int $y
  220. * @param int $width
  221. * @param int $height
  222. * @param int $rotate
  223. * @return Widget_ImageCropper
  224. */
  225. public function setValue( $x, $y, $width, $height, $rotate = 0)
  226. {
  227. $this->x->setValue($x);
  228. $this->y->setValue($x);
  229. $this->width->setValue($width);
  230. $this->height->setValue($height);
  231. $this->rotate->setValue($rotate);
  232. return $this;
  233. }
  234. /**
  235. * @see Widget_Item::getClasses()
  236. */
  237. public function getClasses()
  238. {
  239. $classes = parent::getClasses();
  240. $classes[] = 'widget_imagecropper_container';
  241. return $classes;
  242. }
  243. public function display(Widget_Canvas $canvas)
  244. {
  245. $this->imageWidget->setMetadata('minSizeW', $this->minW);
  246. $this->imageWidget->setMetadata('minSizeH', $this->minH);
  247. $this->imageWidget->setMetadata('minSelectedW', $this->minSelectedW);
  248. $this->imageWidget->setMetadata('minSelectedH', $this->minSelectedH);
  249. $this->imageWidget->setMetadata('aspectRatio', $this->ratio);
  250. $this->imageWidget->setMetadata('x', $this->x->getValue());
  251. $this->imageWidget->setMetadata('y', $this->x->getValue());
  252. $this->imageWidget->setMetadata('width', $this->width->getValue());
  253. $this->imageWidget->setMetadata('height', $this->height->getValue());
  254. $this->imageWidget->setMetadata('rotate', $this->rotate->getValue());
  255. $this->imageWidget->setMetadata('visibleInputs', $this->visibleLayout);
  256. $widgetsAddon = bab_getAddonInfosInstance('widgets');
  257. $imageCropperDiv = $canvas->div(
  258. $this->getId(),
  259. $this->getClasses(),
  260. $this->getLayout(),
  261. $this->getCanvasOptions(),
  262. $this->getTitle(),
  263. $this->getAttributes()
  264. ) . $canvas->metadata($this->getId(), $this->getMetadata())
  265. . $canvas->loadAddonScript($this->getId(), $widgetsAddon, 'widgets.imagecropper.jquery.js')
  266. . $canvas->loadAddonStyleSheet($widgetsAddon, 'widgets.imagecropper.css');
  267. return $imageCropperDiv;
  268. }
  269. }