image.lib.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. class image {
  3. var $bg;
  4. var $logo;
  5. var $logox;
  6. var $logoy;
  7. var $bgx;
  8. var $bgy;
  9. var $fontfile = './verdana';
  10. var $color;
  11. function image($bgfile = '') {
  12. image::addbackground($bgfile);
  13. }
  14. function createimagefromtype($file, $handler) {
  15. $size = api_getimagesize($file);
  16. $type = $size[2];
  17. switch ($type) {
  18. case 2 : $imhandler = @imagecreatefromjpeg($file); break;
  19. case 3 : $imhandler = @imagecreatefrompng($file); break;
  20. case 1 : $imhandler = @imagecreatefromgif($file); break;
  21. }
  22. $xtmpstr = $handler.'x';
  23. $ytmpstr = $handler.'y';
  24. $this->$xtmpstr = $size[0];
  25. $this->$ytmpstr = $size[1];
  26. return $imhandler;
  27. }
  28. function resize($thumbw, $thumbh, $border) {
  29. $size [0] = $this->bgx;
  30. $size [1] = $this->bgy;
  31. if ($border == 1) {
  32. $scale = min($thumbw / $size[0], $thumbh / $size[1]);
  33. $width = (int)($size[0] * $scale);
  34. $height = (int)($size[1] * $scale);
  35. $deltaw = (int)(($thumbw - $width) / 2);
  36. $deltah = (int)(($thumbh - $height) / 2);
  37. $dst_img = @ImageCreateTrueColor($thumbw, $thumbh);
  38. if (!empty($this->color)) {
  39. @imagefill($dst_img, 0, 0, $this->color);
  40. }
  41. $this->bgx = $thumbw;
  42. $this->bgy = $thumbh;
  43. }
  44. elseif ($border == 0) {
  45. $scale = ($size[0] > 0 && $size[1] > 0) ? min($thumbw / $size[0], $thumbh / $size[1]) : 0;
  46. $width = (int)($size[0] * $scale);
  47. $height = (int)($size[1] * $scale);
  48. $deltaw = 0;
  49. $deltah = 0;
  50. $dst_img = @ImageCreateTrueColor($width, $height);
  51. $this->bgx = $width;
  52. $this->bgy = $height;
  53. }
  54. $src_img = $this->bg;
  55. @ImageCopyResampled($dst_img, $src_img, $deltaw, $deltah, 0, 0, $width, $height, ImageSX($src_img), ImageSY($src_img));
  56. $this->bg=$dst_img;
  57. @imagedestroy($src_img);
  58. }
  59. function addbackground($bgfile) {
  60. if (!empty($bgfile) && file_exists($bgfile)) {
  61. $this->bg = image::createimagefromtype($bgfile, 'bg');
  62. @imagealphablending($this->bg, true);
  63. }
  64. }
  65. function addlogo($file) {
  66. $this->logo = image::createimagefromtype($file, 'logo');
  67. @imagealphablending($this->logo , true);
  68. $size = api_getimagesize($file);
  69. $this->logox = $size[0];
  70. $this->logoy = $size[1];
  71. }
  72. function mergelogo($x, $y, $alpha = 100) {
  73. if ($x < 0) $x = $this->bgx - $this->logox + $x;
  74. if ($y < 0) $y = $this->bgy - $this->logoy + $y;
  75. return @imagecopymerge($this->bg, $this->logo, $x, $y, 0, 0, $this->logox, $this->logoy, $alpha);
  76. }
  77. function makecolor($red, $green, $blue) {
  78. $this->color = @imagecolorallocate($this->bg, $red, $green, $blue);
  79. }
  80. function setfont($fontfile) {
  81. $this->fontfile = $fontfile;
  82. }
  83. function addtext ($text, $x = 0, $y = 0, $size = 12, $angle = 0) {
  84. putenv('GDFONTPATH=' . realpath('.'));
  85. $this->fontfile='verdana';
  86. $text= preg_replace('`(?<!\r)\n`', "\r\n", $text);
  87. $box = @imagettfbbox($size, $angle, $this->fontfile, $text);
  88. if ($x < 0) {
  89. $x = $this->bgx - max($box[2], $box[4]) + $x;
  90. } else {
  91. $x = max(-$box[0], -$box[6]) + $x;
  92. }
  93. if ($y < 0) {
  94. $y = $this->bgy - max($box[1], $box[3]) + $y;
  95. } else {
  96. $y = max(-$box[7], -$box[5]) + $y;
  97. }
  98. @imagettftext($this->bg, $size, $angle, $x, $y, $this->color, $this->fontfile , $text);
  99. }
  100. function send_image($type, $file = '', $compress = -1) {
  101. switch ($type) {
  102. case 'JPG':
  103. if (!$file) header("Content-type: image/jpeg");
  104. if ($compress == -1) $compress = 100;
  105. return imagejpeg($this->bg, $file, $compress);
  106. case 'PNG':
  107. if (!$file) header("Content-type: image/png");
  108. if ($compress != -1) {
  109. @imagetruecolortopalette($this->bg, true, $compress);
  110. }
  111. return imagepng($this->bg, $file, $compress);
  112. case 'GIF':
  113. if (!$file) header("Content-type: image/gif");
  114. if ($compress != -1) {
  115. @imagetruecolortopalette($this->bg, true, $compress);
  116. }
  117. return imagegif($this->bg, $file, $compress);
  118. default: return 0;
  119. }
  120. // TODO: Occupied memory is not released, because the following fragment of code is actually dead.
  121. @imagedestroy($this->bg);
  122. @imagedestroy($this->logo);
  123. }
  124. }