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.111com.net

数组: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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值