PHP image operations: 3D images, scaling, rotating, cropping, and adding watermarks (3)
Source: http://www.ido321.com/887.html
5. PHP rotating Images
1:
2: Before Rotation
3:
4:
5:
6: header("content-type","text/html;charset=utf-8");
7:
8: /*
9: * The image is rotated along the Y axis. The png format is used as an example.
10: * @ param string $ filename url of the image
11: */
12: function turn_y($filename)
13: {
14:/* Create Image Resources */
15: $backy = imagecreatefrompng($filename);
16:
17:/* Get size */
18: $widthy = imagesx($backy);
19: $heighty = imagesy($backy);
20:
21:/* create a new image resource and save the flipped image */
22: $newy = imagecreatetruecolor($widthy, $heighty);
23:
24:/* flip along the Y axis, that is, copy the source image one by one from right to left to the new resource */
25: for ($i=0; $i < $widthy; $i++) {
26: imagecopy($newy,$backy,$widthy-$i-1,0,$i,0,1,$heighty);
27: }
28:
29:/* Save the flipped image */
30: imagepng($newy,'http://www.bkjia.com/uploads/allimg/141009/0425435558-1.png');
31:
32:/* release resources */
33: imagedestroy($backy);
34: imagedestroy($newy);
35: }
36:
37: /*
38: * The image is rotated along the X axis. The png format is used as an example.
39: * @ param string $ filename url of the image
40: */
41: function turn_x($filename)
42: {
43:/* Create Image Resources */
44: $backx = imagecreatefrompng($filename);
45:
46:/* Get the size */
47: $widthx = imagesx($backx);
48: $heightx = imagesy($backx);
49:
50:/* create a new image resource and save the flipped image */
51: $newx = imagecreatetruecolor($widthx, $heightx);
52:
53:/* flip along the X axis, that is, copy the source image one by one from top to bottom to the new resource in a pixel width */
54: for ($i=0; $i < $heightx; $i++) {
55: imagecopy($newx,$backx,0,$heightx-$i-1,0,$i,$widthx,1);
56: }
57:
58:/* Save the flipped image */
59: imagepng($newx,'http://www.bkjia.com/uploads/allimg/141009/0425432228-2.png');
60:
61:/* release resources */
62: imagedestroy($backx);
63: imagedestroy($newx);
64: }
65:/* call a function */
66: turn_y('http://www.bkjia.com/uploads/allimg/141009/0425435O8-0.png');
67: turn_x('http://www.bkjia.com/uploads/allimg/141009/0425435O8-0.png');
68: ?>
69:
70: rotate along the Y axis
71:
72:
73:
74: rotate along the X axis
75:
76:
Effect