1、批量上传
2、生成缩略图
3、增加水印
4、缩略图上增加链接到原图
第一步、首先建立如下目录结构
1,/upfiles/image
2,/image/
第二步、建立如下文件
1、/upfiles/upload.php(用于图片的上传)
2、/image/watermark.png(水印图)
第三步、upload.php文件内容
<?php
/*!
* upload demo for php
* @requires xhEditor
*
* @author Yanis.Wang<yanis.wang@gmail.com>
* @site http://xheditor.com/
* @licence LGPL(http://www.opensource.org/licenses/lgpl-license.php)
*
* @Version: 0.9.4 (build 100520)
*
* 注1:本程序仅为演示用,请您务必根据自己需求进行相应修改,或者重开发
* 注2:本程序特别针对HTML5上传,在程序开头加入了特殊处理
*/
header('Content-Type: text/html; charset=UTF-8');
$inputname='filedata';//表单文件域name
$attachdir='image';//上传文件保存路径,结尾不要带/
$save_path = 'image';//此路径相对于upload.php
//文件保存目录URL
$save_url = "/upfiles/image/"; //上传文件保存路径
if( !file_exists($save_path) )
mkdir( $save_path, 0777 );
//$attachdir = $save_path;
$dirtype=2;//1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
$maxattachsize=20971520;//最大上传大小,默认是2M
$upext='txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid';//上传扩展名
$msgtype=1;//返回上传参数的格式:1,只返回url,2,返回参数数组
$immediate=isset($_GET['immediate'])?$_GET['immediate']:0;//立即上传模式,仅为演示用
ini_set('date.timezone','Asia/Shanghai');//时区
if(isset($_SERVER['HTTP_CONTENT_DISPOSITION']))//HTML5上传
{
if(preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',$_SERVER['HTTP_CONTENT_DISPOSITION'],$info))
{
$temp_name=ini_get("upload_tmp_dir").'\\'.date("YmdHis").mt_rand(1000,9999).'.tmp';
file_put_contents($temp_name,file_get_contents("php://input"));
$size=filesize($temp_name);
$_FILES[$info[1]]=array('name'=>$info[2],'tmp_name'=>$temp_name,'size'=>$size,'type'=>'','error'=>0);
}
}
$err = "";
$msg = "''";
$upfile=@$_FILES[$inputname];
if(!isset($upfile))$err='文件域的name错误';
elseif(!empty($upfile['error']))
{
switch($upfile['error'])
{
case '1':
$err = '文件大小超过了php.ini定义的upload_max_filesize值';
break;
case '2':
$err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
break;
case '3':
$err = '文件上传不完全';
break;
case '4':
$err = '无文件上传';
break;
case '6':
$err = '缺少临时文件夹';
break;
case '7':
$err = '写文件失败';
break;
case '8':
$err = '上传被其它扩展中断';
break;
case '999':
default:
$err = '无有效错误代码';
}
}
elseif(empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$err = '无文件上传';
else
{
$temppath=$upfile['tmp_name'];
$fileinfo=pathinfo($upfile['name']);
$extension=$fileinfo['extension'];
if(preg_match('/'.str_replace(',','|',$upext).'/i',$extension))
{
$bytes=filesize($temppath);
if($bytes > $maxattachsize)$err='请不要上传大小超过'.formatBytes($maxattachsize).'的文件';
else
{
switch($dirtype)
{
case 1: $attach_subdir = 'day_'.date('ymd'); break;
case 2: $attach_subdir = date('Y-m'); break;
case 3: $attach_subdir = 'ext_'.$extension; break;
}
$attach_dir = $attachdir.'/'.$attach_subdir;
if(!is_dir($attach_dir))
{
@mkdir($attach_dir, 0777);
@fclose(fopen($attach_dir.'/index.htm', 'w'));
}
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
$filename=date("YmdHis").mt_rand(1000,9999).'.'.$extension;
$target = $attach_dir.'/'.$filename;
rename($upfile['tmp_name'],$target);
@chmod($target,0755);
$target = $save_url.$attach_subdir.'/'.$filename;
//缩略图
$resizesrcname = $attachdir.'/'.$attach_subdir.'/'.$filename;
$resizesrcname = resizeImg("$resizesrcname",460,600);
$resizesrcname = jsonString($resizesrcname);
//文字水印
$waterimg = $attachdir.'/'.$attach_subdir.'/'.$filename;
imageWaterMark($waterimg,9,"../image/watermark.png","<A href="'http://WWW.7878478.COM" ,5,?#FF0000??>HTTP://WWW.7878478.COM",5,"#FF0000");
$target=jsonString($target);
//if($immediate=='1')$target='!'.$target;
if($msgtype==1)$msg="'$target'";
else $msg="{'url':'".$target."','localname':'".jsonString($upfile['name'])."','id':'1'}";//id参数固定不变,仅供演示,实际项目中可以是数据库ID
}
}
else $err='上传文件扩展名必需为:'.$upext;
@unlink($temppath);
}
$resizesrcname = '/upfiles/'.$resizesrcname;
echo "{'err':'".jsonString($err)."','msg':'!$resizesrcname||$target'}";
function jsonString($str)
{
return preg_replace("/([\\\\\/'])/",'\\\$1',$str);
}
function formatBytes($bytes) {
if($bytes >= 1073741824) {
$bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
} elseif($bytes >= 1048576) {
$bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
} elseif($bytes >= 1024) {
$bytes = round($bytes / 1024 * 100) / 100 . 'KB';
} else {
$bytes = $bytes . 'Bytes';
}
return $bytes;
}
function resizeImg($srcName,$newWidth,$newHeight,$newName="")
{
if($newName=="")
{
$nameArr=explode('.',$srcName);
$expName=array_pop($nameArr);
$expName='thumb.'.$expName;
array_push($nameArr,$expName);
$newName = implode('_',$nameArr);
}
$data = getimagesize($srcName);
switch ($data[2])
{
case 1:
if(!function_exists("imagecreatefromgif")){
echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回";
exit();
}
$im = ImageCreateFromGIF($srcName);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回";
exit();
}
$im = ImageCreateFromJpeg($srcName);
break;
case 3:
$im = ImageCreateFromPNG($srcName);
break;
}
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$newWidthH=$newWidth/$newHeight;
$srcWH=$srcW/$srcH;
if($newWidthH<=$srcWH){
$ftoW=$newWidth;
$ftoH=$ftoW*($srcH/$srcW);
}
else{
$ftoH=$newHeight;
$ftoW=$ftoH*($srcW/$srcH);
}
if($srcW>$newWidth||$srcH>$newHeight)
{
if(function_exists("imagecreatetruecolor"))
{
@$ni = ImageCreateTrueColor($ftoW,$ftoH);
if($ni) ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
else
{
$ni=ImageCreate($ftoW,$ftoH);
ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
}
}
else
{
$ni=ImageCreate($ftoW,$ftoH);
ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
}
if(function_exists('imagejpeg')) {
ImageJpeg($ni,$newName);
//echo $newName;
//return $newName;
}
else ImagePNG($ni,$newName);
ImageDestroy($ni);
}
ImageDestroy($im);
return $newName;
}
//水印函数开始============
/** $groundImage 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
* $waterPos 水印位置,有10种状态,0为随机位置;
* 1为顶端居左,2为顶端居中,3为顶端居右;
* 4为中部居左,5为中部居中,6为中部居右;
* 7为底端居左,8为底端居中,9为底端居右;
* $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
* $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
* $textFont 文字大小,值为1、2、3、4或5,默认为5;
* $textColor 文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
* */
function imageWaterMark($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*2.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,$groundImage);break;
case 2:imagejpeg($ground_im,$groundImage);break;
case 3:imagepng($ground_im,$groundImage);break;
default:die($errorMsg);
}
return $groundImage;
//释放内存
if(isset($water_info)) unset($water_info);
if(isset($water_im)) imagedestroy($water_im);
unset($ground_info);
imagedestroy($ground_im);
}
//水印函数完
?>
- 本文标题: xheditor 图片PHP上传实现链接、缩略图、水印功能
- 文章分类:【HTML/CSS】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.