PHP to compress image size and convert to JPG format example _php tips

Source: Internet
Author: User
Tags redis cluster
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);

Articles you may be interested in:

Example of how PHP remembers state when searching _php tips

Laravel Project full steps to deploy a redis cluster using twemproxy PHP instance

PHP7 how to open opcode to build powerful performance PHP tips








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.