Php image compression implementation code

Source: Internet
Author: User
Php image compression implementation code

  1. /*

  2. Function: adjust the image size or generate a thumbnail.
  3. Return value: True/False
  4. Parameters:
  5. $ Image: the Image to be adjusted (including the path)
  6. $ Dw = 450 max width for resizing; absolute width for thumbnails
  7. $ Dh = 450 max. height when adjusting; absolute height when thumbnails
  8. $ Type = 1 1, adjust the size; 2, generate a thumbnail
  9. Site http://bbs.it-home.org
  10. */

  11. $ Path = 'IMG/'; // path

  12. $ Phtypes = array (
  13. 'IMG/GIF ',
  14. 'IMG/jpg ',
  15. 'IMG/jpeg ',
  16. 'IMG/bmp ',
  17. 'IMG/pjpeg ',
  18. 'IMG/x-png'
  19. );

  20. FunctionImg ($ Image, $ Dw = 450, $ Dh = 450, $ Type = 1 ){

  21. IF (! File_Exists ($ Image )){
  22. ReturnFalse;
  23. }
  24. # If you want to generate a thumbnail, copy the source Image and assign a value to $ Image.
  25. IF ($ Type! = 1 ){
  26. Copy ($ Image, Str_Replace (".", "_ x.", $ Image ));
  27. $ Image = Str_Replace (".", "_ x.", $ Image );
  28. }

  29. # Obtain the file type and create different objects based on different types

  30. $ ImgInfo = GetImageSize ($ Image );
  31. Switch ($ ImgInfo [2]) {
  32. Case1:
  33. $ Img = @ ImageCreateFromGIF ($ Image );
  34. Break;
  35. Case2:
  36. $ Img = @ ImageCreateFromJPEG ($ Image );
  37. Break;
  38. Case3:
  39. $ Img = @ ImageCreateFromPNG ($ Image );
  40. Break;
  41. }

  42. # If the object is not created successfully, it indicates that the object is not an image file.

  43. IF (Empty ($ Img )){
  44. # If an error occurs when generating a thumbnail, delete the copied file.
  45. IF ($ Type! = 1) {Unlink ($ Image );}
  46. ReturnFalse;
  47. }

  48. # If you want to adjust the size

  49. IF ($ Type = 1 ){
  50. $ W = ImagesX ($ Img );
  51. $ H = ImagesY ($ Img );
  52. $ Width = $ w;
  53. $ Height = $ h;
  54. IF ($ width> $ Dw ){
  55. $ Par = $ Dw/$ width;
  56. $ Width = $ Dw;
  57. $ Height = $ height * $ Par;
  58. IF ($ height> $ Dh ){
  59. $ Par = $ Dh/$ height;
  60. $ Height = $ Dh;
  61. $ Width = $ width * $ Par;
  62. }
  63. } ElseIF ($ height> $ Dh ){
  64. $ Par = $ Dh/$ height;
  65. $ Height = $ Dh;
  66. $ Width = $ width * $ Par;
  67. IF ($ width> $ Dw ){
  68. $ Par = $ Dw/$ width;
  69. $ Width = $ Dw;
  70. $ Height = $ height * $ Par;
  71. }
  72. } Else {
  73. $ Width = $ width;
  74. $ Height = $ height;
  75. }
  76. $ NImg = ImageCreateTrueColor ($ width, $ height); # Create a true color canvas
  77. ImageCopyReSampled ($ nImg, $ Img, $ width, $ height, $ w, $ h); # Copy part of the image and resize it.
  78. ImageJpeg ($ nImg, $ Image); # output the Image to a browser or file in JPEG format
  79. ReturnTrue;
  80. # If you want to generate a thumbnail
  81. } Else {
  82. $ W = ImagesX ($ Img );
  83. $ H = ImagesY ($ Img );
  84. $ Width = $ w;
  85. $ Height = $ h;

  86. $ NImg = ImageCreateTrueColor ($ Dw, $ Dh );

  87. IF ($ h/$ w> $ Dh/$ Dw) {# relatively high
  88. $ Width = $ Dw;
  89. $ Height = $ h * $ Dw/$ w;
  90. $ IntNH = $ height-$ Dh;
  91. ImageCopyReSampled ($ nImg, $ Img, 0,-$ IntNH/1.8, $ Dw, $ height, $ w, $ h );
  92. } Else {# relatively wide
  93. $ Height = $ Dh;
  94. $ Width = $ w * $ Dh/$ h;
  95. $ IntNW = $ width-$ Dw;
  96. ImageCopyReSampled ($ nImg, $ Img,-$ IntNW/1.8, 0, 0, $ width, $ Dh, $ w, $ h );
  97. }
  98. ImageJpeg ($ nImg, $ Image );
  99. ReturnTrue;
  100. }
  101. }
  102. ?>

2. call example

  1. /**
  2. Image compression
  3. */
  4. If ($ _ SERVER ['request _ method'] = 'post '){
  5. If (! Is_uploaded_file ($ _ FILES ["photo"] [tmp_name]) {
  6. Echo "the image does not exist ";
  7. Exit ();
  8. }

  9. If (! Is_dir ('IMG ') {// create a path if it does not exist

  10. Mkdir ('IMG ');
  11. }

  12. $ Upfile = $ _ FILES ["photo"];

  13. $ Pinfo = pathinfo ($ upfile ["name"]);
  14. $ Name = $ pinfo ['basename']; // file name
  15. $ Tmp_name = $ upfile ["tmp_name"];
  16. $ File_type = $ pinfo ['extension']; // Obtain the file type
  17. $ Showphpath = $ path. $ name;
  18. If (in_array ($ upfile ["type"], $ phtypes )){
  19. Echo "file type does not match! ";
  20. Exit ();
  21. }
  22. If (move_uploaded_file ($ tmp_name, $ path. $ name )){
  23. Echo "successful! ";
  24. Img ($ showphpath, 100,800, 2 );
  25. }
  26. Echo "";
  27. }
  28. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.