php gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码

2019-04-09 20:13:50王旭

{
case 1://gif
$src_im = imagecreatefromgif($all_path);//从源图片文件取得图像
break;
case 2://jpg
$src_im = imagecreatefromjpeg($all_path);
break;
case 3://png
$src_im = imagecreatefrompng($all_path);
break;
//case 6:
//$src_im=imagecreatefromwbmp($all_path);
//break;
default:
die("不支持的文件类型");
exit;
}
if($watermark == 1)
{
//$iinfo = getimagesize($all_path,$iinfo);
$dst_im = imagecreatetruecolor($src_w,$src_h);
//根据原图尺寸创建一个相同大小的真彩色位图
$white = imagecolorallocate($dst_im,255,255,255);//白
//给新图填充背景色
$black = imagecolorallocate($dst_im,0,0,0);//黑
$red = imagecolorallocate($dst_im,255,0,0);//红
$orange = imagecolorallocate($dst_im,255,85,0);//橙
imagefill($dst_im,0,0,$white);
imagecopymerge($dst_im,$src_im,0,0,0,0,$src_w,$src_h,100);//原图图像写入新建真彩位图中
//imagefilledrectangle($dst_im,1,$src_h-15,80,$src_h,$white);
switch($watertype)
{
case 1: //加水印字符串
imagestring($dst_im,5,5,$src_h-20,$waterstring,$orange);//文字水印,字体5号颜色橙色,位于背景图左下角
break;
case 2: //加水印图片
$lim_size = getimagesize($waterimg); //取得水印图像尺寸,信息
switch($lim_size[2]) //判断水印图片文件类型
{
case 1://gif
$src_lim = imagecreatefromgif($waterimg); //取得水印图像
break;
case 2://jpg
$src_lim = imagecreatefromjpeg($waterimg);
break;
case 3://png
$src_lim = imagecreatefrompng($waterimg);
break;
//case 6:
//$src_im=imagecreatefromwbmp($waterimg);
//break;
default:
die("不支持的文件类型");
exit;
}
$src_lw = ($src_w-$lim_size[0])/2; //水印位于背景图正中央width定位
$src_lh = ($src_h-$lim_size[1])/2; //height定位
imagecopymerge($dst_im,$src_lim,$src_lw,$src_lh,0,0,$lim_size[0],$lim_size[1],$waterclearly);// 合并两个图像,设置水印透明度$waterclearly
imagedestroy($src_lim);
break;
}
switch($src_type)
{
case 1:
imagegif($dst_im,$all_path,$imclearly);//生成gif文件,图片清晰度0-100
break;
case 2:
imagejpeg($dst_im,$all_path,$imclearly);//生成jpg文件,图片清晰度0-100
break;
case 3:
imagepng($dst_im,$all_path,$imclearly);//生成png文件,图片清晰度0-100
break;
//case 6:
//imagewbmp($dst_im,$all_path);
break;
}
//释放缓存
imagedestroy($dst_im);
}
if($smallmark == 1)
{
$sall_path = $path_sim.time().".".$filetype;
if (file_exists($sall_path))
{
echo "同名文件已经存在了";
exit;
}
if($src_w <= $dst_sw) // 原图尺寸 <= 缩略图尺寸
{
$dst_sim = imagecreatetruecolor($src_w,$src_h); //新建缩略图真彩位图
imagecopymerge($dst_sim,$src_im,0,0,0,0,$src_w,$src_h,100); //原图图像写入新建真彩位图中
}
if($src_w > $dst_sw) // 原图尺寸 > 缩略图尺寸
相关文章 大家在看