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

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

Jul 20, 2016 am 11:12 AM
image php 函数 创建 剪裁 图像处理 图片资源 大全 实例 总结 旋转 缩放 翻转 透明 锐化

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

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

登录后复制

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?<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>?>

登录后复制

文字水印

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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

登录后复制

图片旋转

1

2

3

4

5

6

7

8

9

10

<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> ?>

登录后复制

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<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(图片名称);画出各种图像...
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

在本章中,我们将了解CakePHP中的环境变量、常规配置、数据库配置和电子邮件配置。

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

See all articles