一、创建图片资源
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> ?>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment
