php 等比例缩小图片
本文章收藏了四款关于利用php等比例缩小图片代码函数,我们可定义图片宽度或高度对图片缩小或放大的图片宽度,好了看看四款实例那一款适合于你吧.
php 等比例缩小图片实例代码如下:
<?php 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; } //方法二 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); //方法三 /* 函数原型如下: 参数说明: $oldwidth:原图片宽度 $oldheight:原图片高度 $imgwidth:缩小或放大的图片宽度 $imgheight:缩小或放大的图片高度 返回:wwww.phprm.com 数组:arraysize ,其中索引为:width 和height 即:arraysize['width']、arraysize['height'] */ function getimgsize($oldwidth, $oldheight, $imgwidth, $imgheight) { //$oldwidth设置的宽度,$oldheight设置的高度,$imgwidth图片的宽度,$imgheight图片的高度; //单元格装得能装得进图片,则按图片的真实大小显示; 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) { //单元格高度为准; $aftersuoxiaowidth = $imgwidth * (1 - $suoxiaoheightper); $arraysize = array( 'width' => $aftersuoxiaowidth, 'height' => $oldheight ); return $arraysize; } else { //单元格宽度为准; $aftersuoxiaoheight = $imgheight * (1 - $suoxiaowidthper); $arraysize = array( 'width' => $oldwidth, 'height' => $aftersuoxiaoheight ); return $arraysize; } } }
本文链接:http://www.phprm.com/tuxiang/fs4474.html
收藏随意^^请保留教程地址.