ËÑË÷   
Êղر¾Õ¾    ÉèΪÊ×Ò³    


Ê×Ò³  |  µçÓ°  |  ¶¯»­  |  Âþ»­  |  Ìùͼ  |  web±à³Ì  |  ÓÄĬЦ»°  |  °ïÖú  
РÌù    »Ø¸´Ìù×Ó    ¹²ÓÐ 0 Ìõ»ØÌù
>> web±à³Ì
  phpÉú³É¼ÓˮӡµÄͼƬÀà (Ö§³ÖˮӡΪͼƬ»òÕßÎÄ×Ö)


gb2312.txtÏÂÔØ: ÏÂÔØ1 ÏÂÔØ2

<?php
/*
+--------------------------------------------------------------------------
| Éú³É¼ÓˮӡµÄͼƬÀà (Ö§³ÖˮӡΪͼƬ»òÕßÎÄ×Ö)
| ============================
| by JackMing £¨¸ÐлDashºÍÆäËû²»ÖªµÀÐÕÃûµÄÅóÓÑÖ§³Ö£¬±¾ÀàÔÚÕâЩÅóÓÑ×÷Æ·µÄ»ù´¡ÉÏ´´½¨)
+--------------------------------------------------------------------------
²âÊÔ£º

$img = new Gimage();
$img->gburl = "inc/";//¼òÌåÖÐÎÄÂëÎļþ·¾¶
//$img->wm_text = "ÎÒÃǶ¼ÊÇÖйúÈË";
$img->wm_text = "www.csdn.net";
$img->wm_text_font = "c:\windows/fonts/STXINWEI.TTF";//×ÖÌåÎļþµÄ·¾¶Ãû£«ÎļþÃû
//$img->wm_image_name = "image/mouse.jpg";//ˮӡͼƬ£¬×¢Òâ°üº¬Â·¾¶Ãû
//$img->wm_text_size = 20;//×ÖÌå´óСÕë¶ÔÎı¾Ë®Ó¡
$img->create("./mouse.jpg");

*/

Class Gimage{
 var $src_image_name = "";           //ÊäÈëͼƬµÄÎļþÃû(±ØÐë°üº¬Â·¾¶Ãû)
 var $jpeg_quality = 60;             //jpegͼƬÖÊÁ¿
 var $save_file = "";                //Êä³öÎļþÃû
 var $dstW = 1024;                   //ͼƬÊä³ö×î´ó¿í¶È
 var $fsize = 307200;                //ͼƬÊä³ö×î´ó×Ö½ÚÊý
 var $wm_image_name = "";            //ˮӡͼƬµÄÎļþÃû(±ØÐë°üº¬Â·¾¶Ãû)
 var $wm_image_pos = 3;              //ˮӡͼƬ·ÅÖõÄλÖÃ
 // 0 = middle
 // 1 = top left
 // 2 = top right
 // 3 = bottom right
 // 4 = bottom left
 // 5 = top middle
 // 6 = middle right
 // 7 = bottom middle
 // 8 = middle left
 //other = 3
 var $wm_image_transition = 80;            //ˮӡͼƬÓëԭͼƬµÄÈÚºÏ¶È (1=100)
 var $gburl = "./";                        //¼òÌåÖÐÎļþÂëÎļþ·¾¶
 var $wm_text = "";                        //ˮӡÎÄ×Ö(Ö§³ÖÖÐÓ¢ÎÄÒÔ¼°´øÓÐ\r\nµÄ¿çÐÐÎÄ×Ö)
 var $wm_text_size = 20;                   //ˮӡÎÄ×Ö´óС
 var $wm_text_angle = 5;                   //ˮӡÎÄ×ֽǶÈ,Õâ¸öÖµ¾¡Á¿²»Òª¸ü¸Ä
 var $wm_text_pos = 3;                     //ˮӡÎÄ×Ö·ÅÖÃλÖÃ
 var $wm_text_font = "";                   //ˮӡÎÄ×ÖµÄ×ÖÌå
 var $wm_text_color = "#cccccc";           //ˮӡ×ÖÌåµÄÑÕɫֵ
 
 
 function create($filename="")
 {
  if ($filename) {
   $this->src_image_name = strtolower(trim($filename));
  }
  
  $src_image_type = $this->get_type($this->src_image_name);
  $src_image = $this->createImage($src_image_type,$this->src_image_name);
  if (!$src_image) return;
  $src_image_w=ImageSX($src_image);
  $src_image_h=ImageSY($src_image);
  
  
  if ($this->wm_image_name){
      $this->wm_image_name = strtolower(trim($this->wm_image_name));
      $wm_image_type = $this->get_type($this->wm_image_name);
      $wm_image = $this->createImage($wm_image_type,$this->wm_image_name);
      $wm_image_w=ImageSX($wm_image);
      $wm_image_h=ImageSY($wm_image);
      $temp_wm_image = $this->getPos($src_image_w,$src_image_h,$this->wm_image_pos,$wm_image);
      $wm_image_x = $temp_wm_image["dest_x"];
      $wm_image_y = $temp_wm_image["dest_y"];
      imagecopymerge($src_image, $wm_image,$wm_image_x,$wm_image_y,0,0,$wm_image_w,$wm_image_h,$this->wm_image_transition);
  }
  
  if ($this->wm_text){
      $this->wm_text = $this->gb2utf8($this->wm_text);
      $temp_wm_text = $this->getPos($src_image_w,$src_image_h,$this->wm_text_pos);
      $wm_text_x = $temp_wm_text["dest_x"];
      $wm_text_y = $temp_wm_text["dest_y"];
     if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color))
     {
     $red = hexdec($color[1]);
     $green = hexdec($color[2]);
     $blue = hexdec($color[3]);
     $wm_text_color = imagecolorallocate($src_image, $red,$green,$blue);
     }else{
     $wm_text_color = imagecolorallocate($src_image, 255,255,255);
     }
  
      imagettftext($src_image, $this->wm_text_size, $this->wm_text_angle, $wm_text_x, $wm_text_y, $wm_text_color,$this->wm_text_font,  $this->wm_text);
  }
  
  //µ÷Õû´óС»òÕ߳ߴç
  if($this->dstW){
   $flag = 0;
   $size = getimagesize($filename);
   
   if($size[0] > $this->dstW){//ͼƬ³ß´ç´óÓÚÏÞ¶¨³ß´ç
    $flag = 1;
    $dstH = $this->dstW/$size[0]*$size[1];
   }else if($this->fsize < filesize($filename)){//ͼƬ´óС´óÓÚÏÞ¶¨´óС
    $flag = 1;
    $this->dstW = $size[0];
    $dstH = $size[1];
   }
   if($flag){
    $dst_image = imagecreatetruecolor($this->dstW, $dstH);
    imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $this->dstW, $dstH, $size[0], $size[1]);
    $src_image = $dst_image;
    unset($dst_image);
   }
  }
  
  if ($this->save_file)
  {
    switch ($this->get_type($this->save_file)){
     case 'gif':$src_img=ImagePNG($src_image, $this->save_file); break;
     case 'jpeg':$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
     case 'png':$src_img=ImagePNG($src_image, $this->save_file); break;
     default:$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
    }
  }
  else
  {
  if ($src_image_type = "jpg") $src_image_type="jpeg";
    header("Content-type: image/{$src_image_type}");
    switch ($src_image_type){
     case 'gif':$src_img=ImagePNG($src_image); break;
     case 'jpg':$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
     case 'png':$src_img=ImagePNG($src_image);break;
     default:$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
    }
  }
  imagedestroy($src_image);
 }
 
 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 /*
 createImage     ¸ù¾ÝÎļþÃûºÍÀàÐÍ´´½¨Í¼Æ¬
 ÄÚ²¿º¯Êý
 
 $type:                ͼƬµÄÀàÐÍ£¬°üÀ¨gif,jpg,png
 $img_name:  ͼƬÎļþÃû£¬°üÀ¨Â·¾¶Ãû£¬ÀýÈç " ./mouse.jpg"
 */
 function createImage($type,$img_name){
    if (!$type){
      $type = $this->get_type($img_name);
    }
 
     switch ($type){
       case 'gif':
       if (function_exists('imagecreatefromgif'))
           $tmp_img=@ImageCreateFromGIF($img_name);
       break;
       case 'jpg':
       $tmp_img=ImageCreateFromJPEG($img_name);
       break;
       case 'png':
       $tmp_img=ImageCreateFromPNG($img_name);
       break;
       default:
       $tmp_img=ImageCreateFromString($img_name);
       break;
     }
     return $tmp_img;
 }
 
 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 getPos               ¸ù¾ÝԴͼÏñµÄ³¤¡¢¿í£¬Î»ÖôúÂ룬ˮӡͼƬidÀ´Éú³É°Ñˮӡ·ÅÖõ½Ô´Í¼ÏñÖеÄλÖÃ
 ÄÚ²¿º¯Êý
 
 $sourcefile_width:        ԴͼÏñµÄ¿í
 $sourcefile_height: ԭͼÏñµÄ¸ß
 $pos:               λÖôúÂë
 // 0 = middle
 // 1 = top left
 // 2 = top right
 // 3 = bottom right
 // 4 = bottom left
 // 5 = top middle
 // 6 = middle right
 // 7 = bottom middle
 // 8 = middle left
 $wm_image:           ˮӡͼƬID
 */
 function getPos($sourcefile_width,$sourcefile_height,$pos,$wm_image=""){
    if  ($wm_image){
      $insertfile_width = ImageSx($wm_image);
      $insertfile_height = ImageSy($wm_image);
    }else {
      $lineCount = explode("\r\n",$this->wm_text);
      $fontSize = imagettfbbox($this->wm_text_size,$this->wm_text_angle,$this->wm_text_font,$this->wm_text);
      $insertfile_width = $fontSize[2] - $fontSize[0];
      $insertfile_height = count($lineCount)*($fontSize[1] - $fontSize[3]);
    }
 
    switch ($pos){
     case 0:
        $dest_x = ( $sourcefile_width / 2 ) - ( $insertfile_width / 2 );
        $dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
        break;
 
     case 1:
        $dest_x = 0;
        if ($this->wm_text){
         $dest_y = $insertfile_height;
        }else{
         $dest_y = 0;
        }
        break;
 
     case 2:
       $dest_x = $sourcefile_width - $insertfile_width;
       if ($this->wm_text){
       $dest_y = $insertfile_height;
       }else{
        $dest_y = 0;
       }
       break;
 
     case 3:
       $dest_x = $sourcefile_width - $insertfile_width;
       $dest_y = $sourcefile_height - $insertfile_height;
       break;
 
     case 4:
       $dest_x = 0;
       $dest_y = $sourcefile_height - $insertfile_height;
       break;
 
     case 5:
      $dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );
      if ($this->wm_text){
      $dest_y = $insertfile_height;
      }else{
      $dest_y = 0;
      }
      break;
 
     case 6:
      $dest_x = $sourcefile_width - $insertfile_width;
      $dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
      break;
 
     case 7:
      $dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );
      $dest_y = $sourcefile_height - $insertfile_height;
      break;
 
     case 8:
      $dest_x = 0;
      $dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
      break;
 
     default:
       $dest_x = $sourcefile_width - $insertfile_width;
       $dest_y = $sourcefile_height - $insertfile_height;
       break;
    }
   return array("dest_x"=>$dest_x,"dest_y"=>$dest_y);
 }
 
 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gb2utf8                Ö¸¶¨µÄÎÄ×Öת»»ÎªUTF-8¸ñʽ£¬°üÀ¨ÖÐÓ¢ÎÄ»ìºÏ
 ÄÚ²¿º¯Êý
 */
 function gb2utf8($gb)
 {
   if(!trim($gb))
     return $gb;
   $filename = $this->gburl."gb2312.txt";
   $tmp=file($filename);
   $codetable=array();
   while(list($key,$value)=each($tmp))
   $codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
 
   $utf8="";
   while($gb)
   {
     if (ord(substr($gb,0,1))>127)
     {
       $tthis=substr($gb,0,2);
       $gb=substr($gb,2,strlen($gb)-2);
       $utf8.=$this->u2utf8(hexdec($codetable[hexdec(bin2hex($tthis))-0x8080]));
     }
     else
     {
       $tthis=substr($gb,0,1);
       $gb=substr($gb,1,strlen($gb)-1);
       $utf8.=$this->u2utf8($tthis);
     }
   }
 
 return $utf8;
 }
 
 function u2utf8($c)
 {
   $str="";
   if ($c < 0x80)
   {
     $str.=$c;
   }
   else if ($c < 0x800)
   {
     $str.=chr(0xC0 | $c>>6);
     $str.=chr(0x80 | $c & 0x3F);
   }
   else if ($c < 0x10000)
   {
     $str.=chr(0xE0 | $c>>12);
     $str.=chr(0x80 | $c>>6 & 0x3F);
     $str.=chr(0x80 | $c & 0x3F);
   }
   else if ($c < 0x200000)
   {
     $str.=chr(0xF0 | $c>>18);
     $str.=chr(0x80 | $c>>12 & 0x3F);
     $str.=chr(0x80 | $c>>6 & 0x3F);
     $str.=chr(0x80 | $c & 0x3F);
   }
 return $str;
 }
 
 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 get_type                »ñµÃͼƬµÄ¸ñʽ£¬°üÀ¨jpg,png,gif
 ÄÚ²¿º¯Êý
 
 $img_name£º        ͼƬÎļþÃû£¬¿ÉÒÔ°üÀ¨Â·¾¶Ãû
 */
 function get_type($img_name)//»ñȡͼÏñÎļþÀàÐÍ
 {
  $name_array = explode(".",$img_name);
  if (preg_match("/\.(jpg|jpeg|gif|png)$/", $img_name, $matches))
  {
   $type = strtolower($matches[1]);
  }
  else
  {
   $type = "string";
  }
  return $type;
 }

}
?>


bafnfqp    58.62.119.*    2008-01-07 13:18:16      

http://www.zc36.com °æÈ¨ËùÓÐ  czc136#hotmail.com   QQ:834011201  ÔÁICP±¸07512307ºÅ