image.lib.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 = @getimagesize($file);
  16. $type=$size[2];
  17. switch ($type) {
  18. case 2 : $imhandler = @imagecreatefromjpeg($file);
  19. break;
  20. case 3 : $imhandler = @imagecreatefrompng($file);
  21. break;
  22. case 1 : $imhandler = @imagecreatefromgif($file);
  23. break;
  24. }
  25. $xtmpstr=$handler.'x';
  26. $ytmpstr=$handler.'y';
  27. $this->$xtmpstr=$size[0];
  28. $this->$ytmpstr=$size[1];
  29. return $imhandler;
  30. }
  31. function resize ( $thumbw , $thumbh , $border) {
  32. $size [0]=$this->bgx;
  33. $size [1]=$this->bgy;
  34. if ($border==1) {
  35. $scale = min($thumbw/$size[0], $thumbh/$size[1]);
  36. $width = (int)($size[0]*$scale);
  37. $height = (int)($size[1]*$scale);
  38. $deltaw = (int)(($thumbw - $width)/2);
  39. $deltah = (int)(($thumbh - $height)/2);
  40. $dst_img = @ImageCreateTrueColor($thumbw, $thumbh);
  41. if ( !empty($this->color))
  42. @imagefill ( $dst_img, 0, 0, $this->color );
  43. $this->bgx=$thumbw;
  44. $this->bgy=$thumbh;
  45. }
  46. elseif ($border==0) {
  47. $scale = ($size[0] > 0 && $size[1] >0)?min($thumbw/$size[0], $thumbh/$size[1]):0;
  48. $width = (int)($size[0]*$scale);
  49. $height = (int)($size[1]*$scale);
  50. $deltaw = 0;
  51. $deltah = 0;
  52. $dst_img = @ImageCreateTrueColor($width, $height);
  53. $this->bgx=$width;
  54. $this->bgy=$height;
  55. }
  56. $src_img = $this->bg;
  57. @ImageCopyResampled($dst_img, $src_img, $deltaw, $deltah, 0, 0, $width, $height, ImageSX($src_img),ImageSY($src_img));
  58. $this->bg=$dst_img;
  59. @imagedestroy($src_img);
  60. }
  61. function addbackground($bgfile) {
  62. if ( !empty($bgfile) && file_exists($bgfile) ) { $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 = 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. break;
  108. case 'PNG' :
  109. if (!$file) header("Content-type: image/png");
  110. if ($compress!=-1)
  111. @imagetruecolortopalette ( $this->bg, true, $compress);
  112. return imagepng($this->bg,$file,$compress);
  113. break;
  114. case 'GIF' :
  115. if (!$file) header("Content-type: image/gif");
  116. if ($compress!=-1)
  117. @imagetruecolortopalette ( $this->bg, true, $compress);
  118. return imagegif($this->bg,$file,$compress);
  119. break;
  120. default: return 0;
  121. }
  122. @imagedestroy($this->bg);
  123. @imagedestroy($this->logo);
  124. }
  125. }
  126. ?>