The GD Library is widely used in php. in this article, I use an instance program to explain how to use the GD library and php program to control images. The GD Library is widely used in php. in this article, I use an instance program to explain how to use the GD library and php program to control images.
1. 2.
3. $ FILENAME = "image_name ";
4.
5. // generate the image width
6. $ RESIZEWIDTH = 400;
7.
8. // generate the image height
9. $ RESIZEHEIGHT = 400;
10.
11.
12. function ResizeImage ($ im, $ maxwidth, $ maxheight, $ name ){
13. $ width = imagesx ($ im );
14. $ height = imagesy ($ im );
15. if ($ maxwidth & $ width> $ maxwidth) | ($ maxheight & $ height> $ maxheight )){
16. if ($ maxwidth & $ width> $ maxwidth ){
17. $ widthratio = $ maxwidth/$ width;
18. $ RESIZEWIDTH = true;
19 .}
20. if ($ maxheight & $ height> $ maxheight ){
21. $ heightratio = $ maxheight/$ height;
22. $ RESIZEHEIGHT = true;
23 .}
24. if ($ RESIZEWIDTH & $ RESIZEHEIGHT ){
25. if ($ widthratio <$ heightratio ){
26. $ ratio = $ widthratio;
27.} else {
28. $ ratio = $ heightratio;
29 .}
30.} elseif ($ RESIZEWIDTH ){
31. $ ratio = $ widthratio;
32.} elseif ($ RESIZEHEIGHT ){
33. $ ratio = $ heightratio;
34 .}
35. $ newwidth = $ width * $ ratio;
36. $ newheight = $ height * $ ratio;
37. if (function_exists ("imagecopyresampled ")){
38. $ newim = imagecreatetruecolor ($ newwidth, $ newheight );
39. imagecopyresampled ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
40.} else {
41. $ newim = imagecreate ($ newwidth, $ newheight );
42. imagecopyresized ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
43 .}
44. ImageJpeg ($ newim, $ name. ". jpg ");
45. ImageDestroy ($ newim );
46.} else {
47. ImageJpeg ($ im, $ name. ". jpg ");
48 .}
49 .}
50.
51.
52. if ($ _ FILES ['image'] ['size']) {
53. if ($ _ FILES ['image'] ['type'] = "image/pjpeg "){
54. $ im = imagecreatefromjpeg ($ _ FILES ['image'] ['tmp _ name']);
55.} elseif ($ _ FILES ['image'] ['type'] = "image/x-png "){
56. $ im = imagecreatefrompng ($ _ FILES ['image'] ['tmp _ name']);
57.} elseif ($ _ FILES ['image'] ['type'] = "image/gif "){
58. $ im = imagecreatefromgif ($ _ FILES ['image'] ['tmp _ name']);
59 .}
60. if ($ im ){
61. if (file_exists ("your filename.jpg ")){
62. unlink ("your filename.jpg ");
63 .}
64. ResizeImage ($ im, $ RESIZEWIDTH, $ RESIZEHEIGHT, $ FILENAME );
65. ImageDestroy ($ im );
66 .}
67 .}
68.
69.?>
70.
71. ">
72.
73.
78.
79.
80. |