最近经常有小伙伴私信询问php如何实现水印平铺?相关的问题,今天,我爱编程网小编整理了以下内容,希望可以对大家有所帮助。
本文目录一览:

谁有打水印代码???PHP
非常好用的加水印程序
//watermark(源图,生成文件,生成位置,水印文件,水印文本,背景色)
function watermark($source, $target = '', $w_pos = 0, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc')
{
if(!$this->watermark_enable || !$this->check($source)) return false;
if(!$target) $target = $source;
if ($w_img == '' && $w_text == '')
$w_img = $this->w_img;
$source_info = getimagesize($source);
$source_w = $source_info[0]; //获取宽
$source_h = $source_info[1]; //获取高
if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false; //宽和高达不到要求直接返回
switch($source_info[2]) //新建图片
{
case 1 :
$source_img = imagecreatefromgif($source);
break;
case 2 :
$source_img = imagecreatefromjpeg($source);
break;
case 3 :
$source_img = imagecreatefrompng($source);
break;
default :
return false;
}
if(!empty($w_img) && file_exists($w_img)) //水印文件
{
$ifwaterimage = 1; //是否水印图
$water_info = getimagesize($w_img); //水印信息
$width = $water_info[0];
$height = $water_info[1];
switch($water_info[2])
{
case 1 :
$water_img = imagecreatefromgif($w_img);
break;
case 2 :
$water_img = imagecreatefromjpeg($w_img);
break;
case 3 :
$water_img = imagecreatefrompng($w_img);
break;
default :
return;
}
}
else
{
$ifwaterimage = 0;
//imagettfbbox 本函数计算并返回一个包围着 TrueType 文本范围的虚拟方框的像素大小。
//imagettfbbox ( 字体大小, 字体角度, 字体文件,文件 )
$temp = imagettfbbox(ceil($w_font*1.2), 0, $this->fontfile, $w_text);//取得使用 truetype 字体的文本的范围
$width = $temp[4] - $temp[6]; //右上角 X 位置 - 左上角 X 位置
$height = $temp[3] - $temp[5]; //右下角 Y 位置- 右上角 Y 位置
unset($temp);
}
switch($w_pos)
{
case 0: //随机位置
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
case 1: //左上角
$wx = 5;
$wy = 5;
break;
case 2: //上面中间位置
$wx = ($source_w - $width) / 2;
$wy = 0;
break;
case 3: //右上角
$wx = $source_w - $width;
$wy = 0;
break;
case 4: //左面中间位置
$wx = 0;
$wy = ($source_h - $height) / 2;
break;
case 5: //中间位置
$wx = ($source_w - $width) / 2;
$wy = ($source_h - $height) / 2;
break;
case 6: //底部中间位置
$wx = ($source_w - $width) / 2;
$wy = $source_h - $height;
break;
case 7: //左下角
$wx = 0;
$wy = $source_h - $height;
break;
case 8: //右面中间位置
$wx = $source_w - $width;
$wy = ($source_h - $height) /2;
break;
case 9: //右下角
$wx = $source_w - $width;
$wy = $source_h - $height ;
break;
default: //随机
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
}
if($ifwaterimage) //如果有水印图
{
//imagecopymerge 拷贝并合并图像的一部分
//参数(源图,水印图,拷贝到源图x位置,拷贝到源图y位置,从水印图x位置,从水印图y位置,高,宽,透明度)
imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
}
else
{
if(!empty($w_color) && (strlen($w_color)==7))
{
$r = hexdec(substr($w_color,1,2)); //获取红色
$g = hexdec(substr($w_color,3,2)); //获取绿色
$b = hexdec(substr($w_color,5)); //获取蓝色
}
else
{
return;
}
//imagecolorallocate 基于调色板的图像填充背景色
//imagestring 水平地画一行字符串
//imagestring(源图,字体大小,位置X,位置Y,文字,颜色)
//参数($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);
//imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
}
//输出到文件或者浏览器
switch($source_info[2])
{
case 1 :
imagegif($source_img, $target); //以 GIF 格式将图像输出到浏览器或文件
break;
case 2 :
imagejpeg($source_img, $target, $this->w_quality); //以 JPEG 格式将图像输出到浏览器或文件
break;
case 3 :
imagepng($source_img, $target); //以 PNG 格式将图像输出到浏览器或文件
break;
default :
return;
}
if(isset($water_info))
{
unset($water_info); //销毁
}
if(isset($water_img))
{
imagedestroy($water_img); //销毁
}
unset($source_info);
imagedestroy($source_img);
return true;
}
//gd库必须存在,后缀为jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
function check($image)
{
return extension_loaded('gd') &&
preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) &&
file_exists($image) &&
function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
}
}

求一段添加水印的代码 phpd的
<?php functionimageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000") { $isWaterImage = FALSE; $formatMsg = “暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。”; //读取水印文件 if(!empty($waterImage) &&file_exists($waterImage)) { $isWaterImage = TRUE; $water_info = getimagesize($waterImage); $water_w = $water_info[0];//取得水印图片的宽 $water_h = $water_info[1];//取得水印图片的高 switch($water_info[2])//取得水印图片的格式 { case 1:$water_im = imagecreatefromgif($waterImage);break; case 2:$water_im = imagecreatefromjpeg($waterImage);break; case 3:$water_im = imagecreatefrompng($waterImage);break; default:die($formatMsg); } } //读取背景图片 if(!empty($groundImage) &&file_exists($groundImage)) { $ground_info = getimagesize($groundImage); $ground_w = $ground_info[0];//取得背景图片的宽 $ground_h = $ground_info[1];//取得背景图片的高 switch($ground_info[2])//取得背景图片的格式 { case 1:$ground_im = imagecreatefromgif($groundImage);break; case 2:$ground_im = imagecreatefromjpeg($groundImage);break; case 3:$ground_im = imagecreatefrompng($groundImage);break; default:die($formatMsg); } } else { die("需要加水印的图片不存在!"); } //水印位置 if($isWaterImage)//图片水印 { $w = $water_w; $h = $water_h; $label = "图片的"; } else//文字水印 { $temp =imagettfbbox(ceil($textFont*5),0,"./cour.ttf",$waterText);//取得使用TrueType 字体的文本的范围 $w = $temp[2] - $temp[6]; $h = $temp[3] - $temp[7]; unset($temp); $label = "文字区域"; } if( ($ground_w<$w) || ($ground_h<$h)) { echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!"; return; } switch($waterPos) { case 0://随机 $posX = rand(0,($ground_w - $w)); $posY = rand(0,($ground_h - $h)); break; case 1://1为顶端居左 $posX = 0; $posY = 0; break; case 2://2为顶端居中 $posX = ($ground_w - $w) / 2; $posY = 0; break; case 3://3为顶端居右 $posX = $ground_w - $w; $posY = 0; break; case 4://4为中部居左 $posX = 0; $posY = ($ground_h - $h) / 2; break; case 5://5为中部居中 $posX = ($ground_w - $w) / 2; $posY = ($ground_h - $h) / 2; break; case 6://6为中部居右 $posX = $ground_w - $w; $posY = ($ground_h - $h) / 2; break; case 7://7为底端居左 $posX = 0; $posY = $ground_h - $h; break; case 8://8为底端居中 $posX = ($ground_w - $w) / 2; $posY = $ground_h - $h; break; case 9://9为底端居右 $posX = $ground_w - $w; $posY = $ground_h - $h; break; default://随机 $posX = rand(0,($ground_w - $w)); $posY = rand(0,($ground_h - $h)); break; } //设定图像的混色模式 imagealphablending($ground_im, true); if($isWaterImage)//图片水印 { imagecopy($ground_im, $water_im, $posX, $posY, 0, 0,$water_w,$water_h);//拷贝水印到目标文件 } else//文字水印 { if( !empty($textColor) &&(strlen($textColor)==7) ) { $R = hexdec(substr($textColor,1,2)); $G = hexdec(substr($textColor,3,2)); $B = hexdec(substr($textColor,5)); } else { die(”水印文字颜色格式不正确!”); } imagestring ( $ground_im, $textFont, $posX, $posY, $waterText,imagecolorallocate($ground_im, $R, $G, $B)); } //生成水印后的图片 //@unlink($groundImage); switch($ground_info[2])//取得背景图片的格式 { case 1:imagegif($ground_im,"watermark/".$groundImage);break; case 2:imagejpeg($ground_im,"watermark/".$groundImage);break; case 3:imagepng($ground_im,"watermark/".$groundImage);break; default:die($errorMsg); } //释放内存 if(isset($water_info)) unset($water_info); if(isset($water_im)) imagedestroy($water_im); unset($ground_info); imagedestroy($ground_im); } //文字水印 //$flg=imageWaterMark("./20100203/123_1.jpg",9,"","
www.123.com
",5,"#cccccc"); //echo $flg; //图片水印 $waterImage="20100203/123_1_tiny.jpg";//水印图片路径 imageWaterMark("20100203/123_3.jpg",5,$waterImage); 哦,不支持中文
满意请采纳
php如何实现水印平铺?
我爱编程网(https://www.52biancheng.com)小编还为大家带来php如何实现水印平铺?的相关内容。
//PHP水印(四个随机位置).php,你改一下就行的:
<?
function waterMark($fileInHD, $wmFile, $transparency = 50, $jpegQuality = 90, $margin = 5) {
$wmImg = imageCreateFromGIF($wmFile);
$jpegImg = imageCreateFromJPEG($fileInHD);
// Water mark random position
$wmX = (bool)rand(0,1) ? $margin : (imageSX($jpegImg) - imageSX($wmImg)) - $margin;
$wmY = (bool)rand(0,1) ? $margin : (imageSY($jpegImg) - imageSY($wmImg)) - $margin;
// Water mark process
imageCopyMerge($jpegImg, $wmImg, $wmX, $wmY, 0, 0, imageSX($wmImg), imageSY($wmImg), $transparency);
// Overwriting image
ImageJPEG($jpegImg, $fileInHD, $jpegQuality);
}
waterMark('bg.jpg','./images/bz.gif');
?>
<img src='bg.jpg'>
我爱编程网以上就是我爱编程网小编整理的内容,想要了解更多相关资讯内容敬请关注我爱编程网。更多相关文章关注我爱编程网:
www.52biancheng.com免责声明:文章内容来自网络,如有侵权请及时联系删除。