image.lib.php 4.0 KB

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