【小白笔记】PHP学习之路 (28) --图像操作+画线

来源:互联网 发布:mac ladybug试色 编辑:程序博客网 时间:2024/05/22 09:00

getimagesize()  获取图像的宽高、类型、位数、通道、mime类型等。

imagecreatefromxxx()     从图像建立资源类型。xxx可以是png、jpeg、gif、wbmp等。不能混用,要打开一个图像必须使用相应的函数。

imagecreatefromstring()     从文本(字符串)创建图像资源。

imagesx()          获取图像资源的宽度。

imagesy()          获取图像资源的高度。

image_type_to_extension()       将图像类型序号转换为具体的字母形式。如1->.gif、2->.jpeg、3->.png。

image_type_to_mime_type()      将图像类型序号转换为mime类型。如1->image/gif。


imagecreate()          创建基于调色板的图像资源。可以指定高度和宽度。默认颜色为黑色。色彩为256色。

imagecolorstotal()       获取指定的图像资源有多少种颜色。初始画布颜色数为0,输出时默认以黑色输出。

imagecolorallocate()      为一幅图像分配颜色。参数:资源,红,绿,蓝    其中颜色为数值0~255。

imagecolorallocatealpha()      比imagecolorallocate()多一个透明参数,范围0~127。127为全透明。

imagegif()      显示gif图像。

imagejpeg()     显示jpeg图像。

imagedestory()       释放图像资源。

         注:不能写成jpg,必须写成jpeg。

imagefill()       为真彩色图像填充颜色。第二三个参数为填充坐标,与该坐标颜色相同且相邻的点才会被填充。真彩色图像在分配颜色后必须填充才能被应用。

imagefilltoborder()      区域填充到指定颜色的边界为止

imagecolorat()      对于真彩色图像,获取图像指定位置的RGB颜色。对于基于调色板的图像,获取指定位置的索引。

imagecopy()      将一张图放到另一张图上。

imagesettile()      使用贴图来填充一块区域。

imageline()      用指定颜色在两点之间画一条线段。

imagedashedline()     用指定颜色画一条虚线。用法与imageline()相同。

imagesetstyle()      自定义划线风格。风格以数组形式定义。

imagesetthickness()      定义划线的粗细。

imagesetbrush()      设置图像的笔刷。可以将图像资源作为笔刷。



格式用法:

 

[php] view plain copy
  1. <?php  
  2. /*  echo "<pre>"; 
  3.     $imgInfo = getimagesize("picx.gif");  //第二项:1为gif、2为jpg、3为png 
  4.     print_r($imgInfo); 
  5.     echo "<img src='picx.gif' $imgInfo[3]/>"."<br/>"; 
  6.     echo image_type_to_extension($imgInfo[2])."<br/>"; 
  7.     echo image_type_to_mime_type($imgInfo[2]); 
  8.     echo "<br/>===============================<br/>"; 
  9.     $img_png = imagecreatefrompng('pic2.png'); 
  10.     echo "pic2.png的高度是:".imagesy($img_png)."<br/>"; 
  11.     echo "pic2.png的宽度是:".imagesx($img_png)."<br/>"; 
  12.     echo "<br/>===============================<br/>"; 
  13. */  
  14. /*  header("Content-type:image/gif"); 
  15.     //header("Content-type:image/jpeg");   //必须指定mime类型 
  16.     $gif_img = imagecreate(600,600);   //创建基于调色板的256索引色图像 
  17.     $jpg_img = imagecreatetruecolor(300,300); //创建真彩色图像 
  18.     //imagecolorallocate($gif_img,0,255,0);    //为图像分配颜色 
  19.     imagegif($gif_img);          //显示图像 
  20.     //imagejpeg($jpg_img); 
  21.     //echo imagecolorstotal($gif_img);    //显示图像总颜色数 
  22.     imagedestory($jpg_img); 
  23. */  
  24. /*  header("Content-type:image/gif"); 
  25.     $gif1 = imagecreate(500,200); 
  26.     $gif2 = imagecreate(500,200); 
  27.     $red = imagecolorallocate($gif1,255,0,0); 
  28.     imagegif($gif1); 
  29. */  
  30. /*  header("Content-type:image/jpeg"); 
  31.     $jpeg1 = imagecreatetruecolor(200,600); 
  32.     $red = imagecolorallocate($jpeg1,255,0,0); 
  33.     imagefill($jpeg1,0,0,$red); 
  34.     imagejpeg($jpeg1); 
  35. */  
  36.       
  37.     $filename = "pic2.png";  
  38.     if(!file_exists($filename)){  
  39.         die("文件不存在!");  
  40.     }  
  41.     $imgInfo = getimagesize($filename);  
  42.     $base = image_type_to_extension($imgInfo[2]);  //实现方法一  
  43.     //$base = strtolower(strrchr($filename,'.'));    
  44. /*  switch($base){     
  45.         case '.gif': 
  46.             $img = imagecreatefromgif($filename); 
  47.             break; 
  48.         case '.jpeg': 
  49.             $img = imagecreatefromjpeg($filename); 
  50.             break; 
  51.         case '.jpg': 
  52.             $img = imagecreatefromjpeg($filename); 
  53.             break; 
  54.         case '.png': 
  55.             $img = imagecreatefrompng($filename); 
  56.             break; 
  57.         default: 
  58.             die("文件类型错误!"); 
  59.     } 
  60. */  
  61. /*  //$base = substr(strtolower(strrchr($filename,'.')),1);   
  62.     //$base = substr($imgInfo['mime'],6); 
  63.     $base = substr(strrchr($imgInfo['mime'],'/'),1);  //实现方法二 
  64.     if($base=="jpg") $base = "jpeg"; 
  65.     eval('$img = imagecreatefrom'.$base.'($filename);'); 
  66.     eval('header(\'Content-type:image/'.$base.'\');'); 
  67.     eval('image'.$base.'($img);'); 
  68. */  
  69.     $str = file_get_contents("pic2.png");  
  70.     file_put_contents('image.txt',$str);  
  71.   
  72. //  $img = imagecreatefromstring('image.txt');  
  73. //  $img = imagecreatefromjpeg('image.txt');  
  74.     $strget = file_get_contents('image.txt');  
  75.     $img = imagecreatefromstring($strget);  
  76.     header("Content-type:image/png");  
  77.     imagepng($img);  
  78.     imagedestory($img);  
  79. ?>  

原创粉丝点击