1.数字生成图片

public function wordImg($uuid){

//创建一个长为500高为80的空白图片
$im =imagecreate(3000,100);
$background_color = imagecolorallocatealpha($im,255, 255, 255,127);
$col = imagecolorallocate($im, 0, 0, 0);
// $font="C:\Windows\Fonts\ARIALN.ttf"; //字体所放目录
$font=ROOT_PATH.'public/upload/font/ARIALN.TTF'; //字体所放目录
$come=iconv("gb2312","utf-8",$uuid);
imagettftext($im,50,0,430,60,$col,$font,$come); //写 TTF 文字到图中
$param = array();
$param['code'] =$uuid;
$spath = "shop/position/code/".implode('/', $param).".png";
$ssavePath = ROOT_PATH.'public/upload/'.$spath;
if(file_exists($ssavePath)){
return cmf_get_image_preview_url($spath);
}else{
$info = pathinfo($ssavePath);
if(!is_dir($info['dirname'])){
mkdir($info['dirname'],0755,true);
}
imagepng($im, $ssavePath);
return cmf_get_image_preview_url($spath);
}

}

2.合并图片
public function mergeImg($img1,$img2,$uuid){
$ename=getimagesize($img1);
$ename=explode('/',$ename['mime']);
$ext=$ename[1];
switch($ext){
case "png":
$image_1=imagecreatefrompng($img1);
break;
case "jpeg":
$image_1=imagecreatefromjpeg($img1);
break;
case "jpg":
$image_1=imagecreatefromjpeg($img1);
break;
case "gif":
$image_1=imagecreatefromgif($img1);
break;
}

$image_2 = imagecreatefrompng($img2); // 合成图片
$im3 = imagecreatetruecolor(imagesx($image_1), imagesy($image_1) + imagesy($image_2));
$_bg_color = imagecolorallocate($im3, 255,255,255); //创建颜色,返回颜色标识符
imagefill($im3, 0, 0, $_bg_color); //初始化图像背景为$_bg_color
imagecopymerge($im3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), 100);
imagecopymerge($im3, $image_2, 0, imagesy($image_1), 0, 0, imagesx($image_2), imagesy($image_2), 100);

$param = array();
$param['code'] =$uuid;
$path = "shop/position/".implode('/', $param).".png";
$savePath = ROOT_PATH.'public/upload/'.$path;
if(file_exists($savePath)){
return cmf_get_image_preview_url($path);
}else{
$info = pathinfo($savePath);
if(!is_dir($info['dirname'])){
mkdir($info['dirname'],0755,true);
}
imagepng($im3, $savePath);
return cmf_get_image_preview_url($path);
}

}

//获取当前时间戳,精确到毫秒
public static function getMillisecond(){
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectimes = substr($msectime,0,13);
}

//裁剪图片
public function cut($img,$uuid){
$src = imagecreatefromstring(file_get_contents($img));

//裁剪开区域左上角的点的坐标
$x = 0;
$y = 0;
//裁剪区域的宽和高
$percent = 0.85;
list($width, $height) = getimagesize($img);
$new_width = $width * $percent;
$new_height = $height * $percent;
//最终保存成图片的宽和高,和源要等比例,否则会变形

//将裁剪区域复制到新图片上,并根据源和目标的宽高进行缩放或者拉升
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $src, 0, 0, $x, $y, $new_width, $new_height, $width, $height);
//输出图片
header('Content-Type: image/jpeg');
$param = array();
$param['code'] =$uuid;
$path = "shop/position/".implode('/', $param).".png";
$savePath = ROOT_PATH.'public/upload/'.$path;
imagepng($new_image, $savePath);
return cmf_get_image_preview_url($path);
}
//最终合成
public function lastmarge($img,$uuid){
// 图片一
$path_1 = ROOT_PATH.'public/upload/shop/position/bg_img.jpg';
// 创建图片对象
$image_1 = imagecreatefromjpeg($path_1);
$image_2 = imagecreatefrompng($img);
// 合成图片
imagecopymerge($image_1, $image_2, 135, 385, 0, 0, imagesx($image_2), imagesy($image_2), 100);
// imagejpeg($src);
$param = array();
$param['code'] =$uuid;
$path = "shop/position/".implode('/', $param).".png";
$savePath = ROOT_PATH.'public/upload/'.$path;
imagepng($image_1, $savePath);
return ($savePath);
}


}

5.方变圆

//方变圆
public function circular($imgpath){
$ename=getimagesize($imgpath);
$ename=explode('/',$ename['mime']);
$ext=$ename[1];
$src_img = null;
switch($ext){
case "png":
$src_img=imagecreatefrompng($imgpath);
break;
case "jpeg":
$src_img=imagecreatefromjpeg($imgpath);
break;
case "jpg":
$src_img=imagecreatefromjpeg($imgpath);
break;
case "gif":
$src_img=imagecreatefromgif($imgpath);
break;
}
$wh = getimagesize($imgpath);
$w = $wh[0];
$h = $wh[1];
$w = min($w, $h);
$h = $w;
$img =imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w/2;//圆半径
$y_x = $r;//圆心X坐标
$y_y = $r;//圆心Y坐标
for($x=0;$x<$w;$x++){
for($y=0;$y<$h;$y++){
$rgbColor = imagecolorat($src_img,$x,$y);
if((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r)) {
imagesetpixel($img, $x, $y, $rgbColor);
}

}
}
$param = array();
$param['code'] =7;
$path = "shop/position/".implode('/', $param).".png";
$savePath = ROOT_PATH.'public/upload/'.$path;
//return $img;
imagepng($img, $savePath);
imagedestroy($img);
return cmf_get_image_preview_url($path);
// return $img;
}

6.合并

//合并背景图
public function test($img,$uuid){
//背景图片路径
$srcurl = ROOT_PATH.'public/upload/shop/position/bg_img.jpg';
//目标图片路径
$desurl = $img;

//创建源图的实例
$src = imagecreatefromstring(file_get_contents($srcurl));
//创建点的实例
$des = imagecreatefrompng($desurl);
//获取点图片的宽高
list($point_w, $point_h) = getimagesize($desurl);

//重点:png透明用这个函数
imagecopy($src, $des, 113, 400, 0, 0, $point_w, $point_h);
imagecopy($src, $des, 930, 1310, 0, 0, $point_w, $point_h);

header('Content-Type: image/jpeg');
$param = array();
$param['code'] =$uuid;
$path = "shop/position/".implode('/', $param).".png";
$savePath = ROOT_PATH.'public/upload/'.$path;
imagejpeg($src,$savePath);
imagedestroy($src);
imagedestroy($des);

}