php生成宣传海报图片
public function test(){
$imageUrl = 'https://car.xintutc.com/storage/avatar/20210911/e4d6ba186906453c3388f0ccf1ba7672.png';
$codeUrl = 'https://car.xintutc.com/upload/qrcode/16311776586962317.png';
$avatarUrl = 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIC73HpXIIMjDnLbGU1tOkuuxlZzyOqgcvI8icfsoaibzY5nAq0mYT4h5lEDNicSBJjpicXwD3MicibBicEQ/132';
$ttf = '/static/yahei.ttc';
$rs = generateImg('西安北客站','太白南路','跨城拼车',3,120,'宋师傅',$avatarUrl,'131****5555',$codeUrl,$imageUrl,'544',$ttf);
$this->success('请求成功',$rs);
}
function generateImg($start,$end,$type,$car_lave,$price,$name,$avatarUrl,$mobile,$codeUrl='',$imageUrl='',$width = 1080,$ttf = 'simhei.ttf'){
$height = $width*1.81;
$padd = $width/24.73;
$code_width = $width/4;
$avatar_width = $width/8;
$image_width = $width-2*$padd;
$canvas = imageCreatetruecolor($width, $height);
$color = imagecolorallocate($canvas, 255, 255, 255);
imagefill($canvas, 0, 0, $color);
$white= imagecolorallocate($canvas,255,255,255);
$gray= imagecolorallocate($canvas,105,105,105);
$black= imagecolorallocate($canvas,35,35,35);
$blue= imagecolorallocate($canvas,58,170,216);
$arr = explode('.', $codeUrl);
$suffix = end($arr);
if ($suffix == 'jpg' || $suffix == 'jpeg') $code = imagecreatefromjpeg($codeUrl);
if ($suffix == 'png') $code = imagecreatefrompng($codeUrl);
if ($suffix == 'gif') $code = imagecreatefromgif($codeUrl);
if ($suffix == 'bmp') $code = imagecreatefrombmp($codeUrl);
$code_oldW = imagesx($code);
$code_oldH = imagesy($code);
$percent1 = $code_oldH/$code_oldW;
$arr1 = explode('.', $imageUrl);
$suffix = end($arr1);
if ($suffix == 'jpg' || $suffix == 'jpeg') $image = imagecreatefromjpeg($imageUrl);
if ($suffix == 'png') $image = imagecreatefrompng($imageUrl);
if ($suffix == 'gif') $image = imagecreatefromgif($imageUrl);
if ($suffix == 'bmp') $image = imagecreatefrombmp($imageUrl);
$image_oldW = imagesx($image);
$image_oldH = imagesy($image);
$percent2 = $image_oldH/$image_oldW;
imagecopyresampled( $canvas , $image , $padd , $padd , 0 , 0 , $image_width , $image_width*$percent2 , $image_oldW , $image_oldH);
imagecopyresampled( $canvas , $code , 364 , 790 , 0 , 0 , $code_width , $code_width * $percent1 , $code_oldW , $code_oldH);
imagettftext ( $canvas , 10, 0 , 101 , 640, $white , $ttf , date('m月d日'));
imagettftext ( $canvas , 10, 0 , 140 , 673, $gray , $ttf , $start);
imagettftext ( $canvas , 10, 0 , 140 , 700, $gray , $ttf , $end);
imagettftext ( $canvas , 12, 0 , 175 , 640, $black , $ttf , $type);
imagettftext ( $canvas , 10, 0 , 140 , 727, $gray , $ttf , $car_lave);
imagettftext ( $canvas , 30, 0 , 370 , 729, $blue , $ttf , $price);
imagettftext ( $canvas , 14, 0 , 126 , 820, $black , $ttf , $name);
imagettftext ( $canvas , 10, 0 , 145 , 845, $black , $ttf , $mobile);
$name = mt_rand(0,999).uniqid();
$filename = mb_substr($name,0,5).'.jpg';
if (!file_exists($filename)) {
imagejpeg($canvas, './storage/share/'.$filename);
imagedestroy($canvas);
} else {
imagedestroy($canvas);
}
return 'https://car.xintutc.com/storage/share/'.$filename;
}
|