1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > PHP合成图片 文字居中 图片处理成圆角

PHP合成图片 文字居中 图片处理成圆角

时间:2019-09-26 17:27:59

相关推荐

PHP合成图片 文字居中 图片处理成圆角

public function share(){$userName = "小仙女哈";//昵称$userAvatar = './avatar.png';//默认头像//文字内容$content[] = "我正在学习PHP图片合成,";//34px$content[] = "快来为我加油助威吧!";//26pxob_clean();$types = array(1 => "gif", 2 => "jpeg", 3 => "png");//图片类型$bj_path = app()->baseDir . '/Public/test.jpg';//目标大图,背景图list($width0, $height0, $type0) = getimagesize($bj_path);//获取图片参数$createBgImg = "imagecreatefrom" . $types[$type0];$dstImage = $createBgImg($bj_path);$dstImage = $this->radiusImg($dstImage, $width0,$height0, 17);//把背景图的四直角处理成圆角//头像合并$avatarPath = '';if (!empty($userAvatar) && @fopen($userAvatar, 'r') != false) {$avatarPath = $this->roundImg($userAvatar, app()->baseDir . '/Data/Uploads/temp' . date('Y-m-d') . '/');//将头像处理成圆图list($width2, $height2, $type2) = getimagesize($avatarPath);//获取图片参数$createBgImg2 = "imagecreatefrom" . $types[$type2];$viceImage = $createBgImg2($avatarPath);//1:读取文件对象$src_w = imagesx($viceImage);//读取副图文件的宽$src_y = imagesy($viceImage);//读取副图文件的高$dst_x = $dst_y = $width2;//创建新的图片对象--由原图缩放尺寸$viceDstImg = imagecreatetruecolor($dst_x, $dst_y);//2:根据尺寸创建画布imagealphablending($viceDstImg, false);//混色模式-关闭imagesavealpha($viceDstImg, true);//图像时保存完整的alpha 透明度通道信息imagecopyresized($viceDstImg, $viceImage, 0, 0, 0, 0, $dst_x, $dst_y, $src_w, $src_y);//原图的copy-实现缩放imagecopy($dstImage, $viceDstImg, ($width0-$width2)/2, 192, 0, 0, $dst_x, $dst_y);//头像合并到主图 水平居中}//step1:昵称合并$vice_w = $vice_h = 500;//副图的画布大小$im = imagecreate($vice_w, $vice_h);//副图创建画布$white = imagecolorallocate($im, 255, 255, 255);imagecolortransparent($im, $white); //设置具体某种颜色为透明色$black = imagecolorallocate($im, 51, 51, 51);$nameSize = 20;$fontBox = imagettfbbox($nameSize, 0, app()->baseDir . '/msyh.ttf', $userName);//文字水平居中实质$name_x = ceil(($width0 - $fontBox[2]) / 2);//水平居中坐标$name_y = 192+80+50;//垂直坐标根据图片位置计算 192是图片顶端到头像的距离,80是头像的高度,50是头像和昵称的空白距离imagettftext($im, $nameSize, 0, 10, 100, $black, app()->baseDir . '/msyh.ttf', $userName); //字体设置部分linux和windows的路径可能不同imagettftext($im, $nameSize, 0, 11, 100, $black, app()->baseDir . '/msyh.ttf', $userName); //字体加粗 向右偏移一个像素imagecopymerge($dstImage, $im, $name_x, $name_y, 10, 10, $vice_w, $vice_h, 100);//副图合并到主图//step2:文字内容合并 同step1 foreach ($content as $key => $item) {if($key == 0){$im1 = imagecreate($vice_w, $vice_h);//副图创建画布$white1 = imagecolorallocate($im1, 255, 255, 255);imagecolortransparent($im1, $white1); //设置具体某种颜色为透明色$black1 = imagecolorallocate($im1, 51, 51, 51);$dstY = 530;imagettftext($im1, 20, 0, 10, 100, $black1, app()->baseDir . '/msyh.ttf', $item);imagettftext($im1, 20, 0, 11, 100, $black1, app()->baseDir . '/msyh.ttf', $item);$fontBox = imagettfbbox(20, 0, app()->baseDir . '/msyh.ttf', $item);//文字水平居中实质$dstX = ceil(($width0 - $fontBox[2]) / 2);imagecopymerge($dstImage, $im1, $dstX, $dstY, 0, 0, $vice_w, $vice_h, 100);}else{$im1 = imagecreate($vice_w, $vice_h);//副图创建画布$white1 = imagecolorallocate($im1, 255, 255, 255);imagecolortransparent($im1, $white1); //设置具体某种颜色为透明色$black1 = imagecolorallocate($im1, 102,102,102);$dstY = 565;imagettftext($im1, 18, 0, 10, 100, $black1, app()->baseDir . '/msyh.ttf', $item);imagettftext($im1, 18, 0, 11, 100, $black1, app()->baseDir . '/msyh.ttf', $item);$fontBox = imagettfbbox(18, 0, app()->baseDir . '/msyh.ttf', $item);//文字水平居中实质$dstX = ceil(($width0 - $fontBox[2]) / 2);imagecopymerge($dstImage, $im1, $dstX, $dstY, 0, 0, $vice_w, $vice_h, 100);}}//直接输出到浏览器app('response')->header('Content-Type', 'image/png');imagepng($dstImage);// 保存合成图片/*$dir = app()->baseDir . '/Data/Uploads/temp' . date('Y-m-d');if (!file_exists($dir)) {mkdir($dir, 0777, true);}$endImgUrl = "{$dir}/".uniqid().".png";//图片保存路径$save = "image" . $types[$type0];$save($dstImage, $endImgUrl);//保存文件*///删除图片!empty($avatarPath) && unlink($avatarPath);imagedestroy($dstImage);@imagedestroy($viceImage);@imagedestroy($viceDstImg);imagedestroy($im);imagedestroy($im1);}/*** 方图转圆形* @param string $original_path 图片地址* @param string $destFolder 保存的图片路径* @return string*/private function roundImg($original_path = '', $destFolder = './'){//获取参数$types = array(1 => "gif", 2 => "jpeg", 3 => "png");//图片类型list($width1, $height1, $type1) = getimagesize($original_path);$createBgImg1 = "imagecreatefrom" . $types[$type1];$viceImage = $createBgImg1($original_path);//1:读取文件对象$w = imagesx($viceImage);//读取副图文件的宽高$h = imagesy($viceImage);if (!file_exists($destFolder)) {mkdir($destFolder, 0777, true);}$dest_path = $destFolder . uniqid() . '.png';$src = imagecreatefromstring(file_get_contents($original_path));$newpic = imagecreatetruecolor($w, $h);imagealphablending($newpic, false);$transparent = imagecolorallocatealpha($newpic, 255, 255, 255, 127);$r = $w / 2;for ($x = 0; $x < $w; $x++) {for ($y = 0; $y < $h; $y++) {$c = imagecolorat($src, $x, $y);$_x = $x - $w / 2;$_y = $y - $h / 2;if ((($_x * $_x) + ($_y * $_y)) < ($r * $r)) {imagesetpixel($newpic, $x, $y, $c);} else {imagesetpixel($newpic, $x, $y, $transparent);}}}imagesavealpha($newpic, true);imagepng($newpic, $dest_path);imagedestroy($newpic);imagedestroy($src);imagedestroy($viceImage);return $dest_path;}/*** 将图片四直角处理成圆角** @param $src_img 目标图片* @param $width 宽* @param $height 高* @param int $radius 圆角半径* @return resource*/private function radiusImg($src_img, $width,$height, $radius = 17) {$w = &$width;$h = &$height;$img = imagecreatetruecolor($w, $h);//创建底图//这一句一定要有imagesavealpha($img, true);//设置是否保存透明图像资源//拾取一个完全透明的颜色,最后一个参数127为全透明$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);imagefill($img, 0, 0, $bg);$r = $radius; //圆角半径for ($x = 0; $x < $w; $x++) {for ($y = 0; $y < $h; $y++) {$rgbColor = imagecolorat($src_img, $x, $y);if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {//不在四角的范围内,直接画imagesetpixel($img, $x, $y, $rgbColor);} else {//在四角的范围内选择画//上左$y_x = $r; //圆心X坐标$y_y = $r; //圆心Y坐标if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {imagesetpixel($img, $x, $y, $rgbColor);}//上右$y_x = $w - $r; //圆心X坐标$y_y = $r; //圆心Y坐标if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {imagesetpixel($img, $x, $y, $rgbColor);}//下左$y_x = $r; //圆心X坐标$y_y = $h - $r; //圆心Y坐标if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {imagesetpixel($img, $x, $y, $rgbColor);}//下右$y_x = $w - $r; //圆心X坐标$y_y = $h - $r; //圆心Y坐标if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {imagesetpixel($img, $x, $y, $rgbColor);}}}}return $img;}

合成前背景图

合成后图片

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。