Home  >  Article  >  Backend Development  >  PHP image compression implementation code

PHP image compression implementation code

WBOY
WBOYOriginal
2016-07-25 09:00:07896browse
  1. /*

  2. Function: Adjust image size or generate thumbnail
  3. Return: True/False
  4. Parameters:
  5. $Image The image that needs to be adjusted (including path)
  6. $Dw =450 The maximum width when adjusting; the absolute width when thumbnailing
  7. $Dh=450 The maximum height when adjusting; the absolute height when thumbnailing
  8. $Type=1 1, adjust the size; 2, generate thumbnails
  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 need to generate a thumbnail, copy the original image and re-assign $Image
  25. IF($Type!=1){
  26. Copy($Image,Str_Replace(".","_x.",$Image));
  27. $Image=Str_Replace(".","_x.",$Image);
  28. }

  29. #Get the file type and create different objects according to 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. }< ;/p>
  42. #If the object is not created successfully, it means it is not an image file

  43. IF(Empty($Img)){
  44. #If there is an error when generating thumbnails, you need to delete the copied files
  45. IF($Type!=1){Unlink($Image);}
  46. ReturnFalse;
  47. }

  48. #If the resize operation is performed,

  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 new true color canvas
  77. ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$ w,$h); #Resample copy part of the image and resize it
  78. ImageJpeg($nImg,$Image); #Output the image to the browser or file in JPEG format
  79. ReturnTrue;
  80. #If you are performing a thumbnail generation operation, then
  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){#Height is larger
  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{ #Width Relatively large
  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. Call example

  1. The file types allowed to be uploaded are:
  2. /**
  3. Image compression processing
  4. */
  5. if($_SERVER ['REQUEST_METHOD']=='POST'){
  6. if(!is_uploaded_file($_FILES["photo"][tmp_name])){
  7. echo "The picture does not exist";
  8. exit();
  9. }
  10. if(!is_dir('img')){//Create the path if it does not exist

  11. mkdir('img');
  12. }

  13. $upfile=$_FILES ["photo"];

  14. $pinfo=pathinfo($upfile["name"]);
  15. $name=$pinfo['basename'];//File name
  16. $tmp_name=$upfile["tmp_name"];
  17. $file_type=$pinfo['extension'];//Get the file type
  18. $showphpath=$path.$name;
  19. if(in_array($upfile["type"],$phtypes)){
  20. echo "File type Not consistent! ";
  21. exit();
  22. }
  23. if(move_uploaded_file($tmp_name,$path.$name)){
  24. echo "Success! ";
  25. Img($showphpath,100,800,2);
  26. }
  27. echo "";
  28. }
  29. ?>
  30. < ;/html>

  31. Copy code
  32. Upload pictures


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn