本文章收藏了四款关于利用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);
- }
- }
-
- $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);
-
-
-
-
-
-
-
-
-
-
-
-
-
- function getimgsize($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;
- }
- }
- }
|