This article mainly introduces the PHP implementation of compressed image size and into a JPG format, involving PHP for image reading, calculation, conversion, output and other related operations skills, the need for friends can refer to the next
The example in this paper describes how to compress the image size and convert it to JPG format in PHP. Share to everyone for your reference, as follows:
<?phpfunction imagetojpg ($srcFile, $dstFile, $towidth, $toheight) {$quality = 80; $data = @GetImageSize ($srcFile); Switch ($data [' 2 ']) {Case 1: $im = Imagecreatefromgif ($srcFile); Break Case 2: $im = Imagecreatefromjpeg ($srcFile); Break Case 3: $im = Imagecreatefrompng ($srcFile); Break Case 6: $im = Imagecreatefrombmp ($srcFile); Break }//$dstX = $srcW = @ImageSX ($im); $dstY = $srcH = @ImageSY ($im); $srcW = @ImageSX ($im); $srcH = @ImageSY ($im); $towidth, $toheight if ($toheight/$srcW > $towidth/$srcH) {$b = $toheight/$srcH; }else{$b = $towidth/$srcW; }//Calculate the zoom//floor to the decimal point, take the whole $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 (' source filename ', ' target filename ', target width, target height); imagetojpg (' Test2.png ', ' test2.jpg ', 80,50);