Implementing equal-ratio compression of image size based on PHP, _PHP tutorial

WBOY
Release: 2016-07-12 08:57:57
Original
1156 people have browsed it

Based on PHP to achieve equal compression of image size,

Without further ado, I will directly post the relevant code of PHP equal compression of image size. The specific code is as follows :

<&#63;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);
} 
} 
Copy after login

The above code content is related to the implementation of equal-ratio compression of image size based on PHP introduced by the editor. The code is simple and easy to understand. If there is anything wrong with it, you are welcome to give your valuable opinions. The editor is very happy.

Articles you may be interested in:

  • PHP thumbnails are losslessly compressed in equal proportions, and can fill in blank areas with supplementary colors
  • php uses the imagick module to achieve image scaling, cropping, and compression Example
  • php script to realize batch compression of image file size
  • Two examples of PHP to realize image compression
  • PHP encapsulation class for adding watermarks to images, compression and cutting
  • PHP image watermark adding, compression and cutting encapsulation class implementation
  • PHP implementation of image uploading and compression
  • PHP implementation method of uploading images and compressing

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1106112.htmlTechArticle Based on PHP to achieve equal-ratio compression of image size. Without further ado, I will directly post PHP equal-ratio compressed images to everyone. The relevant code for size is as follows: php$im = imagecreatefr...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!