<?php
/**
* 上传附件和上传视频upload_action.php
* User: Jinqn
* Date: 14-04-09
* Time: 上午10:17
*/
include "Uploader.class.php";
/* 上传配置 */
$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
case 'uploadimage':
$config = array(
"pathFormat" => $CONFIG['imagePathFormat'],
"maxSize" => $CONFIG['imageMaxSize'],
"allowFiles" => $CONFIG['imageAllowFiles']
);
$fieldName = $CONFIG['imageFieldName'];
break;
case 'uploadscrawl':
$config = array(
"pathFormat" => $CONFIG['scrawlPathFormat'],
"maxSize" => $CONFIG['scrawlMaxSize'],
"allowFiles" => $CONFIG['scrawlAllowFiles'],
"oriName" => "scrawl.png"
);
$fieldName = $CONFIG['scrawlFieldName'];
$base64 = "base64";
break;
case 'uploadvideo':
$config = array(
"pathFormat" => $CONFIG['videoPathFormat'],
"maxSize" => $CONFIG['videoMaxSize'],
"allowFiles" => $CONFIG['videoAllowFiles']
);
$fieldName = $CONFIG['videoFieldName'];
break;
case 'uploadfile':
default:
$config = array(
"pathFormat" => $CONFIG['filePathFormat'],
"maxSize" => $CONFIG['fileMaxSize'],
"allowFiles" => $CONFIG['fileAllowFiles']
);
$fieldName = $CONFIG['fileFieldName'];
break;
}
/* 生成上传实例对象并完成上传 */
$up = new Uploader($fieldName, $config, $base64);
/**
* 得到上传文件所对应的各个参数,数组结构
* array(
* "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
* "url" => "", //返回的地址
* "title" => "", //新文件名
* "original" => "", //原始文件名
* "type" => "" //文件类型
* "size" => "", //文件大小
* )
*/
/* 返回数据 */
// var_dump($up);
$result = $up->getFileInfo();
// 水印处理开始-S
include "./water.php";
// 原图路径
$dstname = $result['url'];
// var_dump($dstname);
//先算出比例值,暂时按180/904比例
$pre = 180/904;
$water_pre = 90/180;
$dstname_info = getimagesize("../../../../..".$dstname);
// var_dump($dstname_info);
$width = $dstname_info[0] * $pre;
$height = $width*$water_pre;
// var_dump($dstname_info);
// echo $pre;
// 把图片按比例缩放
setpicSize('./logo2.png','./logo22.png',$width,$height);
// 如果是服务器上,请显示setWater("../../../..".$dstname,"./logo.png",9,"img");也就是向上返回4层
setWater("../../../../..".$dstname,"./logo22.png",9,"img");
// 水印处理结束-E
return json_encode($result);
//水印处理类,等相关water.php
<?php
function setWater($imgSrc,$markImg,$markPos,$markType)
{
// $srcInfo = @getimagesize($imgSrc);
$srcInfo = @getimagesize($imgSrc);
// var_dump($srcInfo);
$markInfo = @getimagesize($markImg);
// var_dump($markInfo);
// var_dump($srcInfo);die;
$srcImg_w = $srcInfo[0];
$srcImg_h = $srcInfo[1];
switch ($srcInfo[2])
{
case 1:
$srcim =imagecreatefromgif($imgSrc);
break;
case 2:
$srcim =imagecreatefromjpeg($imgSrc);
break;
case 3:
$srcim =imagecreatefrompng($imgSrc);
break;
default:
die("不支持的图片文件类型");
exit;
}
if(!strcmp($markType,"img"))
{
if(!file_exists($markImg) || empty($markImg))
{
return;
}
$markImgInfo = @getimagesize($markImg);
$markImg_w = $markImgInfo[0];
$markImg_h = $markImgInfo[1];
if($srcImg_w < $markImg_w || $srcImg_h < $markImg_h)
{
return;
}
switch ($markImgInfo[2])
{
case 1:
$markim =imagecreatefromgif($markImg);
break;
case 2:
$markim =imagecreatefromjpeg($markImg);
break;
case 3:
$markim =imagecreatefrompng($markImg);
break;
default:
die("不支持的水印图片文件类型");
exit;
}
$logow = $markImg_w;
$logoh = $markImg_h;
}
if($markPos == 0)
{
$markPos = rand(1, 9);
}
switch($markPos)
{
case 1:
$x = +5;
$y = +5;
break;
case 2:
$x = ($srcImg_w - $logow) / 2;
$y = +5;
break;
case 3:
$x = $srcImg_w - $logow - 5;
$y = +15;
break;
case 4:
$x = +5;
$y = ($srcImg_h - $logoh) / 2;
break;
case 5:
$x = ($srcImg_w - $logow) / 2;
$y = ($srcImg_h - $logoh) / 2;
break;
case 6:
$x = $srcImg_w - $logow - 5;
$y = ($srcImg_h - $logoh) / 2;
break;
case 7:
$x = +5;
$y = $srcImg_h - $logoh - 5;
break;
case 8:
$x = ($srcImg_w - $logow) / 2;
$y = $srcImg_h - $logoh - 5;
break;
// 右下
case 9:
$x = $srcImg_w - $logow - 5;
$y = $srcImg_h - $logoh -10;
break;
default:
die("此位置不支持");
exit;
}
$dst_img = @imagecreatetruecolor($srcImg_w, $srcImg_h);
imagecopy ( $dst_img, $srcim, 0, 0, 0, 0, $srcImg_w, $srcImg_h);
if(!strcmp($markType,"img"))
{
imagecopy($dst_img, $markim, $x, $y, 0, 0, $logow, $logoh);
imagedestroy($markim);
}
// if(!strcmp($markType,"text"))
//{
// $rgb = explode(',', $TextColor);
// $color = imagecolorallocate($dst_img, $rgb[0], $rgb[1], $rgb[2]);
// imagettftext($dst_img, $fontSize, 0, $x, $y, $color, $fontType,$markText);
//}
switch ($srcInfo[2])
{
case 1:
imagegif($dst_img, $imgSrc);
break;
case 2:
imagejpeg($dst_img, $imgSrc);
break;
case 3:
// $dst_img=imagecolorallocate($dst_img);
imagepng($dst_img, $imgSrc);
break;
default:
die("不支持的水印图片文件类型");
exit;
}
imagedestroy($dst_img);
imagedestroy($srcim);
}
/**
* *
*等比缩放
* @param unknown_type $srcImage 源图片路径
* @param unknown_type $toFile 目标图片路径
* @param unknown_type $maxWidth 最大宽
* @param unknown_type $maxHeight 最大高
* @param unknown_type $imgQuality 图片质量
* @return unknown
*/
function setpicSize($src,$toname,$newWidth=180,$newHeight=90){
//获取源图gd图像标识符
$srcImg = imagecreatefrompng($src);
$srcWidth = imagesx($srcImg);
$srcHeight = imagesy($srcImg);
//创建新图
$newImg = imagecreatetruecolor($newWidth, $newHeight);
//分配颜色 + alpha,将颜色填充到新图上
$alpha = imagecolorallocatealpha($newImg, 0, 0, 0, 127);
imagefill($newImg, 0, 0, $alpha);
//将源图拷贝到新图上,并设置在保存 PNG 图像时保存完整的 alpha 通道信息
imagecopyresampled($newImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $srcWidth, $srcHeight);
imagesavealpha($newImg, true);
imagepng($newImg, $toname);
}
/**
*
*函数:调整图片尺寸或生成缩略图
*修改:2013-2-15
*返回:True/False
*参数:
* $Image 需要调整的图片(含路径)
* $Dw=450 调整时最大宽度;缩略图时的绝对宽度
* $Dh=450 调整时最大高度;缩略图时的绝对高度
* $Type=1 1,调整尺寸; 2,生成缩略图
*/
$phtypes=array('img/gif', 'img/jpg', 'img/jpeg', 'img/bmp', 'img/pjpeg', 'img/x-png');
function compressImg($Image,$Dw,$Dh,$Type){
// echo $Image;
IF(!file_exists($Image)){
echo "不存在图片";
return false;
}
// echo "存在图片";
// 如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值(生成缩略图操作)
// 当Type==1的时候,将不拷贝原图像文件,而是在原来的图像文件上重新生成缩小后的图像(调整尺寸操作)
if($Type!=1){
copy($Image,str_replace(".","_x.",$Image));
$Image=str_replace(".","_x.",$Image);
}
// 取得文件的类型,根据不同的类型建立不同的对象
$ImgInfo=getimagesize($Image);
Switch($ImgInfo[2]){
case 1:
$Img =@imagecreatefromgif($Image);
break;
case 2:
$Img =@imagecreatefromjpeg($Image);
Break;
case 3:
$Img =@imagecreatefrompng($Image);
break;
}
// 如果对象没有创建成功,则说明非图片文件
if(Empty($Img)){
// 如果是生成缩略图的时候出错,则需要删掉已经复制的文件
IF($Type!=1){
unlink($Image);
}
return false;
}
// 如果是执行调整尺寸操作则
if($Type==1){
$w=ImagesX($Img);
$h=ImagesY($Img);
$width = $w;
$height = $h;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
IF($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
}
} elseif($height>$Dh) {
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
}
} else {
$width=$width;
$height=$height;
}
$nImg =ImageCreateTrueColor($width,$height);// 新建一个真彩色画布
ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);// 重采样拷贝部分图像并调整大小
ImageJpeg($nImg,$Image);// 以JPEG格式将图像输出到浏览器或文件
return true;
} else {// 如果是执行生成缩略图操作则
$w=ImagesX($Img);
$h=ImagesY($Img);
$width = $w;
$height = $h;
$nImg =ImageCreateTrueColor($Dw,$Dh);
IF($h/$w>$Dh/$Dw){// 高比较大
$width=$Dw;
$height=$h*$Dw/$w;
$IntNH=$height-$Dh;
ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
} else {// 宽比较大
$height=$Dh;
$width=$w*$Dh/$h;
$IntNW=$width-$Dw;
ImageCopyReSampled($nImg, $Img,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
}
ImageJpeg($nImg,$Image);
return true;
}
}
//生成缩略图
//makeThumbnail(源图片,缩略图,缩略图宽,缩略图高)
//makeThumbnail("images/222.jpg","images/2221.jpg",100,100);
function makeThumbnail($srcImgPath,$targetImgPath,$targetW,$targetH)
{
//echo $targetImgPath;
$imgSize = GetImageSize($srcImgPath);
$imgType = $imgSize[2];
//@ 使函数不向页面输出错误信息
switch ($imgType)
{
case 1:
$srcImg = @ImageCreateFromGIF($srcImgPath);
break;
case 2:
$srcImg = @ImageCreateFromJpeg($srcImgPath);
break;
case 3:
$srcImg = @ImageCreateFromPNG($srcImgPath);
break;
}
//取源图象的宽高
$srcW = ImageSX($srcImg);
$srcH = ImageSY($srcImg);
if($srcW>$targetW || $srcH>$targetH)
{
$targetX = 0;
$targetY = 0;
if ($srcW > $srcH)
{
$finaW=$targetW;
$finalH=round($srcH*$finaW/$srcW);
$targetY=floor(($targetH-$finalH)/2);
}
else
{
$finalH=$targetH;
$finaW=round($srcW*$finalH/$srcH);
$targetX=floor(($targetW-$finaW)/2);
}
//function_exists 检查函数是否已定义
//ImageCreateTrueColor 本函数需要GD2.0.1或更高版本
if(function_exists("ImageCreateTrueColor"))
{
$targetImg=ImageCreateTrueColor($targetW,$targetH);
}
else
{
$targetImg=ImageCreate($targetW,$targetH);
}
$targetX=($targetX<0)?0:$targetX;
$targetY=($targetX<0)?0:$targetY;
$targetX=($targetX>($targetW/2))?floor($targetW/2):$targetX;
$targetY=($targetY>($targetH/2))?floor($targetH/2):$targetY;
//背景白色
$white = ImageColorAllocate($targetImg, 255,255,255);
ImageFilledRectangle($targetImg,0,0,$targetW,$targetH,$white);
/*
PHP的GD扩展提供了两个函数来缩放图象:
ImageCopyResized 在所有GD版本中有效,其缩放图象的算法比较粗糙,可能会导致图象边缘的锯齿。
ImageCopyResampled 需要GD2.0.1或更高版本,其像素插值算法得到的图象边缘比较平滑,
该函数的速度比ImageCopyResized慢。
*/
if(function_exists("ImageCopyResampled"))
{
ImageCopyResampled($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
}
else
{
ImageCopyResized($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
}
switch ($imgType) {
case 1:
ImageGIF($targetImg,$targetImgPath);
break;
case 2:
ImageJpeg($targetImg,$targetImgPath);
break;
case 3:
ImagePNG($targetImg,$targetImgPath);
break;
}
ImageDestroy($srcImg);
ImageDestroy($targetImg);
}
else //不超出指定宽高则直接复制
{
copy($srcImgPath,$targetImgPath);
ImageDestroy($srcImg);
}
}
function smallpic($srcImgPath)
{
$img_arr=explode(".",$srcImgPath);
$targetImgPath="..".$img_arr[2]."sm.".$img_arr[3];
return $targetImgPath;
}
转载于:https://my.oschina.net/bibo/blog/330174