Achieves proportional compression of image size based on PHP,

Source: Internet
Author: User
Tags imagejpeg

Achieves proportional compression of image size based on PHP,

If you don't talk much about it, just paste the php code to compress the image size. The specific code is as follows:

<?php$im = imagecreatefromjpeg('D:\phpplace\.jpeg');resizeImage($im,,,'xinde','.jpg');function resizeImage($im,$maxwidth,$maxheight,$name,$filetype){$pic_width = imagesx($im);$pic_height = imagesy($im);echo "start-----------------" ;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);imagecopyresampled($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);}else{$newim = imagecreate($newwidth,$newheight);imagecopyresized($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);}$name = $name.$filetype;imagejpeg($newim,$name);imagedestroy($newim);}else{$name = $name.$filetype;imagejpeg($im,$name);} } 

The above code content is related to PHP-based implementation of proportional compression of image size. The code is easy to understand and cannot be written properly. Thank you for your valuable comments, xiaobian is very happy.

Articles you may be interested in:
  • PHP thumbnails and Other proportional lossless compression, can fill the blank area to supplement the color
  • Php uses the imagick module to scale, crop, and compress images.
  • Php script for batch compression of image files
  • Two PHP Image Compression instances
  • PHP adds a watermark compression and cutting encapsulation class to the image
  • Php image watermark adding, compression, and cutting encapsulation class implementation
  • PHP uploads and compresses Images
  • How to upload and compress images in php

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.