Home > Backend Development > PHP Tutorial > php proportional reduction of images_PHP tutorial

php proportional reduction of images_PHP tutorial

WBOY
Release: 2016-07-13 10:45:35
Original
1100 people have browsed it

php scale down the image proportionally This article collects four code functions about using PHP to reduce image proportions. We can define the image width or height to reduce or enlarge the image width. Well, let’s see which of the four examples is suitable for you.

php tutorial to scale down images
This article collects four code functions about using PHP to reduce image proportions. We can define the image width or height to reduce or enlarge the image width. Well, let’s see which of the four examples is suitable for you.
*/

function imageresize2($width, $height, $targetw, $targeth)
{
$percentage = 1;
if (($width > $targetw) || ($height > $targeth))
{
$width_diff = $width - $targetw;
$height_diff = $height - $targeth;

if ($width_diff >= $height_diff)
{
$percentage = ($targetw / $width);
}
else
{
$percentage = ($targeth / $height);
}
}
//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);
$resize[0] = $width;
$resize[1] = $height;
return $resize;
}

//Method 2

if (!$max_width)
$max_width = 240;
if (!$max_height)
$max_height = 200;

$size = getimagesize($image);
$width = $size[0];
$height = $size[1];

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if ( ($width <= $max_width) && ($height <= $max_height) ) {
$tn_width = $width;
$tn_height = $height;
}
else if (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}

$src = imagecreatefrompng($image);
$dst = imagecreate($tn_width,$tn_height);
imagecopyresized($dst, $src, 0, 0, 0, 0,
$tn_width,$tn_height,$width,$height);
header("content-type: image/png");
imagepng($dst, null, -1);
imagedestroy($src);
imagedestroy($dst);

//Method 3

/*
The function prototype is as follows:
Parameter description:
$oldwidth: original image width
$oldheight: original image height
$imgwidth: Reduced or enlarged image width
$imgheight: reduced or enlarged image height
Return: wwww.bKjia.c0m
Array: arraysize, where the indexes are: width and height, namely: arraysize['width'], arraysize['height']
*/
function getimgsize($oldwidth,$oldheight,$imgwidth,$imgheight)
{
//The width set by $oldwidth, the height set by $oldheight, the width of the $imgwidth image, and the height of the $imgheight image;

//If the cell can fit the image, it will be displayed according to the actual size of the image;
if($imgwidth<=$oldwidth&&$imgheight<=$oldheight)
{
$arraysize=array('width'=>$imgwidth,'height'=>$imgheight);
return $arraysize;
}
else
{
$suoxiaowidth=$imgwidth-$oldwidth;
$suoxiaoheight=$imgheight-$oldheight;
$suoxiaoheightper=$suoxiaoheight/$imgheight;
$suoxiaowidthper=$suoxiaowidth/$imgwidth;
if($suoxiaoheightper>=$suoxiaowidthper)
{
//Cell height shall prevail;
$aftersuoxiaowidth=$imgwidth*(1-$suoxiaoheightper);
$arraysize=array('width'=>$aftersuoxiaowidth,'height'=>$oldheight);
return $arraysize;
}
else
{
//Cell width shall prevail;
$aftersuoxiaoheight=$imgheight*(1-$suoxiaowidthper);
$arraysize=array('width'=>$oldwidth,'height'=>$aftersuoxiaoheight);
return $arraysize;
}
}
}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633026.htmlTechArticlephp proportional reduction of images This article collects four code functions about using php to proportionally reduce images, we can define Image width or height. The image width to reduce or enlarge the image. Okay...
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