PageRenderTime 33ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ php-ppcms/includes/classes/lib.image.corner.class.php

http://php-ppcms.googlecode.com/
PHP | 271 lines | 213 code | 31 blank | 27 comment | 35 complexity | 80d1ef7923db13509bdacf078c06e6ee MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. * (c) 2009, jianyuzhu@gmail.com
  5. * All rights reserved
  6. * This script is part of the PPEMI project.
  7. ***************************************************************/
  8. class LibImageCorner {
  9. var $src_file = '';
  10. var $src_w = 0;
  11. var $src_h = 0;
  12. var $src_x = 0;
  13. var $src_y = 0;
  14. var $dest_file = '';
  15. var $dest_w = 189;
  16. var $dest_h = 247;
  17. var $dest_x = 0;
  18. var $dest_y = 0;
  19. var $cutting = 0;
  20. var $angle = 0;
  21. var $alpha_top = 80;
  22. var $alpha_bottom = 60;
  23. var $alpha_x = 0;
  24. var $alpha_y = 0;
  25. var $alpha_w = 0;
  26. var $alpha_h = 0;
  27. var $corner_file = 'images/corners/rounded_corner_10px.png';
  28. var $corner_radius = 10;
  29. var $corner_topleft = true;
  30. var $corner_topright = true;
  31. var $corner_bottomleft = true;
  32. var $corner_bottomright = true;
  33. var $text_str = 'Brake';
  34. var $font_path = 'images/fonts/';
  35. var $font_list = array(
  36. 'Antykwa' => array('spacing' => -3, 'minSize' => 27, 'maxSize' => 30, 'font' => 'AntykwaBold.ttf'),
  37. 'Candice' => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 31, 'font' => 'Candice.ttf'),
  38. 'DingDong' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 30, 'font' => 'Ding-DongDaddyO.ttf'),
  39. 'Duality' => array('spacing' => -2, 'minSize' => 30, 'maxSize' => 38, 'font' => 'Duality.ttf'),
  40. 'Heineken' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 34, 'font' => 'Heineken.ttf'),
  41. 'Jura' => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 32, 'font' => 'Jura.ttf'),
  42. 'StayPuft' => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 32, 'font' => 'StayPuft.ttf'),
  43. 'Times' => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 34, 'font' => 'TimesNewRomanBold.ttf'),
  44. 'VeraSans' => array('spacing' => -1, 'minSize' => 20, 'maxSize' => 28, 'font' => 'VeraSansBold.ttf'),
  45. );
  46. var $imagecreatefromfunc = '';
  47. var $imagefunc = '';
  48. //constructor
  49. function LibImageCorner($src_file, $dest_file) {
  50. $this->src_file = $src_file;
  51. $this->dest_file = $dest_file;
  52. //
  53. $srcinfo = @getimagesize($src_file);
  54. $this->src_w = $srcinfo['0'];
  55. $this->src_h = $srcinfo['1'];
  56. switch($srcinfo['mime'] ) {
  57. case 'image/jpeg':
  58. $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
  59. $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
  60. break;
  61. case 'image/gif':
  62. $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
  63. $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
  64. break;
  65. case 'image/png':
  66. $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng': '';
  67. $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
  68. break;
  69. }
  70. //
  71. $this->_image();
  72. }
  73. //methods
  74. function _image() {
  75. if( function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg') ) {
  76. $imagecreatefromfunc = $this->imagecreatefromfunc;
  77. $imagefunc = $this->imagefunc;
  78. if( $this->src_w >= $this->dest_w || $this->src_h >= $this->dest_h ) {
  79. //
  80. if( $this->cutting == 3 ) {
  81. $x_ratio = $this->dest_w / $this->src_w;
  82. $y_ratio = $this->dest_h / $this->src_h;
  83. if( ($x_ratio * $this->src_h) < $this->dest_h ) {
  84. $dest_h = ceil($x_ratio * $this->src_h);
  85. $dest_w = $this->dest_w;
  86. } else {
  87. $dest_w = ceil($y_ratio * $this->src_w);
  88. $dest_h = $this->dest_h;
  89. }
  90. $src_w = $this->src_w;
  91. $src_h = $this->src_h;
  92. if( $this->src_w > $this->dest_w ) {
  93. $src_w = $this->dest_w;
  94. }
  95. if( $this->src_h > $this->dest_h ) {
  96. $src_h = $this->dest_h;
  97. }
  98. $dest_w = $this->dest_w;
  99. $dest_h = $this->dest_h;
  100. } elseif( $this->cutting == 2 ) {
  101. $x_ratio = $this->dest_w / $this->src_w;
  102. $y_ratio = $this->dest_h / $this->src_h;
  103. if( ($x_ratio * $this->src_h) < $this->dest_h ) {
  104. $dest_h = ceil($x_ratio * $this->src_h);
  105. $dest_w = $this->dest_w;
  106. } else {
  107. $dest_w = ceil($y_ratio * $this->src_w);
  108. $dest_h = $this->dest_h;
  109. }
  110. $src_w = $this->src_w;
  111. $src_h = $this->src_h;
  112. } elseif( $this->cutting == 1 ) {
  113. $x_ratio = $this->dest_w / $this->src_w;
  114. $y_ratio = $this->dest_h / $this->src_h;
  115. if( ($x_ratio * $this->src_h) < $this->dest_h ) {
  116. $dest_h = ceil($x_ratio * $this->src_h);
  117. $dest_w = $this->dest_w;
  118. } else {
  119. $dest_w = ceil($y_ratio * $this->src_w);
  120. $dest_h = $this->dest_h;
  121. }
  122. $src_w = $this->src_w;
  123. $src_h = $this->src_h;
  124. } else {
  125. $src_w = $this->src_w;
  126. $src_h = $this->src_h;
  127. $dest_w = $this->dest_w;
  128. $dest_h = $this->dest_h;
  129. }
  130. //
  131. $src_im = $imagecreatefromfunc($this->src_file);
  132. //
  133. $dest_im = imagecreatetruecolor($dest_w, $dest_h);
  134. imagecopyresampled($dest_im, $src_im, $this->dest_x, $this->dest_y, $this->src_x, $this->src_y, $dest_w, $dest_h, $src_w, $src_h);
  135. //
  136. $dest_im = $this->_alpha($dest_im, $dest_w, $dest_h);
  137. //
  138. $dest_im = $this->_corner($dest_im, $dest_w, $dest_h);
  139. //
  140. $dest_im = $this->_text($dest_im);
  141. //
  142. if( $this->attachinfo['mime'] == 'image/jpeg' ) {
  143. $imagefunc($dest_im, $this->dest_file, 100);
  144. } else {
  145. $imagefunc($dest_im, $this->dest_file);
  146. }
  147. //
  148. imagedestroy($src_im);
  149. imagedestroy($dest_im);
  150. }
  151. }
  152. }
  153. function _corner($dest_im, $dest_width, $dest_height) {
  154. $corner_im = imagecreatefrompng($this->corner_file);
  155. $corner_width = imagesx($corner_im);
  156. $corner_height = imagesy($corner_im);
  157. $corner_imr = imagecreatetruecolor($this->corner_radius, $this->corner_radius);
  158. imagecopyresampled($corner_imr, $corner_im, 0, 0, 0, 0, $this->corner_radius, $this->corner_radius, $corner_width, $corner_height);
  159. $corner_width = imagesx($corner_imr);
  160. $corner_height = imagesy($corner_imr);
  161. $col_white = imagecolorallocate($dest_im, 255, 255, 255);
  162. $col_black = imagecolorallocate($dest_im, 0, 0, 0);
  163. // Top-left corner
  164. if( $this->corner_topleft == true ) {
  165. $dest_x = 0;
  166. $dest_y = 0;
  167. imagecolortransparent($corner_imr, $col_black);
  168. imagecopymerge($dest_im, $corner_imr, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
  169. }
  170. // Bottom-left corner
  171. if( $this->corner_bottomleft == true ) {
  172. $dest_x = 0;
  173. $dest_y = $dest_height - $corner_height;
  174. $rotated = imagerotate($corner_imr, 90, 0);
  175. imagecolortransparent($rotated, $col_black);
  176. imagecopymerge($dest_im, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
  177. }
  178. // Bottom-right corner
  179. if( $this->corner_bottomright == true ) {
  180. $dest_x = $dest_width - $corner_width;
  181. $dest_y = $dest_height - $corner_height;
  182. $rotated = imagerotate($corner_imr, 180, 0);
  183. imagecolortransparent($rotated, $col_black);
  184. imagecopymerge($dest_im, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
  185. }
  186. // Top-right corner
  187. if( $this->corner_topright == true ) {
  188. $dest_x = $dest_width - $corner_width;
  189. $dest_y = 0;
  190. $rotated = imagerotate($corner_imr, 270, 0);
  191. imagecolortransparent($rotated, $col_black);
  192. imagecopymerge($dest_im, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
  193. }
  194. // Rotate image
  195. //$dest_im = imagerotate($dest_im, $this->angle, $col_white);
  196. return $dest_im;
  197. }
  198. //
  199. function _alpha($dest_im, $dest_width, $dest_height) {
  200. if( $this->alpha_w == 0 ) {
  201. $this->alpha_w = $dest_width;
  202. $this->alpha_h = $dest_height;
  203. }
  204. for($i=0; $i<$this->alpha_top; $i++){
  205. $alphax = round($i/$this->alpha_top*127)+20;
  206. if( $alphax >= 128 ) {
  207. $alphax = 127;
  208. }
  209. $alpha = imagecolorallocatealpha($dest_im, 20, 20, 20, $alphax);
  210. imagefilledrectangle($dest_im, $this->alpha_x, $this->alpha_y+$i, $this->alpha_w, $this->alpha_y+$i+1, $alpha);
  211. }
  212. for($i=$this->alpha_h-$this->alpha_bottom; $i<$this->alpha_h; $i++){
  213. $alphax = round(($this->alpha_h-$i)/$this->alpha_bottom*110)+30;
  214. if( $alphax >= 128 ) {
  215. $alphax = 127;
  216. }
  217. $alpha = imagecolorallocatealpha($dest_im, 80, 80, 80, $alphax);
  218. imagefilledrectangle($dest_im, $this->alpha_x, $this->alpha_y+$i, $this->alpha_w, $this->alpha_y+$i+1, $alpha);
  219. }
  220. return $dest_im;
  221. }
  222. //
  223. function _text($dest_im) {
  224. $white = imagecolorallocate($dest_im, 255, 255, 255);
  225. $black = imagecolorallocate($dest_im, 0, 0, 0);
  226. $fontcfg = $this->font_list[array_rand($this->font_list)];
  227. $fontfile = $this->font_path . $fontcfg['font'];
  228. $text_size = 30;
  229. $text_angle = 0;
  230. $text_box = imagettfbbox($text_size, $text_angle, $fontfile, $this->text_str);
  231. $text_w = $text_box['2'] - $text_box['0'];
  232. $text_h = $text_box['3'] - $text_box['5'];
  233. $text_x = ceil(($this->dest_w - $text_w)/2);
  234. $text_y = $text_h+10;
  235. imagettftext($dest_im, $text_size, $text_angle, $text_x, $text_y, $white, $fontfile, $this->text_str);
  236. return $dest_im;
  237. }
  238. }
  239. //
  240. ?>