php背景图片可以调小一点么,php – 如何在调整图像大小时填充白色背景

$final = imagecreatetruecolor($tn_w, $tn_h);

$backgroundColor = imagecolorallocate($final, 255, 255, 255);

imagefill($final, 0, 0, $backgroundColor);

//imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h);

imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h);

这是你的整个剧本(用肖像,风景和方形jpg测试):

function resize($source_image, $destination, $tn_w, $tn_h, $quality = 100, $wmsource = false)

{

$info = getimagesize($source_image);

$imgtype = image_type_to_mime_type($info[2]);

#assuming the mime type is correct

switch ($imgtype) {

case 'image/jpeg':

$source = imagecreatefromjpeg($source_image);

break;

case 'image/gif':

$source = imagecreatefromgif($source_image);

break;

case 'image/png':

$source = imagecreatefrompng($source_image);

break;

default:

die('Invalid image type.');

}

#Figure out the dimensions of the image and the dimensions of the desired thumbnail

$src_w = imagesx($source);

$src_h = imagesy($source);

#Do some math to figure out which way we'll need to crop the image

#to get it proportional to the new size, then crop or adjust as needed

$x_ratio = $tn_w / $src_w;

$y_ratio = $tn_h / $src_h;

if (($src_w <= $tn_w) && ($src_h <= $tn_h)) {

$new_w = $src_w;

$new_h = $src_h;

} elseif (($x_ratio * $src_h) < $tn_h) {

$new_h = ceil($x_ratio * $src_h);

$new_w = $tn_w;

} else {

$new_w = ceil($y_ratio * $src_w);

$new_h = $tn_h;

}

$newpic = imagecreatetruecolor(round($new_w), round($new_h));

imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);

$final = imagecreatetruecolor($tn_w, $tn_h);

$backgroundColor = imagecolorallocate($final, 255, 255, 255);

imagefill($final, 0, 0, $backgroundColor);

//imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h);

imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h);

#if we need to add a watermark

if ($wmsource) {

#find out what type of image the watermark is

$info = getimagesize($wmsource);

$imgtype = image_type_to_mime_type($info[2]);

#assuming the mime type is correct

switch ($imgtype) {

case 'image/jpeg':

$watermark = imagecreatefromjpeg($wmsource);

break;

case 'image/gif':

$watermark = imagecreatefromgif($wmsource);

break;

case 'image/png':

$watermark = imagecreatefrompng($wmsource);

break;

default:

die('Invalid watermark type.');

}

#if we're adding a watermark, figure out the size of the watermark

#and then place the watermark image on the bottom right of the image

$wm_w = imagesx($watermark);

$wm_h = imagesy($watermark);

imagecopy($final, $watermark, $tn_w - $wm_w, $tn_h - $wm_h, 0, 0, $tn_w, $tn_h);

}

if (imagejpeg($final, $destination, $quality)) {

return true;

}

return false;

}

resize('teszt2.jpg', 'out.jpg', 100, 100);

?>

out.jpg

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值