当前位置:首页 » 文件管理 » php上传图片水印

php上传图片水印

发布时间: 2025-03-07 18:36:48

A. php文字水印,水印会根据图片的大小自动变化

这个好像有点复杂哦
一般水印都是用GD库函数实现的,你可以把上传的图片长宽取出来,这应该不难吧,然后根据需要在你实现水印的函数里根长宽参数关联,应该就能实现。

B. PHP给图片加水印的思想是什么

贴个php的图片处理类 你可以自己研究下 共同进步 呵呵

/*
此类包含以下功能
A.生成缩略图
B.给图片添加文字水印(包括中文)
C.将指定的图片旋转90度/180度/270度,并保存至文件
D.将图片水平/垂直翻转,并保存至文件
E.在线裁剪图片任意部分矩形(正在编写之中)
具体目标效果暂时可以参考 http://yananzb.com/cut/cut.htm
期待您的响应

成员函数说明
---------------------------------------------------------------------------------------------
public void CImage::__construct(string $src_image_file)
功能:类CImage的构造函数
参数
$src_image_file 字符串,源图片文件名 注意,目前只支持gif,png,jpeg,jpg格式,这是GD库的限制,并非本程序的局限
---------------------------------------------------------------------------------------------
public bool CImage: thumb($image_dist,$x)
功能:根据源图片生成缩略图,并保存至文件
$image_dist 字符串 目标缩略图片路径及文件名 如 /File/th.jpg
$x 整型 目标缩略图片的尺寸限制即当原始图片的宽大于高时,那么新的缩略图的宽为$x,反之高为$x
---------------------------------------------------------------------------------------------
public bool image_press($image_dist,$str,$font="simkai.ttf")

函数功能:
图片生成水印并保存新图片至目标文件
参数说明:
$image_dist 字符串 目标图片名
$str 字符串 要写入到图片水印的字符串
$font 字符串 合法的系统字体名或WEB目录中正确的字体文件名
---------------------------------------------------------------------------------------------
public bool rotate($image_dist,$angle)
函数功能:
将源图片旋转一定角度并将新图片保存至文件
参数说明:
$image_dist 字符串 目标图片文件名
$angle 整数 要旋转的角度 只能是90或180或270度
---------------------------------------------------------------------------------------------
public bool rotate_h($image_dist)
函数功能:
将源图片水平翻转,并将新图片保存至文件
参数说明:
$image_dist 字符串 目标图片文件名
---------------------------------------------------------------------------------------------
public bool rotate_v($image_dist)
函数功能:
将源图片垂直翻转,并将新图片保存至文件
参数说明:
$image_dist 字符串 目标图片文件名
---------------------------------------------------------------------------------------------
使用范例:
$p=new CImage("s.jpg"); //创建一个图片处理对象
$p->thumb("thumb.jpg",300); //生成缩略图 限制尺寸为300,保存为thumb.jpg
$p->rotate("rt.jpg",90); //旋转90度,并保存为rt.jpg,类似地,你可以将90换成180,270进行旋转
$p->rotate_h("h.jpg"); //水平翻转
$p->rotate_v("v.jpg"); //垂直翻转

*/
class CImage{
var $src_image;
var $width;
var $height;
var $image_type;
var $img;
var $src_x;
var $src_y;

function __construct($image_file)
{
$info=GetImageSize($image_file);
$this->src_image=$image_file;
$this->width=$info[0];
$this->height=$info[1];

switch($info[2])
{
case 1:
$this->image_type="gif";
break;
case 2:
$this->image_type="jpeg";
break;
case 3:
$this->image_type="png";
break;
default:
return false;
//echo("Unsurport Image type.");
break;
} //swith end
//echo "ok";
$new_function='ImageCreateFrom'.ucfirst($this->image_type);
$this->img=$new_function($this->src_image);
$this->src_x=ImageSX($this->img);
$this->src_y=ImageSY($this->img);
}
function thumb($image_dist,$x) //$x为新图的限制边的尺寸
{
$src_x=ImageSX($this->img);
$src_y=ImageSY($this->img);
$scale=min($x/$src_x,$x/$src_y);

if($scale<1)
{
$new_x=floor($scale*$src_x);
$new_y=floor($scale*$src_y);
$img_tmp=ImageCreateTrueColor($new_x,$new_y); //set the size of Canvas for the new Image
ImageCopyResampled($img_tmp,$this->img,0,0,0,0,$new_x,$new_y,$src_x,$src_y); //Resampled
ImageDestroy($this->img);
$new_function="Image".ucfirst($this->image_type);

return $new_function($img_tmp,$image_dist);
}
} // thumb end

//给图片生成文字水印
function image_press($image_dist,$str,$font="simkai.ttf") {
$str=iconv("GB2312","utf-8",$str);
$blue=ImageColorAllocate($this->img,90,255,255);
$white=ImageColorAllocate($this->img,255,0,0);
ImageTTFText($this->img,20,0,$this->src_x/2/2,$this->src_y-80,$white,$font,$str);
$new_function="Image".ucfirst($this->image_type);
return $new_function($this->img,$image_dist);
}

function rotate($image_dist,$angle)
{
$img_tmp=null;
$new_function="Image".ucfirst($this->image_type);
if(($angle!=90)&&($angle!=180)&&($angle!=270))
{
echo("Un-valid angle on calling CImage::rotate(\$image_dist,\$angle) .<p>The valid angle must be 90 or 180 or 270.");
return false;
}

if(($angle==90)||($angle==270))
{
$img_tmp=ImageCreateTrueColor($this->src_y,$this->src_x);
}
else
{
$img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
}

switch($angle)
{
case 90:
for($i=0;$i<$this->src_x;$i++)
{
for($j=0;$j<$this->src_y;$j++)
{
ImageSetPixel($img_tmp,$this->src_y-$j-1,$i,ImageColorAt($this->img,$i,$j));
}
}
return $new_function($img_tmp,$image_dist);
break;

case 180:
for($i=0;$i<$this->src_x;$i++)
{
for($j=0;$j<$this->src_y;$j++)
{
ImageSetPixel($img_tmp,$this->src_x-$i-1,$this->src_y-$j-1,ImageColorAt($this->img,$i,$j));
}
}
return $new_function($img_tmp,$image_dist);
break;

case 270:
for($i=0;$i<$this->src_x;$i++)
{
for($j=0;$j<$this->src_y;$j++)
{
ImageSetPixel($img_tmp,$j,$this->src_x-$i-1,ImageColorAt($this->img,$i,$j));
}
}
return $new_function($img_tmp,$image_dist);
break;
} //end switch

} //end rotate

function rotate_h($image_dist)
{
$new_function="Image".ucfirst($this->image_type);
$img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
ImageCopyResampled($img_tmp,$this->img,0,0,$this->src_x-1,0,$this->src_x,$this->src_y,-$this->src_x,$this->src_y); //水平翻转
return $new_function($img_tmp,$image_dist);
}

function rotate_v($image_dist)
{
$new_function="Image".ucfirst($this->image_type);
$img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
ImageCopyResampled($img_tmp,$this->img,0,0,0,$this->src_y-1,$this->src_x,$this->src_y,$this->src_x,-$this->src_y);
return $new_function($img_tmp,$image_dist);
}
} //end CImage

C. php如何实现水印平铺

代码如下:
imagick代码:
<?php
$image = new imagick('logo.jpg');
$im = new imagick();
$im->newimage( 140, 80, new imagickpixel( "none" ) );
$draw = new imagickdraw();
$draw->setfillcolor(new imagickpixel( "grey" ));
$draw->setgravity(imagick::gravity_northwest);
$draw->annotation(10,10 ,'right');
$draw->setgravity(imagick::gravity_southeast);
$draw->annotation(5,15 ,'right');
$im->drawimage( $draw);
$image = $image->textureimage($im);
$image->compositeimage($image,imagick::composite_,0,0);
header( "content-type: image/{$image->getimageformat()}" );
$image->writeimage('wmark_text_tiled.jpg');
$image->clear();
$image->destroy();
?>

D. PHP如何根据图片色阶不同添加水印

在使用php编程的时候, 很多时候需要对上传的图片加水印,来确定图片版权和出处. 但是,一般情况下加水印的位置是图片的右下角, 但是,不同图片的色阶不同,有时候我们 图片的水印和图片本身色阶相同,就会造成水印不明显.
下面这段代码可以实现自动识别图片的色阶,更加色阶差来添加图片的水印,这样可以避免水印和图片色阶一样的弊端.
<?php
function add_wm($nmw_water, $src_file, $output_file, $x, $y) {
if(file_exists($output_file))
return;
$w1 = MagickGetImageWidth($nmw_water);
$h1 = MagickGetImageHeight($nmw_water);
$nmw =NewMagickWand();
MagickReadImage($nmw, $src_file);
// 默认的加水印位置调整
$lt_w = 50;
$lt_h = 50;
if($x == 0){
$w = MagickGetImageWidth($nmw);
$h = MagickGetImageHeight($nmw);
$x = $w;
$y = $h;
}else{
// 根据具体情况调整
$lt_w = 30;
$lt_h = 40;
}
MagickCompositeImage($nmw, $nmw_water, MW_OverCompositeOp, $x - $w1 - $lt_w, $y - $h1 - $lt_h);
MagickWriteImage($nmw, $output_file);
DestroyMagickWand($nmw);
}
// 还是groovy的eachFileRecurse好用啊
function add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr) {
$dp = dir($to_dir);
while($file=$dp->read()){
if($file != '.' && $file != '..'){
if(is_dir($to_dir . '/' . $file)){
mkdir($output_dir . '/' . $file);
add_wm_recurse($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $arr);
}else{
if(!array_key_exists($to_dir . '/' . $file, $arr)){
continue;
}
$sub_arr = $arr[$to_dir . '/' . $file];
if($sub_arr){
$x = intval($sub_arr[0]);
$y = intval($sub_arr[1]);
add_wm($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $x, $y);
}
}
}
}
$dp->close();
}
$to_dir = './resized';
$output_dir = './output';
// 这个是我用java的ImageIO遍历图片像素获取的符合裤子颜色的区域的坐标array(posX, posY)
$arr = array(
array(50, 50)
);
$water = './water.png';
$nmw_water =NewMagickWand();
MagickReadImage($nmw_water, $water);
add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr);
DestroyMagickWand($nmw_water);
?>

补充:
PHP图像处理模块 MagickWand用法
MagickWand 是一个PHP的模块,用来访问 ImageMagick 的图像处理库。下面是一个使用 MagicWand 的代码片段:
$magick_wand=NewMagickWand();
MagickReadImage($magick_wand,'rose.jpg');
$drawing_wand=NewDrawingWand();
DrawSetFont($drawing_wand,"/usr/share/fonts/bitstream-vera/Vera.ttf");
DrawSetFontSize($drawing_wand,20);
DrawSetGravity($drawing_wand,MW_CenterGravity);
$pixel_wand=NewPixelWand();
PixelSetColor($pixel_wand,"white");
DrawSetFillColor($drawing_wand,$pixel_wand);
if (MagickAnnotateImage($magick_wand,$drawing_wand,0,0,0,"Rose") != 0)
{
MagickEchoImageBlob( $magick_wand );
}
else
{
echo MagickGetExceptionString($magick_wand);
}
?>

安装方法:
1. 下载 php_magickwand_q16_st.dll for 5.2.x
2. 将其放在PHP的扩展目录
3. 在php.ini文件总增加 extension=php_magickwand_q16_st.dll
4. 重新启动apache

E. 谁有打水印代码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) //如果有水印图
{
//imagemerge 拷贝并合并图像的一部分
//参数(源图,水印图,拷贝到源图x位置,拷贝到源图y位置,从水印图x位置,从水印图y位置,高,宽,透明度)
imagemerge($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]));
}
}

F. php 怎么上传完图片之后,给这个图片打水印,并且保存在另一个文件夹

这个php中的图片处理类完全足够了,使用图片水印

$groundImg = "DSC05940.jpeg";
$groundInfo = getimagesize($groundImg);
$ground_w = $groundInfo[0];
//print_r($groundInfo);
$ground_h = $groundInfo[1];
switch($groundInfo[2]){
case 1:
$ground_im = imagecreatefromgif($groundImg);
break;
case 2:
$ground_im = imagecreatefromjpeg($groundImg);
break;
case 3:
$ground_im = imagecreatefrompng($groundImg);
break;
}

$waterImg = "DSC05949.jpeg";
$imgInfo =getimagesize($waterImg);
$water_w = $imgInfo[0];
$water_w = $imgInfo[1];

switch($imgInfo[2]){
case 1:
$water_im = imagecreatefromgif($waterImg);
break;
case 2:
$water_im = imagecreatefromjpeg($waterImg);
break;
case 3:
$water_im = imagecreatefrompng($waterImg);
break;
}
image($ground_im,$water_im,100,100,0,0,500,500);
header("Content-type: image/jpeg");

imagejpeg($ground_im);

这些都很麻烦,建议使用框架,很多框架都提供了图片处理类供使用

G. php如何实现自动加水印

加水印逻辑有两种

一种是上传直接加水印

另一种是利用伪静态将图片访问重定向到处理程序,临时加水印缓存并输出

php处理图片加水印可以使用gd库中的相关函数

以下为临时手打代码,可以按此思路优化,有问题可以联系本人

//此处需根据上传的图片格式使用对应函数实例化图片
$img=imagecreatefromjpg($imgurl);
//根据水印图片路径实例化水印
$waterImg=imagecreatefrompng($waterpath);
//获取原图及水印图片尺寸,用以计算是否需要缩放及放置位置
list($width,$height,$type,$attr)=getimagesize($imgurl);
list($waterw,$waterh,$type,$attr)=getimagesize($waterpath);
$scale=1;
$waterReleaseW=$waterw;
$waterReleaseH=$waterh;
if($waterReleaseW>$width*.5){
$scale=$width*.5/$waterw;
$waterReleaseW=$width*.5;
$waterReleaseH=$waterh*$scale;
}
if($waterReleaseH>$height*.5){
$scale*=$height*.5/$waterh;
$waterReleaseH=$height*.5;
$waterReleaseW=$waterw*$scale;
}

//将水印图片拷贝到原图指定位置(此示例为右下角)
imageresized($img,$waterImg,
$width-$waterReleaseW-10,$height-$waterReleaseH-10,
0,0,
$width-10,$height-10,
$waterw,$waterh);
//销毁水印图片实例
imagedestroy($waterImg);

//水印后图片保存
imagejpeg($img,$newpath);

H. 关于PHP给图片添加水印的问题,求大神解答

  1. 加两文字水印,弄成加两次水印就行,不过效率就差点。可以考虑加图片的水印

  2. 你用什么编辑器?如果是记事本的话很容易出现乱码问题,网页是UTF-8了,但是你记事本存储的却是GBK。推荐用Editlus 3,网上有注册版的。

I. PHP图片生成

给你一个php 图像处理类,完全能实现你的功能,你自己研究一下吧

<?php
class image
{
var $w_pct = 50; //透明度
var $w_quality = 80; //质量
var $w_minwidth = 300; //最小宽
var $w_minheight = 300; //最小高
var $thumb_enable; //是否生成缩略图
var $watermark_enable; //是否生水印
var $interlace = 0; //图像是否为隔行扫描的
var $fontfile; //字体文件
var $w_img ; //默认水印图

function __construct()
{
global $SITE_CONFING;
$this->thumb_enable = $SITE_CONFING['thumb_enable'];
$this->watermark_enable = $SITE_CONFING['watermark_enable'];
$this->set($SITE_CONFING['watermark_minwidth'], $SITE_CONFING['watermark_minheight'], $SITE_CONFING['watermark_quality'], $SITE_CONFING['watermark_pct'], $SITE_CONFING['watermark_fontfile'],$SITE_CONFING['watermark_img']);
}

function image()
{
$this->__construct();
}

function set($w_minwidth = 300, $w_minheight = 300, $w_quality = 80, $w_pct = 100,$fontfile,$w_img)
{
$this->w_minwidth = $w_minwidth;
$this->w_minheight = $w_minheight;
$this->w_quality = $w_quality;
$this->w_pct = $w_pct;
$this->fontfile = $fontfile;
$this->w_img = $w_img;
}

function info($img)
{
$imageinfo = getimagesize($img); //返回图像信息数组 0=>宽的像素 1=>高的像素 2=>是图像类型的标记 3 =>是文本字符串,内容为“height="yyy" width="xxx"”,
if($imageinfo === false) return false;
$imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //获取图像文件类型 $imageinfo[2]是图像类型的标记
$imagesize = filesize($img); //图像大小
$info = array(
'width'=>$imageinfo[0],
'height'=>$imageinfo[1],
'type'=>$imagetype,
'size'=>$imagesize,
'mime'=>$imageinfo['mime']
);
return $info;
}

function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 50, $suffix='_thumb', $autocut = 0)
{
if(!$this->thumb_enable || !$this->check($image)) return false;
$info = $this->info($image); //获取图片信息
if($info === false) return false;
$srcwidth = $info['width']; //源图宽
$srcheight = $info['height']; //源图高
$pathinfo = pathinfo($image);
$type = $pathinfo['extension']; //取得扩展名
if(!$type) $type = $info['type']; //如果没有取到,用$info['type']
$type = strtolower($type);
unset($info);
$scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); //获取缩略比例
//获取按照源图的比列
$createwidth = $width = (int)($srcwidth*$scale); //取得缩略宽
$createheight = $height = (int)($srcheight*$scale); //取得缩略高
$psrc_x = $psrc_y = 0;
if($autocut) //按照缩略图的比例来获取
{
if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) //如果缩略图按比列比源图窄的话
{
$width = $maxheight/$height*$width; //宽按照相应比例做处理
$height = $maxheight; //高不变
}
elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)//如果缩略图按比列比源图宽的话
{
$height = $maxwidth/$width*$height;
$width = $maxwidth;
}
$createwidth = $maxwidth;
$createheight = $maxheight;
}
$createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的图像处理函数
$srcimg = $createfun($image); //新建图像
if($type != 'gif' && function_exists('imagecreatetruecolor'))
$thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一个真彩色图像
else
$thumbimg = imagecreate($width, $height); //新建一个基于调色板的图像

if(function_exists('imageresampled')) //重采样拷贝部分图像并调整大小,真彩
//imageresampled(新图,源图,新图左上角x距离,新图左上角y距离,源图左上角x距离,源图左上角y距离,新图宽,新图高,源图宽,源图高)
imageresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
else //拷贝部分图像并调整大小,调色板
imageresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
if($type=='gif' || $type=='png')
{
//imagecolorallocate 为一幅图像分配颜色
$background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 给基于调色板的图像填充背景色, 指派一个绿色
// imagecolortransparent 将某个颜色定义为透明色
imagecolortransparent($thumbimg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
}
// imageinterlace 激活或禁止隔行扫描
if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
$imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
//imagejpeg imagegif imagepng
if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //获取文件名
//aaa.gif aaa_thumb.gif
$imagefun($thumbimg, $filename); //新建图像
imagedestroy($thumbimg); //销毁缩略图
imagedestroy($srcimg); //销毁源图
return $filename;
}
//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) //如果有水印图
{
//imagemerge 拷贝并合并图像的一部分
//参数(源图,水印图,拷贝到源图x位置,拷贝到源图y位置,从水印图x位置,从水印图y位置,高,宽,透明度)
imagemerge($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]));
//imagecreatefromjpeg
//imagecreatefromgif
//imagecreatefrompng
}
}

/**
缩略图
1.新建一个图像资源 通过 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
2.imageresampled 拷贝图像,并调整大小

水印:图片水印,文字水印
1. 创建图像
2.加水印
图片水印:imagemerge 把2张图合并在一起
文字水印:imagettftext 向图像写入文字

*/
?>

热点内容
scratch少儿编程课程 发布:2025-04-16 17:11:44 浏览:639
荣耀x10从哪里设置密码 发布:2025-04-16 17:11:43 浏览:368
java从入门到精通视频 发布:2025-04-16 17:11:43 浏览:84
php微信接口教程 发布:2025-04-16 17:07:30 浏览:310
android实现阴影 发布:2025-04-16 16:50:08 浏览:793
粉笔直播课缓存 发布:2025-04-16 16:31:21 浏览:344
机顶盒都有什么配置 发布:2025-04-16 16:24:37 浏览:213
编写手游反编译都需要学习什么 发布:2025-04-16 16:19:36 浏览:812
proteus编译文件位置 发布:2025-04-16 16:18:44 浏览:366
土压缩的本质 发布:2025-04-16 16:13:21 浏览:592