Home  >  Article  >  Backend Development  >  Complete collection of PHP image processing functions (summary of examples of scaling, cropping, zooming, flipping, rotating, transparency, and sharpening)_PHP Tutorial

Complete collection of PHP image processing functions (summary of examples of scaling, cropping, zooming, flipping, rotating, transparency, and sharpening)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 11:12:48952browse

1. Create image resources
imagecreatetruecolor(width,height);
imagecreatefromgif(picture name);
imagecreatefrompng(picture name);
imagecreatefromjpeg(picture name); draw various images imagegif (image resource, save path);
imagepng()
imagejpeg();

2. Get image attributes
imagesx(res//width
imagesy(res// height
getimagesize(filepath)
returns an array with four cells. Index 0 contains the pixel value of the image width, index 1 contains the pixel value of the image height, and index 2 is the tag of the image type: 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. These tags correspond to the new IMAGETYPE constant added in PHP 4.3.0. Index 3 is a text string with the content "height="yyy". width="xxx"" can be used directly for IMG tags.
Destroy image resources
imagedestroy (picture resources);

3. Transparency processing
PNG and jpeg transparent colors are normal. Only gif is abnormal
imagecolortransparent(resource image [,int color])//Set a certain color to a transparent color
imagecolorstotal()
imagecolorforindex();

4. Picture Cropping
imagecopyresized()
imagecopyresampled();

5. Add watermark (text, picture)
String encoding conversion string iconv ( string $in_charset , string $out_charset , string $str )

6. Image rotation
imagerotate(); // Image flip at a specified angle

7. Image flip
Flip along the X axis and Y axis

8. Sharpen
imagecolorsforindex()
imagecolorat()
Draw graphics on the picture $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>?>

Text watermark

<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);

Picture Rotation

<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.htmlTechArticle1. Create image resources imagecreatetruecolor(width,height); imagecreatefromgif(image name); imagecreatefrompng(image name); imagecreatefromjpeg(picture name); draw various images...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn