PHP给图片加水印的思想是什么?
发布网友
发布时间:2022-04-29 12:14
我来回答
共3个回答
热心网友
时间:2022-04-25 02:07
贴个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
参考资料:http://www.nayitian.net
热心网友
时间:2022-04-25 03:25
PHP给图片加水印:函数
// 上传附件函数
function acceptupload() {
global $DB, $FORM, $mysql_prefix, $attachment, $attachment_size, $attachment_name, $options, $allow_file_type;
@chdir("admin");//chdir -- 改变目录将 PHP 的当前目录改为admin
$attachment_name = strtolower($attachment_name);
$extension = getextension($attachment_name);
if (is_uploaded_file($attachment)) {//判断文件是否是通过 HTTP POST 上传的
if (in_array($extension, array('gif', 'jpg', 'jpeg', 'png'))) {//in_array()在数组中搜索指定的值
//echo $extension;
$path = "../uploadfiles/".$attachment_name;
} else {
echo "加水印";
//$path = "../uploadfiles/".$attachment_name;
}
if(!in_array($extension,$allow_file_type)) {
$FORM->ob_exit("文件格式不允许!");
}
@move_uploaded_file($attachment, $path);
@chmod ($path, 0666);
$attachment = $path;
[email=$filesize=@filesize($attachment]$filesize=@filesize($attachment[/email]);
if ($filesize != $attachment_size) {
@unlink($attachment);
$FROM->ob_exit("上传附件发生意外错误!");
}
}
Return true;
}
--------------------
如有疑问请留言~
热心网友
时间:2022-04-25 05:00
http://djh235.blog.163.com/blog/static/13976226200702194720244/