Home  >  Article  >  Backend Development  >  Example of how to compress image size and convert to jpg format using PHP

Example of how to compress image size and convert to jpg format using PHP

不言
不言Original
2018-06-01 13:46:051217browse

This article mainly introduces the method of PHP to compress image size and convert it to jpg format, involving PHP's related operating skills for image reading, calculation, conversion, output, etc. Friends who need it can refer to it

The example in this article describes how PHP compresses image size and converts it to jpg format. Share it with everyone for your reference, the details are as follows:

 $towidth/$srcH){
    $b = $toheight/$srcH;
  }else{
    $b = $towidth/$srcW;
  }
  //计算出图片缩放后的宽高
  // floor 舍去小数点部分,取整
  $new_w = floor($srcW*$b);
  $new_h = floor($srcH*$b);
  $dstX=$new_w;
  $dstY=$new_h;
  $ni=@imageCreateTrueColor($dstX,$dstY);
  @ImageCopyResampled($ni,$im,0,0,0,0,$dstX,$dstY,$srcW,$srcH);
  @ImageJpeg($ni,$dstFile,$quality);
  @imagedestroy($im);
  @imagedestroy($ni);
}
//ImageToJPG('源文件名','目标文件名',目标宽,目标高);
ImageToJPG('test2.png','test2.jpg',80,50);

##Related recommendations:

PHP7 is based on curl Implemented image upload function

PHP implements multiple image upload and single image upload functions

The above is the detailed content of Example of how to compress image size and convert to jpg format using PHP. For more information, please follow other related articles on the PHP Chinese website!

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