PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/generate_avatar.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 107 lines | 64 code | 15 blank | 28 comment | 9 complexity | 19ae6b8d85cb96eda7dfa871931b1465 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. define('CTRACKER_DISABLED', true);
  11. define('IN_ICYPHOENIX', true);
  12. if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './');
  13. if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
  14. include(IP_ROOT_PATH . 'common.' . PHP_EXT);
  15. $fonts_path = 'images/fonts/';
  16. $generator_template_path = $config['avatar_generator_template_path'] . '/';
  17. $dest_pic = request_var('cachefile', '');
  18. $source_pic = request_var('avatarfile', '');
  19. $source_pic_full = $source_pic . '.gif';
  20. $text_content = stripslashes(request_var('text_content', '', true));
  21. $text_size = request_var('text_size', 10);
  22. $text_font = request_var('text_font', 'denmark.ttf');
  23. $text_font = $fonts_path . $text_font;
  24. $text_color = request_var('text_color', '#ffffff');
  25. $text_position = request_var('text_position', 0);
  26. $avatars_array = array('ip.gif', 'a69_02.gif', 'agreen.gif', 'aphro_lite.gif', 'aphrodite.gif', 'blue.gif', 'darkblue.gif', 'firefox.gif', 'gray.gif', 'green.gif', 'opera.gif', 'pink.gif', 'purple.gif', 'red.gif', 'sblue.gif', 'av01.gif', 'av02.gif', 'av03.gif', 'av04.gif', 'av05.gif', 'av06.gif', 'av07.gif', 'av08.gif', 'av09.gif', 'av10.gif', 'av11.gif', 'av12.gif', 'av13.gif', 'av14.gif', 'av15.gif', 'av16.gif', 'av17.gif');
  27. if (in_array($source_pic_full, $avatars_array))
  28. {
  29. $source_pic = $generator_template_path . $source_pic_full;
  30. }
  31. else
  32. {
  33. $num = mt_rand(1, sizeof($avatars_array));
  34. $source_pic = $generator_template_path . $avatars_array[$num];
  35. }
  36. write_text($source_pic, $dest_pic, $text_content, $text_font, $text_size, $text_color, $text_position);
  37. //write_text($source_pic, $dest_pic, $text_content, $text_font, $text_size, '#FFFFAA', $text_position);
  38. /**
  39. * Write text on images
  40. */
  41. function write_text($source_pic, $dest_pic, $text_content, $text_font, $text_size = 10, $text_color = '#FFFFFF', $text_position = '0')
  42. {
  43. $temp_pic = imagecreatefromgif($source_pic);
  44. list($image_width, $image_height) = getimagesize($source_pic);
  45. /*
  46. $temp_pic_empty = imagecreatetruecolor ($image_width, $image_height);
  47. imagealphablending($temp_pic_empty, false);
  48. imagecopyresampled($temp_pic_empty, $temp_pic, 0, 0, 0, 0, $image_width, $image_height, $image_width, $image_height);
  49. imagesavealpha($temp_pic_empty, true);
  50. //imagepng($im_re, 'small_redfade.png');
  51. $temp_pic2 = imagecreatefrompng($source_pic);
  52. */
  53. // Calculate the centre
  54. for(;;)
  55. {
  56. list($left_x, , $right_x) = imagettfbbox($text_size, $text_position, $text_font, $text_content);
  57. $text_width = $right_x - $left_x;
  58. if($image_width > $text_width + 5)
  59. {
  60. break;
  61. }
  62. $text_size = $text_size - 0.5;
  63. if($text_size == 1)
  64. {
  65. die('Font size may not be reduced further, try to insert a shorter text');
  66. }
  67. }
  68. $text_padding = ($image_width - $text_width) / 2;
  69. $text_color = (substr($text_color, 0, 1) == '#') ? substr($text_color, 1, 6) : $text_color;
  70. $text_color_r = hexdec(substr($text_color, 0, 2));
  71. $text_color_g = hexdec(substr($text_color, 2, 2));
  72. $text_color_b = hexdec(substr($text_color, 4, 2));
  73. $text_color = imagecolorresolve($temp_pic, $text_color_r, $text_color_g, $text_color_b);
  74. //$text_color = imagecolorallocate($temp_pic, $text_color_r, $text_color_g, $text_color_b);
  75. imagettftext($temp_pic, $text_size, $text_position, $text_padding, ($image_height - ($text_size / 2)), $text_color, $text_font, $text_content);
  76. if($_GET['dl'])
  77. {
  78. header('Content-Disposition: attachment; filename="avatar.gif"');
  79. }
  80. /*
  81. header("Content-type: image/png");
  82. imagepng($temp_pic, $dest_pic);
  83. imagepng($temp_pic);
  84. imagedestroy($temp_pic);
  85. */
  86. header('Content-type: image/gif');
  87. imagegif($temp_pic, $dest_pic);
  88. imagegif($temp_pic);
  89. imagedestroy($temp_pic);
  90. return true;
  91. }
  92. ?>