Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php图像处理函数大全(缩放、剪裁、缩放、翻转、旋转、透明、锐化的实例总结)_PHP教程

php图像处理函数大全(缩放、剪裁、缩放、翻转、旋转、透明、锐化的实例总结)_PHP教程

WBOY
WBOYasal
2016-07-20 11:12:48952semak imbas

一、创建图片资源
imagecreatetruecolor(width,height);
imagecreatefromgif(图片名称);
imagecreatefrompng(图片名称);
imagecreatefromjpeg(图片名称);画出各种图像 imagegif(图片资源,保存路径);
imagepng()
imagejpeg();

二、获取图片属性
imagesx(res//宽度
imagesy(res//高度
getimagesize(文件路径)
返回一个具有四个单元的数组。索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。这些标记与 PHP 4.3.0 新加的 IMAGETYPE 常量对应。索引 3 是文本字符串,内容为“height="yyy" width="xxx"”,可直接用于 IMG 标记。
销毁图像资源
imagedestroy(图片资源);

三、透明处理
PNG、jpeg透明色都正常,只有gif不正常
imagecolortransparent(resource image [,int color])//将某个颜色设置成透明色
imagecolorstotal()
imagecolorforindex();

四、图片的裁剪
imagecopyresized()
imagecopyresampled();

五、加水印(文字、图片)
字符串编码转换string iconv ( string $in_charset , string $out_charset , string $str )

六、图片旋转
imagerotate();//制定角度的图片翻转

七、图片的翻转
沿X轴 沿Y轴翻转

八、锐化
imagecolorsforindex()
imagecolorat()
在图片上画图形 $img=imagecreatefromgif("./images/map.gif");

<span  1</span> <?<span PHP
</span><span  2</span> <span /*</span><span *
</span><span  3</span> <span * 图片锐化处理
</span><span  4</span> <span * edit by www.jbxue.com
</span><span  5</span> <span */</span>
<span  6</span> <span $red</span>= imagecolorallocate(<span $img</span>, 255, 0, 0<span );
</span><span  7</span> 
<span  8</span> imageline(<span $img</span>, 0, 0, 100, 100, <span $red</span><span );
</span><span  9</span> imageellipse(<span $img</span>, 200, 100, 100, 100, <span $red</span><span );
</span><span 10</span> imagegif(<span $img</span>, "./images/map2.gif"<span );
</span><span 11</span> imagedestroy(<span $img</span><span );
</span><span 12</span> <span 图片普通缩放
</span><span 13</span> 复制代码 代码如下:
<span 14</span> 
<span 15</span> <span $filename</span>="./images/hee.jpg"<span ;
</span><span 16</span> <span $per</span>=0.3<span ;
</span><span 17</span> <span list</span>(<span $width</span>, <span $height</span>)=<span getimagesize</span>(<span $filename</span><span );
</span><span 18</span> <span $n_w</span>=<span $width</span>*<span $per</span><span ;
</span><span 19</span> <span $n_h</span>=<span $width</span>*<span $per</span><span ;
</span><span 20</span> <span $new</span>=imagecreatetruecolor(<span $n_w</span>, <span $n_h</span><span );
</span><span 21</span> <span $img</span>=imagecreatefromjpeg(<span $filename</span><span );
</span><span 22</span> <span //</span><span 拷贝部分图像并调整</span>
<span 23</span> imagecopyresized(<span $new</span>, <span $img</span>,0, 0,0, 0,<span $n_w</span>, <span $n_h</span>, <span $width</span>, <span $height</span><span );
</span><span 24</span> <span //</span><span 图像输出新图片、另存为</span>
<span 25</span> imagejpeg(<span $new</span>, "./images/hee2.jpg"<span );
</span><span 26</span> imagedestroy(<span $new</span><span );
</span><span 27</span> imagedestroy(<span $img</span><span );
</span><span 28</span> 
<span 29</span> <span 图片等比例缩放、没处理透明色
</span><span 30</span> 复制代码 代码如下:
<span 31</span> 
<span 32</span> <span function</span> thumn(<span $background</span>, <span $width</span>, <span $height</span>, <span $newfile</span><span ) {
</span><span 33</span> <span list</span>(<span $s_w</span>, <span $s_h</span>)=<span getimagesize</span>(<span $background</span>);<span //</span><span 获取原图片高度、宽度</span>
<span 34</span> <span if</span> (<span $width</span> && (<span $s_w</span> < <span $s_h</span><span )) {
</span><span 35</span> <span $width</span> = (<span $height</span> / <span $s_h</span>) * <span $s_w</span><span ;
</span><span 36</span> } <span else</span><span  {
</span><span 37</span> <span $height</span> = (<span $width</span> / <span $s_w</span>) * <span $s_h</span><span ;
</span><span 38</span> <span }
</span><span 39</span> <span $new</span>=imagecreatetruecolor(<span $width</span>, <span $height</span><span );
</span><span 40</span> <span $img</span>=imagecreatefromjpeg(<span $background</span><span );
</span><span 41</span> imagecopyresampled(<span $new</span>, <span $img</span>, 0, 0, 0, 0, <span $width</span>, <span $height</span>, <span $s_w</span>, <span $s_h</span><span );
</span><span 42</span> imagejpeg(<span $new</span>, <span $newfile</span><span );
</span><span 43</span> imagedestroy(<span $new</span><span );
</span><span 44</span> imagedestroy(<span $img</span><span );
</span><span 45</span> <span }
</span><span 46</span> thumn("images/hee.jpg", 200, 200, "./images/hee3.jpg"<span );
</span><span 47</span> 
<span 48</span> <span gif透明色处理
</span><span 49</span> 复制代码 代码如下:
<span 50</span> 
<span 51</span> <span function</span> thumn(<span $background</span>, <span $width</span>, <span $height</span>, <span $newfile</span><span ) {
</span><span 52</span> <span list</span>(<span $s_w</span>, <span $s_h</span>)=<span getimagesize</span>(<span $background</span><span );
</span><span 53</span> <span if</span> (<span $width</span> && (<span $s_w</span> < <span $s_h</span><span )) {
</span><span 54</span> <span $width</span> = (<span $height</span> / <span $s_h</span>) * <span $s_w</span><span ;
</span><span 55</span> } <span else</span><span  {
</span><span 56</span> <span $height</span> = (<span $width</span> / <span $s_w</span>) * <span $s_h</span><span ;
</span><span 57</span> <span }
</span><span 58</span> <span $new</span>=imagecreatetruecolor(<span $width</span>, <span $height</span><span );
</span><span 59</span> <span $img</span>=imagecreatefromgif(<span $background</span><span );
</span><span 60</span> <span $otsc</span>=imagecolortransparent(<span $img</span><span );
</span><span 61</span> <span if</span>(<span $otsc</span> >=0 && <span $otst</span> < imagecolorstotal(<span $img</span>)){<span //</span><span 判断索引色</span>
<span 62</span> <span $tran</span>=imagecolorsforindex(<span $img</span>, <span $otsc</span>);<span //</span><span 索引颜色值</span>
<span 63</span> <span $newt</span>=imagecolorallocate(<span $new</span>, <span $tran</span>["red"], <span $tran</span>["green"], <span $tran</span>["blue"<span ]);
</span><span 64</span> imagefill(<span $new</span>, 0, 0, <span $newt</span><span );
</span><span 65</span> imagecolortransparent(<span $new</span>, <span $newt</span><span );
</span><span 66</span> <span }
</span><span 67</span> imagecopyresized(<span $new</span>, <span $img</span>, 0, 0, 0, 0, <span $width</span>, <span $height</span>, <span $s_w</span>, <span $s_h</span><span );
</span><span 68</span> imagegif(<span $new</span>, <span $newfile</span><span );
</span><span 69</span> imagedestroy(<span $new</span><span );
</span><span 70</span> imagedestroy(<span $img</span><span );
</span><span 71</span> <span }
</span><span 72</span> thumn("images/map.gif", 200, 200, "./images/map3.gif");

<?<span php
</span><span /*</span><span *
* 图片裁剪处理
* edit by www.jbxue.com
</span><span */</span>

<span function</span> cut(<span $background</span>, <span $cut_x</span>, <span $cut_y</span>, <span $cut_width</span>, <span $cut_height</span>, <span $location</span><span ){
</span><span $back</span>=imagecreatefromjpeg(<span $background</span><span );
</span><span $new</span>=imagecreatetruecolor(<span $cut_width</span>, <span $cut_height</span><span );
imagecopyresampled(</span><span $new</span>, <span $back</span>, 0, 0, <span $cut_x</span>, <span $cut_y</span>, <span $cut_width</span>, <span $cut_height</span>,<span $cut_width</span>,<span $cut_height</span><span );
imagejpeg(</span><span $new</span>, <span $location</span><span );
imagedestroy(</span><span $new</span><span );
imagedestroy(</span><span $back</span><span );
}
cut(</span>"./images/hee.jpg", 440, 140, 117, 112, "./images/hee5.jpg"<span );
</span>?>

文字水印

<span  1</span> <?<span PHP
</span><span  2</span> <span /*</span><span *
</span><span  3</span> <span * 
</span><span  4</span> <span * 图片添加文字水印
</span><span  5</span> <span */</span>
<span  6</span> 
<span  7</span> <span function</span> mark_text(<span $background</span>, <span $text</span>, <span $x</span>, <span $y</span><span ){
</span><span  8</span> <span $back</span>=imagecreatefromjpeg(<span $background</span><span );
</span><span  9</span> <span $color</span>=imagecolorallocate(<span $back</span>, 0, 255, 0<span );
</span><span 10</span> imagettftext(<span $back</span>, 20, 0, <span $x</span>, <span $y</span>, <span $color</span>, "simkai.ttf", <span $text</span><span );
</span><span 11</span> imagejpeg(<span $back</span>, "./images/hee7.jpg"<span );
</span><span 12</span> imagedestroy(<span $back</span><span );
</span><span 13</span> <span }
</span><span 14</span> mark_text("./images/hee.jpg", "细说PHP", 150, 250<span );
</span><span 15</span> <span //</span><span 图片水印</span>
<span 16</span> <span function</span> mark_pic(<span $background</span>, <span $waterpic</span>, <span $x</span>, <span $y</span><span ){
</span><span 17</span> <span $back</span>=imagecreatefromjpeg(<span $background</span><span );
</span><span 18</span> <span $water</span>=imagecreatefromgif(<span $waterpic</span><span );
</span><span 19</span> <span $w_w</span>=imagesx(<span $water</span><span );
</span><span 20</span> <span $w_h</span>=imagesy(<span $water</span><span );
</span><span 21</span> imagecopy(<span $back</span>, <span $water</span>, <span $x</span>, <span $y</span>, 0, 0, <span $w_w</span>, <span $w_h</span><span );
</span><span 22</span> imagejpeg(<span $back</span>,"./images/hee8.jpg"<span );
</span><span 23</span> imagedestroy(<span $back</span><span );
</span><span 24</span> imagedestroy(<span $water</span><span );
</span><span 25</span> <span }
</span><span 26</span> mark_pic("./images/hee.jpg", "./images/gaolf.gif", 50, 200);

图片旋转

<span  1</span> <?<span PHP
</span><span  2</span> <span /*</span><span *
</span><span  3</span> <span * 图片旋转
</span><span  4</span> <span * edit by www.jbxue.com
</span><span  5</span> <span */</span>
<span  6</span> <span $back</span>=imagecreatefromjpeg("./images/hee.jpg"<span );
</span><span  7</span> 
<span  8</span> <span $new</span>=imagerotate(<span $back</span>, 45, 0<span );
</span><span  9</span> imagejpeg(<span $new</span>, "./images/hee9.jpg"<span );
</span><span 10</span> ?>

<span  1</span> <?<span php
</span><span  2</span> <span /*</span><span *
</span><span  3</span> <span * 图片水平翻转 垂直翻转
</span><span  4</span> <span * edit by www.jbxue.com
</span><span  5</span> <span */</span>
<span  6</span> <span function</span> turn_y(<span $background</span>, <span $newfile</span><span ){
</span><span  7</span> 
<span  8</span> <span $back</span>=imagecreatefromjpeg(<span $background</span><span );
</span><span  9</span> <span $width</span>=imagesx(<span $back</span><span );
</span><span 10</span> <span $height</span>=imagesy(<span $back</span><span );
</span><span 11</span> <span $new</span>=imagecreatetruecolor(<span $width</span>, <span $height</span><span );
</span><span 12</span> <span for</span>(<span $x</span>=0; <span $x</span> < <span $width</span>; <span $x</span>++<span ){
</span><span 13</span> imagecopy(<span $new</span>, <span $back</span>, <span $width</span>-<span $x</span>-1, 0, <span $x</span>, 0, 1, <span $height</span><span );
</span><span 14</span> <span }
</span><span 15</span> imagejpeg(<span $new</span>, <span $newfile</span><span );
</span><span 16</span> imagedestroy(<span $back</span><span );
</span><span 17</span> imagedestroy(<span $new</span><span );
</span><span 18</span> <span }
</span><span 19</span> <span function</span> turn_x(<span $background</span>, <span $newfile</span><span ){
</span><span 20</span> <span $back</span>=imagecreatefromjpeg(<span $background</span><span );
</span><span 21</span> <span $width</span>=imagesx(<span $back</span><span );
</span><span 22</span> <span $height</span>=imagesy(<span $back</span><span );
</span><span 23</span> <span $new</span>=imagecreatetruecolor(<span $width</span>, <span $height</span><span );
</span><span 24</span> <span for</span>(<span $y</span>=0; <span $y</span> < <span $height</span>; <span $y</span>++<span ){
</span><span 25</span> imagecopy(<span $new</span>, <span $back</span>,0, <span $height</span>-<span $y</span>-1, 0, <span $y</span>, <span $width</span>, 1<span );
</span><span 26</span> <span }
</span><span 27</span> imagejpeg(<span $new</span>, <span $newfile</span><span );
</span><span 28</span> imagedestroy(<span $back</span><span );
</span><span 29</span> imagedestroy(<span $new</span><span );
</span><span 30</span> <span }
</span><span 31</span> turn_y("./images/hee.jpg", "./images/hee11.jpg"<span );
</span><span 32</span> turn_x("./images/hee.jpg", "./images/hee12.jpg"<span );
</span><span 33</span> ?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440415.htmlTechArticle一、创建图片资源 imagecreatetruecolor(width,height); imagecreatefromgif(图片名称); imagecreatefrompng(图片名称); imagecreatefromjpeg(图片名称);画出各种图像...
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn