php背景,php图片背景填充实例
/**
* 添加背景
* @param string $src 圖片路徑
* @param int $w 背景圖像寬度
* @param int $h 背景圖像高度
* @return 返回加上背景的圖片
* **/
public function addBg($src,$w,$h)
{
$bg = imagecreatetruecolor($w,$h);
$white = imagecolorallocate($bg,255,255,255);
imagefill($bg,0,0,$white);//填充背景
//獲取目標圖片信息
$info=getimagesize($src);
$width=$info[0];//目標圖片寬度
$height=$info[1];//目標圖片高度
switch ($info[2]){
case 1:
$img = imagecreatefromgif($src);
break;
case 2:
$img = imagecreatefromjpeg($src);
break;
case 3:
$img = imagecreatefrompng($src);
break;
default:
exit('不支持的圖像格式');
break;
}
if($height < $h)
{
$x=0;
$y=($h-$height)/2;//垂直居中
}
if($width < $w)
{
$x=($w-$width)/2;//水平居中
$y=0;
}
if($height < $h && $width < $w){
$x = ($w-$width)/2;
$y = ($h-$height)/2;
}
imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);
imagejpeg($bg,$src,100);
imagedestroy($bg);
imagedestroy($img);
return $src;
}
總結
以上是生活随笔為你收集整理的php背景,php图片背景填充实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nginx配置多个php端口号,ngin
- 下一篇: laravel5.6 php,Larav