>  기사  >  백엔드 개발  >  php实现图片压缩的二个例子

php实现图片压缩的二个例子

WBOY
WBOY원래의
2016-07-25 08:52:561016검색
  1. /**
  2. * desription 压缩图片
  3. * @param sting $imgsrc 图片路径
  4. * @param string $imgdst 压缩后保存路径
  5. */
  6. function image_png_size_add($imgsrc,$imgdst){
  7. list($width,$height,$type)=getimagesize($imgsrc);
  8. $new_width = ($width>600?600:$width)*0.9;
  9. $new_height =($height>600?600:$height)*0.9;
  10. switch($type){
  11. case 1:
  12. $giftype=check_gifcartoon($imgsrc);
  13. if($giftype){
  14. header('Content-Type:image/gif');
  15. $image_wp=imagecreatetruecolor($new_width, $new_height);
  16. $image = imagecreatefromgif($imgsrc);
  17. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  18. imagejpeg($image_wp, $imgdst,75);
  19. imagedestroy($image_wp);
  20. }
  21. break;
  22. case 2:
  23. header('Content-Type:image/jpeg');
  24. $image_wp=imagecreatetruecolor($new_width, $new_height);
  25. $image = imagecreatefromjpeg($imgsrc);
  26. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  27. imagejpeg($image_wp, $imgdst,75);
  28. imagedestroy($image_wp);
  29. break;
  30. case 3:
  31. header('Content-Type:image/png');
  32. $image_wp=imagecreatetruecolor($new_width, $new_height);
  33. $image = imagecreatefrompng($imgsrc);
  34. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  35. imagejpeg($image_wp, $imgdst,75);
  36. imagedestroy($image_wp);
  37. break;
  38. } // bbs.it-home.org
  39. }
  40. /**
  41. * desription 判断是否gif动画
  42. * @param sting $image_file图片路径
  43. * @return boolean t 是 f 否
  44. */
  45. function check_gifcartoon($image_file){
  46. $fp = fopen($image_file,'rb');
  47. $image_head = fread($fp,1024);
  48. fclose($fp);
  49. return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true;
  50. }
  51. ?>
复制代码

例2:

  1. /*
  2. 函数:调整图片尺寸或生成缩略图
  3. 返回:True/False
  4. 参数:
  5. $Image 需要调整的图片(含路径)
  6. $Dw=450 调整时最大宽度;缩略图时的绝对宽度
  7. $Dh=450 调整时最大高度;缩略图时的绝对高度
  8. $Type=1 1,调整尺寸; 2,生成缩略图
  9. $path='img/';//路径
  10. $phtypes=array(
  11. 'img/gif',
  12. 'img/jpg',
  13. 'img/jpeg',
  14. 'img/bmp',
  15. 'img/pjpeg',
  16. 'img/x-png'
  17. );
  18. Function Img($Image,$Dw=450,$Dh=450,$Type=1){
  19. IF(!File_Exists($Image)){
  20. Return False;
  21. }
  22. //如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值
  23. IF($Type!=1){
  24. Copy($Image,Str_Replace(".","_x.",$Image));
  25. $Image=Str_Replace(".","_x.",$Image);
  26. }
  27. //取得文件的类型,根据不同的类型建立不同的对象
  28. $ImgInfo=GetImageSize($Image);
  29. Switch($ImgInfo[2]){
  30. Case 1:
  31. $Img = @ImageCreateFromGIF($Image);
  32. Break;
  33. Case 2:
  34. $Img = @ImageCreateFromJPEG($Image);
  35. Break;
  36. Case 3:
  37. $Img = @ImageCreateFromPNG($Image);
  38. Break;
  39. }
  40. //如果对象没有创建成功,则说明非图片文件
  41. IF(Empty($Img)){
  42. //如果是生成缩略图的时候出错,则需要删掉已经复制的文件
  43. IF($Type!=1){Unlink($Image);}
  44. Return False;
  45. }
  46. //如果是执行调整尺寸操作则
  47. IF($Type==1){
  48. $w=ImagesX($Img);
  49. $h=ImagesY($Img);
  50. $width = $w;
  51. $height = $h;
  52. IF($width>$Dw){
  53. $Par=$Dw/$width;
  54. $width=$Dw;
  55. $height=$height*$Par;
  56. IF($height>$Dh){
  57. $Par=$Dh/$height;
  58. $height=$Dh;
  59. $width=$width*$Par;
  60. }
  61. }ElseIF($height>$Dh){
  62. $Par=$Dh/$height;
  63. $height=$Dh;
  64. $width=$width*$Par;
  65. IF($width>$Dw){
  66. $Par=$Dw/$width;
  67. $width=$Dw;
  68. $height=$height*$Par;
  69. }
  70. }Else{
  71. $width=$width;
  72. $height=$height;
  73. }
  74. $nImg = ImageCreateTrueColor($width,$height); //新建一个真彩色画布
  75. ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);//重采样拷贝部分图像并调整大小
  76. ImageJpeg ($nImg,$Image); //以JPEG格式将图像输出到浏览器或文件
  77. Return True;
  78. //如果是执行生成缩略图操作则
  79. }Else{
  80. $w=ImagesX($Img);
  81. $h=ImagesY($Img);
  82. $width = $w;
  83. $height = $h;
  84. $nImg = ImageCreateTrueColor($Dw,$Dh);
  85. IF($h/$w>$Dh/$Dw){ //高比较大
  86. $width=$Dw;
  87. $height=$h*$Dw/$w;
  88. $IntNH=$height-$Dh;
  89. ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
  90. }Else{ //宽比较大
  91. $height=$Dh;
  92. $width=$w*$Dh/$h;
  93. $IntNW=$width-$Dw;
  94. ImageCopyReSampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h);
  95. }
  96. ImageJpeg ($nImg,$Image);
  97. Return True;
  98. }
  99. }
  100. ?>
  101. 上传图片
  102. 允许上传的文件类型为:=implode(', ',$phtypes)?>
  103. if($_SERVER['REQUEST_METHOD']=='POST'){
  104. if (!is_uploaded_file($_FILES["photo"][tmp_name])){
  105. echo "图片不存在";
  106. exit();
  107. }
  108. if(!is_dir('img')){//路径若不存在则创建
  109. mkdir('img');
  110. }
  111. $upfile=$_FILES["photo"];
  112. $pinfo=pathinfo($upfile["name"]);
  113. $name=$pinfo['basename'];//文件名
  114. $tmp_name=$upfile["tmp_name"];
  115. $file_type=$pinfo['extension'];//获得文件类型
  116. $showphpath=$path.$name;
  117. if(in_array($upfile["type"],$phtypes)){
  118. echo "文件类型不符!";
  119. exit();
  120. }
  121. if(move_uploaded_file($tmp_name,$path.$name)){
  122. echo "成功!";
  123. Img($showphpath,100,800,2);
  124. }
  125. echo "php实现图片压缩的二个例子 ";
  126. }
  127. ?>
复制代码


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.