上传图片添加水印???
上传图片添加水印???
搞了个上传图片的函数(经过测试可以上传图片,这个没有问题)
现在想上传图片时添加水印图片(一个小gif)
上传图片时,网页提示函数imagecopymerge()报错,不知道哪里出了问题,大家帮我看看
上传图片函数代码:
/*$files为上传的图片文件$folder为上传图片的文件夹*/function uploads($files,$folder){ //取得图片的后缀名 $knamearray=explode(".",$_FILES["upfile"]["name"]); $kname=$knamearray[count($knamearray)-1]; //设置图片文件名为系统时间 $filename=date('YmdHis'); $filenames=$filename.$code.".".$kname; $upfile=$folder."/".$filenames; //上传图片 move_uploaded_file($files,$upfile); //添加水印图片开始了…… $im=imagecreatefromgif("wimages/logo.gif"); //logo.gif图片的尺寸为120*45 imagecopymerge($upfile,$im,0,0,0,0,120,45,80); //现在提示这句有问题。。。 return $filenames;//回传图片文件名}
class upimg{ //该类用来实现上传$uptypes数组内类型的图片,可以通过setWaterMark()启用设置水印
/**
*
* 则在图片上加水印;
* 外部通过访问$msg ,$img_width, $img_height ,$save_dir,$img_size变量
* 获取图片在上传过程中出现的错误,和图片上传后保存的路径,图片大小等信息;
*/
private $uptypes = array('image/gif','image/jpg','image/jpeg','image/png','image/pjpeg');//'image/bmp','image/x-png');
private $max_file_size = 200000;
private $dir_folder = "../imageupload/";
private $water_mark = 1;
private $water_type = 1;
private $water_str = "http://www.516ya.cn";
private $water_img = "xplore.gif";
private $create_water= false;
private $overwrite = false;
public $msg="";
public $img_width=0;
public $img_height=0;
public $save_dir="";
public $img_size=0;
public $sucess =false;
function setWaterMark(){
$this->create_water = true;
}
function startUpload($files){
if(!is_uploaded_file($files['tmp_name']))
{
$this->msg.="图片不存在!--";
return $this->sucess=false;
}
if($files['error']!=UPLOAD_ERR_OK){
$this->msg.="图片上传过程中出现错误";
return $this->sucess=false;
}
$file = $files;
if($this->max_file_size <$file['size'])
{
$this->msg.="你选择的文件大小超过限制--";
return $this->sucess=false;
}
if(!file_exists($this->dir_folder))
{
mkdir($this->dir_folder);
}
$file_name = $file['tmp_name'];
$image_size = getimagesize($file_name);
$path_info = pathinfo($file['name']);
$ftype = $path_info['extension'];
$destination = $this->dir_folder."img_".(double)microtime() * 1000003 * time().".".$ftype;
if(!in_array($file['type'],$this->uptypes)){
$this->msg.="文件类型不正确--";
return $this->sucess=false;
}
if(file_exists($destination)&& $this->overwrite!=true)
{
$this->msg.= "有一个同名文件存在--";
return $this->sucess=false;
}
if(!move_uploaded_file($file_name,$destination))
{
$this->msg.= "图片从零时文件夹保存到指定文件夹时出错--";
return $this->sucess=false;
}
$pinfo = pathinfo($destination);
$fname = $pinfo[basename];
$this->save_dir.= $this->dir_folder.$fname."|";
$this->img_width =$image_size[0];
$this->img_height =$image_size[1];
$this->img_size= $file['size']."字节";
$this->msg.="图片上传成功--";
$this->sucess = true;
if($this->create_water ==true)
{
if($this->water_mark==1)
{
$iinfo=getimagesize($destination);
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
$white=imagecolorallocate($nimage,255,255,255);
$black=imagecolorallocate($nimage,0,0,0);
$red=imagecolorallocate($nimage,255,0,0);
imagefill($nimage,0,0,$white);
switch($iinfo[2])//根据获取图片的类型,判断采用何种操作
{
case 1:
$simage=imagecreatefromgif($destination);
break;
case 2:
$simage=imagecreatefromjpeg($destination);
break;
case 3:
$simage=imagecreatefrompng($destination);
break;
case 6:
$simage=imagecreatefromwbmp($destination);
break;
default:
die("不支持的文件类型");
exit;
}
imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
imagefilledrectangle($nimage,$image_size[0]-124,$image_size[1]-17,$image_size[0]-2,$image_size[1]-2,$white);
switch($this->water_type)
{
case 1: //加水印字符串
imagestring($nimage,2,$image_size[0]-121,$image_size[1]-17,$this->water_str,$black);
break;
case 2: //加水印图片
$simage1=imagecreatefromgif("xplore.gif");
imagecopy($nimage,$simage1,0,0,0,0,85,15);
imagedestroy($simage1);
break;
}
switch($iinfo[2])
{
case 1:
imagejpeg($nimage,$destination);
break;
case 2:
imagejpeg($nimage,$destination);
break;
case 3:
imagepng($nimage,$destination);
break;
case 6:
imagewbmp($nimage,$destination);
break;
default:
die("不支持的文件类型");
exit;
}
//覆盖原上传文件
imagedestroy($nimage);
imagedestroy($simage);
}
$this->success =true;
}
}
}
function uploads($files,$folder){ //取得图片的后缀名 $knamearray=explode(".",$_FILES["upfile"]["name"]); $kname=$knamearray[count($knamearray)-1]; //设置图片文件名为系统时间 $filename=date('YmdHis'); $filenames=$filename.$code.".".$kname; $upfile=$folder."/".$filenames; //上传图片 move_uploaded_file($files,$upfile); //增加部分 start $type = image_type_to_extension($upfile); if($type == "jpg") $type = "jpeg"; $image_func = "imagecreatefrom".$type; $image = $image_func($upfile); //增加部分 end //添加水印图片开始了…… $im=imagecreatefromgif("wimages/logo.gif"); //logo.gif图片的尺寸为120*45 imagecopymerge($image,$im,0,0,0,0,120,45,80); //现在提示这句有问题。。。 //增加部分 start imagedestroy($im); header("Content-Type: image/".$type); $image_new = "image".$type; $image_new($image, $upfile); imagedestroy($image); //增加部分 end return $filenames;//回传图片文件名}
[解决办法]
不好意思上面写错了:
上面的$type = image_type_to_extension($upfile);这一行应改为:
$size = getimagesize($upfile);
$type = substr(image_type_to_extension($size[2]),1);