PHP對(duì)圖像的旋轉(zhuǎn)
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
73
74
75
76
|
<div> <h4>旋轉(zhuǎn)之前</h4> <img src= "1.png" style= "border:1px solid red;" > </div> <?php header( "content-type" , "text/html;charset=utf-8" ); /* *圖片沿y軸旋轉(zhuǎn),以png格式為例 *@param string $filename 圖片的url */ function turn_y( $filename ) { /*創(chuàng)建圖片資源*/ $backy = imagecreatefrompng( $filename ); /*獲取大小*/ $widthy = imagesx( $backy ); $heighty = imagesy( $backy ); /*創(chuàng)建新的圖片資源,保存翻轉(zhuǎn)后的圖片*/ $newy = imagecreatetruecolor( $widthy , $heighty ); /*沿著y軸翻轉(zhuǎn),就是將原圖從右向左按一個(gè)像素寬度向新資源中逐個(gè)復(fù)制*/ for ( $i =0; $i < $widthy ; $i ++) { imagecopy( $newy , $backy , $widthy - $i -1,0, $i ,0,1, $heighty ); } /*保存翻轉(zhuǎn)后的圖片*/ imagepng( $newy , 'test3.png' ); /*釋放資源*/ imagedestroy( $backy ); imagedestroy( $newy ); } /* *圖片沿x軸旋轉(zhuǎn),以png格式為例 *@param string $filename 圖片的url */ function turn_x( $filename ) { /*創(chuàng)建圖片資源*/ $backx = imagecreatefrompng( $filename ); /*獲取大小*/ $widthx = imagesx( $backx ); $heightx = imagesy( $backx ); /*創(chuàng)建新的圖片資源,保存翻轉(zhuǎn)后的圖片*/ $newx = imagecreatetruecolor( $widthx , $heightx ); /*沿著x軸翻轉(zhuǎn),就是將原圖從上到下按一個(gè)像素寬度向新資源中逐個(gè)復(fù)制*/ for ( $i =0; $i < $heightx ; $i ++) { imagecopy( $newx , $backx ,0, $heightx - $i -1,0, $i , $widthx ,1); } /*保存翻轉(zhuǎn)后的圖片*/ imagepng( $newx , 'test4.png' ); /*釋放資源*/ imagedestroy( $backx ); imagedestroy( $newx ); } /*調(diào)用函數(shù)*/ turn_y( '1.png' ); turn_x( '1.png' ); ?> <div style= "float:left" > <h4>沿著y軸旋轉(zhuǎn)</h4> <img src= "test3.png" style= "border:1px solid red;" > </div> <div style= "float:left" > <h4>沿著x軸旋轉(zhuǎn)</h4> <img src= "test4.png" style= "border:1px solid red;" > </div> |