PHP Image Compression Implementation code

Source: Internet
Author: User
  1. /*

  2. Functions: resizing pictures or creating thumbnails
  3. return: True/false
  4. Parameters:
  5. $Image need to adjust the picture (with the path)
  6. $Dw =450 Maximum width when adjusting, absolute width when thumbnail
  7. $Dh Maximum height for =450 adjustment, absolute height at thumbnail
  8. $Type = 1 1, adjust the size; 2, generate thumbnail image
  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. #如果需要生成缩略图, copy the original image and assign it to $image again.
  25. IF ($Type!=1) {
  26. Copy ($Image, Str_replace (".", "_x.", $Image));
  27. $Image =str_replace (".", "_x.", $Image);
  28. }

  29. #取得文件的类型, 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. #如果对象没有创建成功, the non-picture file is indicated

  43. IF (Empty ($IMG)) {
  44. #如果是生成缩略图的时候出错, you need to delete the files that have been copied
  45. IF ($Type!=1) {Unlink ($Image);}
  46. Returnfalse;
  47. }

  48. #如果是执行调整尺寸操作则

  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); #新建一个真彩色画布
  77. Imagecopyresampled ($NIMG, $IMG, 0,0,0,0, $width, $height, $w, $h); #重采样拷贝部分图像并调整大小
  78. Imagejpeg ($NIMG, $Image); #以JPEG格式将图像输出到浏览器或文件
  79. Returntrue;
  80. #如果是执行生成缩略图操作则
  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) {#高比较大
  88. $width = $Dw;
  89. $height = $h * $Dw/$w;
  90. $IntNH = $height-$Dh;
  91. Imagecopyresampled ($NIMG, $IMG, 0,-$IntNH/1.8,0,0, $Dw, $height, $w, $h);
  92. }else{#宽比较大
  93. $height = $Dh;
  94. $width = $w * $Dh/$h;
  95. $IntNW = $width-$Dw;
  96. Imagecopyresampled ($NIMG, $IMG,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
  97. }
  98. Imagejpeg ($NIMG, $Image);
  99. Returntrue;
  100. }
  101. }
  102. ?>

Copy Code

2. Invoking the example

  1. /**
  2. Image compression Processing
  3. */
  4. if ($_server[' Request_method ']== ' POST ') {
  5. if (!is_uploaded_file ($_files["photo"][tmp_name])) {
  6. echo "Picture does not exist";
  7. Exit ();
  8. }

  9. if (!is_dir (' img ')) {//path is created 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 '];//get 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 "Success!" ";
  24. IMG ($showphpath, 100,800,2);
  25. }
  26. echo "";
  27. }
  28. ?>
Copy Code
  • 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.