PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/users/models/Users/Form/Filter/ImageSize.php

https://code.google.com/p/zfcore/
PHP | 206 lines | 123 code | 26 blank | 57 comment | 18 complexity | 178dc2dc222bdad48000fabcbd21d8d9 MD5 | raw file
  1. <?php
  2. /**
  3. * Users_Model_Users_Form_Filter_ImageSize
  4. *
  5. * @version $id$
  6. */
  7. class Users_Model_Users_Form_Filter_ImageSize implements Zend_Filter_Interface
  8. {
  9. /**
  10. * @var integer
  11. */
  12. protected $_width;
  13. /**
  14. * @var integer
  15. */
  16. protected $_height;
  17. /**
  18. * @var bool
  19. */
  20. protected $_keepRatio = true;
  21. /**
  22. * Adds options to the filter at initiation
  23. *
  24. * @param string $options
  25. */
  26. public function __construct($options = null)
  27. {
  28. $width = null;
  29. $height = null;
  30. $keepRatio = null;
  31. if (!is_array($options)) {
  32. $width = $options;
  33. if (func_num_args() > 1) {
  34. $height = func_get_arg(1);
  35. if (func_num_args() > 2) {
  36. $keepRatio = func_get_arg(2);
  37. }
  38. }
  39. } else {
  40. if (isset($options['width'])) {
  41. $width = $options['width'];
  42. }
  43. if (isset($options['height'])) {
  44. $height = $options['height'];
  45. }
  46. if (isset($options['keepRatio'])) {
  47. $keepRatio = $options['keepRatio'];
  48. }
  49. }
  50. if ($width) {
  51. $this->setWidth($width);
  52. }
  53. if ($height) {
  54. $this->setHeight($height);
  55. }
  56. if (is_bool($keepRatio)) {
  57. $this->setKeepRatio($keepRatio);
  58. }
  59. }
  60. /**
  61. * Set width
  62. *
  63. * @param integer $width
  64. * @return Lizard_Images_Filter_ImageSize
  65. */
  66. public function setWidth($width)
  67. {
  68. $this->_width = (int) $width;
  69. return $this;
  70. }
  71. /**
  72. * Get width
  73. *
  74. * @return integer
  75. */
  76. public function getWidth()
  77. {
  78. return $this->_width;
  79. }
  80. /**
  81. * Set height
  82. *
  83. * @param integer $height
  84. * @return Lizard_Images_Filter_ImageSize
  85. */
  86. public function setHeight($height)
  87. {
  88. $this->_height = (int) $height;
  89. return $this;
  90. }
  91. /**
  92. * Get height
  93. *
  94. * @return integer
  95. */
  96. public function getHeight()
  97. {
  98. return $this->_height;
  99. }
  100. /**
  101. * Set keep ratio
  102. *
  103. * @param boolen $keepRatio
  104. * @return Lizard_Images_Filter_ImageSize
  105. */
  106. public function setKeepRatio($keepRatio = true)
  107. {
  108. $this->_keepRatio = (bool) $keepRatio;
  109. return $this;
  110. }
  111. /**
  112. * Get keep ratio
  113. *
  114. * @return boolen
  115. */
  116. public function getKeepRatio()
  117. {
  118. return $this->_keepRatio;
  119. }
  120. /**
  121. * Resize file to width and height
  122. *
  123. * @param string $value
  124. */
  125. public function filter($value)
  126. {
  127. if (!file_exists($value)) {
  128. throw new Zend_Filter_Exception("File '$value' not found");
  129. }
  130. if (!is_writable($value)) {
  131. throw new Zend_Filter_Exception("File '$value' is not writable");
  132. }
  133. $toWidth = $this->getWidth();
  134. $toHeight = $this->getHeight();
  135. $image = imagecreatefromstring(file_get_contents($value));
  136. $width = imagesx($image);
  137. $height = imagesy($image);
  138. $startx = 0;
  139. $starty = 0;
  140. $resampled = imagecreatetruecolor($toWidth, $toHeight);
  141. if (!$toWidth && $toHeight) {
  142. $toWidth = $width / ($height / $toHeight);
  143. } elseif ($toWidth && !$toHeight) {
  144. $toHeight = $height / ($width / $toWidth);
  145. } elseif (!$toWidth && !$toHeight) {
  146. throw new Lizard_Images_Exception('Destination width and height not set');
  147. } elseif ($this->getKeepRatio()) {
  148. $xscale = $width / $toWidth;
  149. $yscale = $height / $toHeight;
  150. $startx = $toWidth;
  151. $starty = $toHeight;
  152. if ($yscale > $xscale) {
  153. $toWidth = $width / $yscale;
  154. $toHeight = $height / $yscale;
  155. } else {
  156. $toWidth = $width / $xscale;
  157. $toHeight = $height / $xscale;
  158. }
  159. $toWidth = round($toWidth);
  160. $toHeight = round($toHeight);
  161. $startx = ($startx - $toWidth) / 2;
  162. $starty = ($starty - $toHeight) / 2;
  163. }
  164. imagecopyresampled(
  165. $resampled,
  166. $image,
  167. $startx, $starty,
  168. 0, 0,
  169. $toWidth,
  170. $toHeight,
  171. $width,
  172. $height
  173. );
  174. @unlink($value);
  175. $value = pathinfo($value, PATHINFO_DIRNAME) . '/'
  176. . pathinfo($value, PATHINFO_FILENAME) . '.jpg';
  177. imagejpeg($resampled, $value);
  178. return $value;
  179. }
  180. }