php gd画填充矩形,php GD添加填充到图像

>打开原始图像

>创建一个新的空白图像.

>用您想要的背景颜色填充新图像

>使用ImageCopyResampled调整大小并复制以新图像为中心的原始图像

>保存新图像

您也可以使用switch语句

$img = imagecreatefromstring( file_get_contents ("path/to/image") );

这将自动检测图像类型(如果您的安装支持imagetype)

更新了代码示例

$orig_filename = 'c:\temp\380x253.jpg';

$new_filename = 'c:\temp\test.jpg';

list($orig_w, $orig_h) = getimagesize($orig_filename);

$orig_img = imagecreatefromstring(file_get_contents($orig_filename));

$output_w = 200;

$output_h = 200;

// determine scale based on the longest edge

if ($orig_h > $orig_w) {

$scale = $output_h/$orig_h;

} else {

$scale = $output_w/$orig_w;

}

// calc new image dimensions

$new_w = $orig_w * $scale;

$new_h = $orig_h * $scale;

echo "Scale: $scale
";

echo "New W: $new_w
";

echo "New H: $new_h
";

// determine offset coords so that new image is centered

$offest_x = ($output_w - $new_w) / 2;

$offest_y = ($output_h - $new_h) / 2;

// create new image and fill with background colour

$new_img = imagecreatetruecolor($output_w, $output_h);

$bgcolor = imagecolorallocate($new_img, 255, 0, 0); // red

imagefill($new_img, 0, 0, $bgcolor); // fill background colour

// copy and resize original image into center of new image

imagecopyresampled($new_img, $orig_img, $offest_x, $offest_y, 0, 0, $new_w, $new_h, $orig_w, $orig_h);

//save it

imagejpeg($new_img, $new_filename, 80);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值