Upimages.class.php php上传类

复制代码代码如下:

class UpImages {

var $annexFolder = "upload";//附件存放点,默认为:annex

var $smallFolder = "small";//缩略图存放路径,注:必须是放在 $annexFolder下的子目录,默认为:smallimg

var $markFolder = "mark";//水印图片存放处

var $upFileType = "jpg gif png";//上传的类型,默认为:jpg gif png rar zip

var $upFileMax = 1024;//上传大小限制,单位是“KB”,默认为:1024KB

var $fontType;//字体

var $maxWidth = 500; //图片最大宽度

var $maxHeight = 600; //图片最大高度

function UpImages($annexFolder,$smallFolder,$includeFolder) {

$this->annexFolder = $annexFolder;

$this->smallFolder = $smallFolder;

$this->fontType = $includeFolder."/04B_08__.TTF";

}

function upLoad($inputName) {

$imageName = time();//设定当前时间为图片名称

if(@empty($_FILES[$inputName]["name"])) die("没有上传图片信息,请确认");

$name = explode(".",$_FILES[$inputName]["name"]);//将上传前的文件以“.”分开取得文件类型

$imgCount = count($name);//获得截取的数量

$imgType = $name[$imgCount-1];//取得文件的类型

if(strpos($this->upFileType,$imgType) === false) die(error("上传文件类型仅支持 ".$this->upFileType." 不支持 ".$imgType));

$photo = $imageName.".".$imgType;//写入数据库的文件名

$uploadFile = $this->annexFolder."/".$photo;//上传后的文件名称

$upFileok = move_uploaded_file($_FILES[$inputName]["tmp_name"],$uploadFile);

if($upFileok) {

$imgSize = $_FILES[$inputName]["size"];

$kSize = round($imgSize/1024);

if($kSize > ($this->upFileMax*1024)) {

@unlink($uploadFile);

die(error("上传文件超过 ".$this->upFileMax."KB"));

}

} else {

die(error("上传图片失败,请确认你的上传文件不超过 $upFileMax KB 或上传时间超时"));

}

return $photo;

}

function getInfo($photo) {

$photo = $this->annexFolder."/".$photo;

$imageInfo = getimagesize($photo);

$imgInfo["width"] = $imageInfo[0];

$imgInfo["height"] = $imageInfo[1];

$imgInfo["type"] = $imageInfo[2];

$imgInfo["name"] = basename($photo);

return $imgInfo;

}

function smallImg($photo,$width=128,$height=128) {

$imgInfo = $this->getInfo($photo);

$photo = $this->annexFolder."/".$photo;//获得图片源

$newName = substr($imgInfo["name"],0,strrpos($imgInfo["name"], "."))."_thumb.jpg";//新图片名称

if($imgInfo["type"] == 1) {

$img = imagecreatefromgif($photo);

} elseif($imgInfo["type"] == 2) {

$img = imagecreatefromjpeg($photo);

} elseif($imgInfo["type"] == 3) {

$img = imagecreatefrompng($photo);

} else {

$img = "";

}

if(empty($img)) return False;

$width = ($width > $imgInfo["width"]) ? $imgInfo["width"] : $width;

$height = ($height > $imgInfo["height"]) ? $imgInfo["height"] : $height;

$srcW = $imgInfo["width"];

$srcH = $imgInfo["height"];

if ($srcW * $width > $srcH * $height) {

$height = round($srcH * $width / $srcW);

} else {

$width = round($srcW * $height / $srcH);

}

if (function_exists("imagecreatetruecolor")) {

$newImg = imagecreatetruecolor($width, $height);

ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);

} else {

$newImg = imagecreate($width, $height);

ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);

}

if ($this->toFile) {

if (file_exists($this->annexFolder."/".$this->smallFolder."/".$newName)) @unlink($this->annexFolder."/".$this->smallFolder."/".$newName);

ImageJPEG($newImg,$this->annexFolder."/".$this->smallFolder."/".$newName);

return $this->annexFolder."/".$this->smallFolder."/".$newName;

} else {

ImageJPEG($newImg);

}

ImageDestroy($newImg);

ImageDestroy($img);

return $newName;

}

function waterMark($photo,$text) {

$imgInfo = $this->getInfo($photo);

$photo = $this->annexFolder."/".$photo;

$newName = substr($imgInfo["name"], 0, strrpos($imgInfo["name"], ".")) . "_mark.jpg";

switch ($imgInfo["type"]) {

case 1:

$img = imagecreatefromgif($photo);

break;

case 2:

$img = imagecreatefromjpeg($photo);

break;

case 3:

$img = imagecreatefrompng($photo);

break;

default:

return False;

}

if (empty($img)) return False;

$width = ($this->maxWidth > $imgInfo["width"]) ? $imgInfo["width"] : $this->maxWidth;

$height = ($this->maxHeight > $imgInfo["height"]) ? $imgInfo["height"] : $this->maxHeight;

$srcW = $imgInfo["width"];

$srcH = $imgInfo["height"];

if ($srcW * $width > $srcH * $height) {

$height = round($srcH * $width / $srcW);

} else {

$width = round($srcW * $height / $srcH);

}

if (function_exists("imagecreatetruecolor")) {

$newImg = imagecreatetruecolor($width, $height);

ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);

} else {

$newImg = imagecreate($width, $height);

ImageCopyResized($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);

}

$white = imageColorAllocate($newImg, 255, 255, 255);

$black = imageColorAllocate($newImg, 0, 0, 0);

$alpha = imageColorAllocateAlpha($newImg, 230, 230, 230, 40);

ImageFilledRectangle($newImg, 0, $height-26, $width, $height, $alpha);

ImageFilledRectangle($newImg, 13, $height-20, 15, $height-7, $black);

ImageTTFText($newImg, 4.9, 0, 20, $height-14, $black, $this->fontType, $text[0]);

ImageTTFText($newImg, 4.9, 0, 20, $height-6, $black, $this->fontType, $text[1]);

if($this->toFile) {

if (file_exists($this->annexFolder."/".$this->markFolder."/".$newName)) @unlink($this->annexFolder."/".$this->markFolder."/".$newName);

ImageJPEG($newImg,$this->annexFolder."/".$this->markFolder."/".$newName);

return $this->annexFolder."/".$this->markFolder."/".$newName;

} else {

ImageJPEG($newImg);

}

ImageDestroy($newImg);

ImageDestroy($img);

return $newName;

}

}

?>

使用方法

复制代码代码如下:

include 'Upimages.class.php';

$max="upload"; //文件上传路径

$mix="small"; //缩略图路径(必须在upload下建立)

$mark="mark"; //加水引的图片存放路径

$text = array("oktang","2012"); //水印内容

$img= new UpImages($max,$mix,$max); //实例化类文件

$photo = $img->upLoad("file"); //上传的文件域

$img->maxWidth = $img->maxHeight = 600; //设置高,和宽

$img->toFile = true;

$newSmallImg = $img->smallImg($photo);

$newMark = $img->waterMark($photo,$text);

echo $newSmallImg;

echo $newMark;

echo "
";

echo "
";

注意里面有个字体文件,大家可以从网上下载。

超级好用的一个php上传图片类(随机名_缩略图_加水印),超级好用的一个php上传图片类(随机名,缩略图,加水印)...相关推荐

  1. 一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。

    一个封装的使用Apache HttpClient进行Http请求(GET.POST.PUT等)的类. import com.qunar.payment.gateway.front.channel.mp ...

  2. 构造方法与重载:定义一个网络用户类,信息有用户 ID、用户密码、 email 地址。在建立类的实例时把以上三个信息都作为构造函数的参数输入

    构造方法与重载:定义一个网络用户类,信息有用户 ID.用户密码. email 地址.在建立类的实例时把以上三个信息都作为构造函数的参数输入, 其中用户 ID 和用户密码时必须缺省时 email地址是用 ...

  3. Java黑皮书课后题第5章:**5.38(十进制转八进制)编写程序,提示用户输入一个十进制整数,然后显示对应的八进制值。(不要是使用Java的Integer类的任何方法)

    **5.38(十进制转八进制)编写程序,提示用户输入一个十进制整数,然后显示对应的八进制值.(不要是使用Java的Integer类的任何方法) 题目 题目概述 破题 代码 运行示例 题目 题目概述 * ...

  4. weblogic启动项目报错找不到类_启动类报错是经常出现的事但是单一的从一个地方找原因会越找越错...

    Error starting ApplicationContext. To display the conditions   report rerun your application with 'd ...

  5. Java 核心五个类(File、Outputstream、Inputstream、Reader、Writer)一个接口(Serializable)...

    java BIO(阻塞式IO)    java.io 核心五个类(File.Outputstream.Inputstream.Reader.Writer)一个接口(Serializable) 1.Fi ...

  6. vb6.0 定义一个公共类_纠正网上的错误:能不能自定义一个类叫java.lang.System/String?...

    前语:不要为了读文章而读文章,一定要带着问题来读文章,勤思考. 作者:一汪清水  来源:https://dwz.cn/i7Pf6VwZ 最近,学习了下java类加载相关的知识.然后看到网上有一道面试题 ...

  7. 设计如下类: 1) 建立一个Point类,表示平面中的一个点;建立一个Line类,表示平面中的一条线端, 内含两个Point类的对象;建立Triangle类,表示一个三角形

    设计如下类:     1) 建立一个Point类,表示平面中的一个点:建立一个Line类,表示平面中的一条线端,     内含两个Point类的对象:建立Triangle类,表示一个三角形,内含三个L ...

  8. Python输入一个字符串,输出其中每个字符的出现次数。要求使用标准库collotections中的Counter类...

    一.题目: 1.输入一个字符串,输出其中每个字符的出现次数.要求使用标准库collotections中的Counter类. 2.输入一个字符串,输出其中只出现了一次的字符及其下标. 3.输入一个字符串 ...

  9. SQL Server 安装程序遇到以下错误: 无法生成临时类(result=1)。 error CS1567: 生成 Win32 资源时出错: 另一个程序正在使用此文件,进程无法访问。...

    SQL Server 安装程序遇到以下错误: 无法生成临时类(result=1). error CS1567: 生成 Win32 资源时出错: 另一个程序正在使用此文件,进程无法访问. 如果你装了千牛 ...

  10. 实现父类一个动物的类, 包括成员变量名字年龄皮毛颜色,带参数构造函数,动物类有一个方法,move,打印动物是可以动的 1.《实现一个子类老鼠的类,继承动物类,老鼠类继承父类成员变量,老鼠还有个自己的属

    编写一个程序,程序包括如下内容 实现父类一个动物的类, 包括成员变量名字年龄皮毛颜色,带参数构造函数,动物类有一个方法,move,打印动物是可以动的 1.<实现一个子类老鼠的类,继承动物类,老鼠 ...

最新文章

  1. 44 jQuery概述和基本使用
  2. 清理多个varnish服务器缓存的脚本
  3. spring-boot 中实现标准 redis 分布式锁
  4. 全志A33-USB虚拟网卡的配置与使用
  5. 收集一些非常实用的Linux命令
  6. mgg mysql_mgg文件怎么转换mp3格式?
  7. AOS V0.8 发布,JavaEE 应用基础平台
  8. python处理路径时 sh: 1:Syntax error: ( unexpected
  9. threadingdaemonmultiprocessing
  10. php把数据表格数据,php怎样把数据添加到数据表
  11. 《AutoCAD 2014中文版超级学习手册》——1.3 设置绘图环境
  12. Spark的RDD概要DAG概述
  13. c++ 工厂模式_Java面试专题之五:设计模式学习,详细分析工厂方法模式
  14. centos分区方案
  15. 【无机纳米材料科研制图——OriginLab 0204】Origin细胞存活率柱状图绘制
  16. max30102c语言程序,STM32驱动MAX30102源码
  17. 掘金往期沸点神评合集(别审过)
  18. 计算机科学与技术专业课程简介
  19. 从 SEC EDGAR 获取股东治理数据 (Shareholder Activism)
  20. 一直被世人误解的薛定谔猫:批判量子力学及其残忍的隐喻

热门文章

  1. Hibernate inverse 详解
  2. Java虚拟机:关于JDK8到JDK9-10的一点小变化(tools.jar、dt.jar)
  3. ACM经典书籍推荐 (算法)
  4. 3、通配符与特殊符号
  5. linux centos7.6 关闭防火墙
  6. python airflow_Airflow 安装
  7. 第三方JavaScript的用法
  8. 蓝牙、WIFI和802.11b
  9. WPS Office之PPT动画应用技能-陈慧-专题视频课程
  10. 分享一道算法面试题和它的三种解法