PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/redaxo/include/addons/image_manager/classes/effects/class.rex_effect_resize.inc.php

https://github.com/spiegel/redaxo4
PHP | 221 lines | 185 code | 25 blank | 11 comment | 27 complexity | 2b2623bfc6a2905e02aa4235e08deb47 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // Todo:
  3. // - Vergrš§ern erlauben oder nicht. aber eher als Modul einsetzen, also
  4. // fit
  5. class rex_effect_resize extends rex_effect_abstract
  6. {
  7. var $options;
  8. var $script;
  9. function rex_effect_resize()
  10. {
  11. $this->options = array('maximum','minimum','exact');
  12. $this->script = '
  13. <script type="text/javascript">
  14. <!--
  15. (function($) {
  16. $(function() {
  17. var $fx_resize_select_style = $("#image_manager_rex_effect_resize_style_select");
  18. var $fx_resize_enlarge = $("#image_manager_rex_effect_resize_allow_enlarge_select").parent().parent();
  19. $fx_resize_select_style.change(function(){
  20. if(jQuery(this).val() == "exact")
  21. {
  22. $fx_resize_enlarge.hide();
  23. }else
  24. {
  25. $fx_resize_enlarge.show();
  26. }
  27. }).change();
  28. });
  29. })(jQuery);
  30. //--></script>';
  31. }
  32. function execute()
  33. {
  34. $gdimage =& $this->image->getImage();
  35. $w = $this->image->getWidth();
  36. $h = $this->image->getHeight();
  37. if(!isset($this->params['style']) || !in_array($this->params['style'],$this->options))
  38. {
  39. $this->params['style'] = 'maximum';
  40. }
  41. // relatives resizen
  42. if(substr(trim($this->params['width']), -1) === '%')
  43. {
  44. $this->params['width'] = round($w * (rtrim($this->params['width'], '%') / 100));
  45. }
  46. if(substr(trim($this->params['height']), -1) === '%')
  47. {
  48. $this->params['height'] = round($h * (rtrim($this->params['height'], '%') / 100));
  49. }
  50. if($this->params['style'] == 'maximum')
  51. {
  52. $this->resizeMax($w, $h);
  53. }else if($this->params['style'] == 'minimum')
  54. {
  55. $this->resizeMin($w, $h);
  56. }else
  57. {
  58. // warp => nichts tun
  59. }
  60. // ----- not enlarge image
  61. if($w <= $this->params['width'] && $h <= $this->params['height'] && $this->params['allow_enlarge'] == "not_enlarge")
  62. {
  63. $this->params['width'] = $w;
  64. $this->params['height'] = $h;
  65. return;
  66. }
  67. if(!isset($this->params["width"]))
  68. {
  69. $this->params["width"] = $w;
  70. }
  71. if(!isset($this->params["height"]))
  72. {
  73. $this->params["height"] = $h;
  74. }
  75. if (function_exists('ImageCreateTrueColor'))
  76. {
  77. $des = @ImageCreateTrueColor($this->params['width'], $this->params['height']);
  78. }else
  79. {
  80. $des = @ImageCreate($this->params['width'], $this->params['height']);
  81. }
  82. if(!$des)
  83. {
  84. return;
  85. }
  86. // Transparenz erhalten
  87. $this->keepTransparent($des);
  88. imagecopyresampled($des, $gdimage, 0, 0, 0, 0, $this->params['width'], $this->params['height'], $w, $h);
  89. $gdimage = $des;
  90. $this->image->refreshDimensions();
  91. }
  92. function resizeMax($w, $h)
  93. {
  94. if (!empty($this->params['height']) && !empty($this->params['width']))
  95. {
  96. $img_ratio = $w / $h;
  97. $resize_ratio = $this->params['width'] / $this->params['height'];
  98. if ($img_ratio >= $resize_ratio)
  99. {
  100. // --- width
  101. $this->params['height'] = ceil ($this->params['width'] / $w * $h);
  102. }else
  103. {
  104. // --- height
  105. $this->params['width'] = ceil ($this->params['height'] / $h * $w);
  106. }
  107. }elseif (!empty($this->params['height']))
  108. {
  109. $img_factor = $h / $this->params['height'];
  110. $this->params['width'] = ceil ($w / $img_factor);
  111. }elseif (!empty($this->params['width']))
  112. {
  113. $img_factor = $w / $this->params['width'];
  114. $this->params['height'] = ceil ($h / $img_factor);
  115. }
  116. }
  117. function resizeMin($w, $h)
  118. {
  119. if (!empty($this->params['height']) && !empty($this->params['width']))
  120. {
  121. $img_ratio = $w / $h;
  122. $resize_ratio = $this->params['width'] / $this->params['height'];
  123. if ($img_ratio < $resize_ratio)
  124. {
  125. // --- width
  126. $this->params['height'] = ceil ($this->params['width'] / $w * $h);
  127. }else
  128. {
  129. // --- height
  130. $this->params['width'] = ceil ($this->params['height'] / $h * $w);
  131. }
  132. }elseif (!empty($this->params['height']))
  133. {
  134. $img_factor = $h / $this->params['height'];
  135. $this->params['width'] = ceil ($w / $img_factor);
  136. }elseif (!empty($this->params['width']))
  137. {
  138. $img_factor = $w / $this->params['width'];
  139. $this->params['height'] = ceil ($h / $img_factor);
  140. }
  141. }
  142. function keepTransparent($des)
  143. {
  144. $image = $this->image;
  145. if ($image->getFormat() == 'PNG')
  146. {
  147. imagealphablending($des, false);
  148. imagesavealpha($des, true);
  149. }else if ($image->getFormat() == 'GIF')
  150. {
  151. $gdimage =& $image->getImage();
  152. $colorTransparent = imagecolortransparent($gdimage);
  153. imagepalettecopy($gdimage, $des);
  154. if($colorTransparent>0)
  155. {
  156. imagefill($des, 0, 0, $colorTransparent);
  157. imagecolortransparent($des, $colorTransparent);
  158. }
  159. imagetruecolortopalette($des, true, 256);
  160. }
  161. }
  162. function getParams()
  163. {
  164. global $REX,$I18N;
  165. return array(
  166. array(
  167. 'label'=>$I18N->msg('imanager_effect_resize_width'),
  168. 'name' => 'width',
  169. 'type' => 'int',
  170. ),
  171. array(
  172. 'label'=>$I18N->msg('imanager_effect_resize_height'),
  173. 'name' => 'height',
  174. 'type' => 'int'
  175. ),
  176. array(
  177. 'label' => $I18N->msg('imanager_effect_resize_style'),
  178. 'name' => 'style',
  179. 'type' => 'select',
  180. 'options' => $this->options,
  181. 'default' => 'fit',
  182. 'suffix' => $this->script
  183. ),
  184. array(
  185. 'label'=>$I18N->msg('imanager_effect_resize_imgtosmall'),
  186. 'name' => 'allow_enlarge',
  187. 'type' => 'select',
  188. 'options' => array('enlarge', 'not_enlarge'),
  189. 'default' => 'enlarge',
  190. ),
  191. );
  192. }
  193. }