PHP and other proportional compression pictures

Source: Internet
Author: User

<?PHPfunctionResizeimage ($im,$maxwidth,$maxheight,$name,$filetype) {  $pic _width= Imagesx ($im); $pic _height= Imagesy ($im); if(($maxwidth&&$pic _width>$maxwidth) || ($maxheight&&$pic _height>$maxheight))  {   if($maxwidth&&$pic _width>$maxwidth)   {    $widthratio=$maxwidth/$pic _width; $resizewidth _tag=true; }    if($maxheight&&$pic _height>$maxheight)   {    $heightratio=$maxheight/$pic _height; $resizeheight _tag=true; }    if($resizewidth _tag&&$resizeheight _tag)   {    if($widthratio<$heightratio)     $ratio=$widthratio; Else     $ratio=$heightratio; }    if($resizewidth _tag&&!$resizeheight _tag)    $ratio=$widthratio; if($resizeheight _tag&&!$resizewidth _tag)    $ratio=$heightratio; $newwidth=$pic _width*$ratio; $newheight=$pic _height*$ratio; if(function_exists("imagecopyresampled"))   {    $newim= Imagecreatetruecolor ($newwidth,$newheight);//PHP System FunctionsImagecopyresampled ($newim,$im, 0,0,0,0,$newwidth,$newheight,$pic _width,$pic _height);//PHP System Functions   }   Else   {    $newim= Imagecreate ($newwidth,$newheight); Imagecopyresized ($newim,$im, 0,0,0,0,$newwidth,$newheight,$pic _width,$pic _height); }    $name=$name.$filetype; Imagejpeg ($newim,$name); Imagedestroy ($newim); }  Else  {   $name=$name.$filetype; Imagejpeg ($im,$name); } }//How to use:$im=imagecreatefromjpeg ("./20140416103023202.jpg");//parameter is the storage path of the picture$maxwidth= "600";//set the maximum width of a picture$maxheight= "400";//set the maximum height of a picture$name= "123";//the name of the picture, pick it up.$filetype= ". jpg";//type of pictureResizeimage ($im,$maxwidth,$maxheight,$name,$filetype);//Call the function above

Online excerpt, not yet tested.

PHP and other proportional compression pictures

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.