图片处理类

test.php

 <?php

     include "images.class.php";

     $image=new Image("./images/");    

     //对图片进行缩放

     echo $image->thumb("hee.jpg",300,300,"th1_");
echo $image->thumb("hee.jpg",200,200,"th2_");
echo $image->thumb("hee.jpg",100,100,"th3_"); //对图片进行加水印
echo $image->waterMark("mag.gif","gaolf.gif",2,"wa2_");
echo $image->waterMark("mag.gif","gaolf.gif",6,"wa6_");
echo $image->waterMark("mag.gif","gaolf.gif",7,"wa7_");
?>

images.class.php

 <?php
class Image {
private $path; //构造方法用来对图片所在位置进行初始化
function __construct($path="./"){ $this->path=rtrim($path,"/")."/"; //用户在输入路径时,无斜杠则加斜杠,有斜杠则删掉再加上
} /* 功能:对图片进行缩放
*
* 参数$name:需处理的图片名称
* 参数$width:缩放后的宽度
* 参数$height:缩放后的高度
* 参数$qz:新图片的名称前缀
* 返回值:缩放后的图片名称,失败返回false
*
*/
function thumb($name,$width,$height,$qz="th_"){
//获取图片信息
$imgInfo=$this->getInfo($name); //原图片的信息 //获取图片资源,通用各种类型的图片(png,jpg,gif)
$srcImg=$this->getImg($name,$imgInfo); //获取计算图片等比例之后的大小
$size=$this->getNewSize($name,$width,$height,$imgInfo); //获取新的图片资源,处理gif透明背景问题
$newImg=$this->kid0fImage($srcImg,$size,$imgInfo); //另存为一个新的图片,返回新的缩放后的图片名称
return $this->createNewImage($newImg,$qz.$name,$imgInfo);
} private function createNewImage($newImg,$newName,$imgInfo){
//另存图片
switch($imgInfo["type"]){
case 1: //gif
$result=imagegif($newImg,$this->path.$newName);
break;
case 2: //jpg
$result=imagejpeg($newImg,$this->path.$newName);
break;
case 3: //png
$result=imagepng($newImg,$this->path.$newName);
break;
}
imagedestroy($newImg);
return $newName;
}
private function kid0fImage($srcImg,$size,$imgInfo){
//创建新图片资源 $newImg=imagecreatetruecolor($size["width"],$size["height"]); //取出透明色指数
$otsc=imagecolortransparent($srcImg); //判断是否有透明色 //()取得一幅图像的调色板中颜色的数目
if($otsc >=0 && $otsc <= imagecolorstotal($srcImg)){
$tran = imagecolorsforindex($srcImg,$otsc); //取得某索引的颜色 $newt = imagecolorallocate($newImg,$tran["red"],$tran["green"],$tran["blue"]); //为一幅图片分配颜色 imagefill($newImg,0,0,$newt); //填充颜色 imagecolortransparent($newImg,$newt); //将某个颜色定义为透明色
}
//拷贝部分图像并调整大小
imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]); imagedestroy($srcImg);
return $newImg;
} private function getNewSize($name,$width,$height,$imgInfo){
$size["width"]=$imgInfo["width"];
$size["height"]=$imgInfo["height"];
//如果缩放后宽度小于原图片宽度,再重新设置图片宽度
if($width < $imgInfo["width"]){
$size["width"]=$width;
}
//如果缩放后高度小于原图高度,再重新设置图片高度
if($height < $imgInfo["height"]){
$size["height"]=$height;
} //图片等比例缩放的算法
if($imgInfo["width"]*$width > $imgInfo["height"]*$height){
$size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);
}else{
$size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);
} return $size;
}
private function getInfo($name){
$date=getImageSize($this->path.$name); $imageInfo["width"]=$date[0];
$imageInfo["height"]=$date[1];
$imageInfo["type"]=$date[2]; return $imageInfo;
}
private function getImg($name,$imgInfo){
$srcPic=$this->path.$name; //某路径下的图片 switch($imgInfo["type"]){
case "1": //gif
$img=imagecreatefromgif($srcPic);
break;
case "2": //jpg
$img=imagecreatefromjpeg($srcPic);
break;
case "3": //png
$img=imagecreatefrompng($srcPic);
break;
default:
return false;
}
return $img;
} /* 功能:为图片加水印
*
* 参数$groundName:背景图片,即需要加水印的图片
* 参数$waterMark:水印图片
* 参数$waterPos:水印位置,10种状态
* 0随机位置
* 1顶端居左 2顶端居中 3顶端居右
* 4中部居左 5中部居中 6中部居右
* 7底部居左 8底部居中 9底部居右
*
* 参数$qz:是加水印后图片名称的前缀
* 返回值:处理后图片的名称
*/
function waterMark($groundName,$waterName,$waterPos=0,$qz="wa_"){
if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){
$groundInfo = $this->getInfo($groundName);
$waterInfo = $this->getInfo($waterName);
//水印位置
if(!$pos = $this->position($groundInfo,$waterInfo,$waterPos)){
echo "水印不应该比背景图片小";
return;
}
$groundImg = $this->getImg($groundName,$groundInfo);
$waterImg = $this->getImg($waterName, $waterInfo); $groundImg = $this->copyImage($groundImg, $waterImg, $pos, $waterInfo);
return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo); }else{
echo "图片或水印不存在";
return false;
}
} private function copyImage($groundImg, $waterImg, $pos, $waterInfo){
imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"], $waterInfo["height"]); imagedestroy($waterImg); return $groundImg;
}
private function position($groundInfo,$waterInfo,$waterPos){
//需要背景比水印图片大
if(($groundInfo["width"] < $waterInfo["width"]) || ($groundInfo["height"] < $waterInfo["height"])){
return false;
}
switch($waterPos){
case 1: //顶部居左
$posX=0;
$posY=0;
break;
case 2: //顶部居中
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=0;
break;
case 3: //顶部居右
$posX=($groundInfo["width"]-$waterInfo["width"]);
$posY=0;
break;
case 4: //中部居左
$posX=0;
$posY=($groundInfo["height"]-$waterInfo["height"])/2;
break;
case 5: //中部居中
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=($groundInfo["height"]-$waterInfo["height"])/2;
break;
case 6: //中部居右
$posX=($groundInfo["width"]-$waterInfo["width"]);
$posY=($groundInfo["height"]-$waterInfo["height"])/2;
break;
case 7: //底部居左
$posX=0;
$posY=($groundInfo["height"]-$waterInfo["height"]);
break;
case 8: //底部居中
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=($groundInfo["height"]-$waterInfo["height"]);
break;
case 9: //底部居右
$posX=($groundInfo["width"]-$waterInfo["width"]);
$posY=($groundInfo["height"]-$waterInfo["height"]);
break;
case 0: //随机位置
$posX=rand(0,($groundInfo["width"]-$waterInfo["width"]));
$posY=rand(0,($groundInfo["height"]-$waterInfo["height"]));
break;
}
return array("posX"=>$posX, "posY"=>$posY);
}
}
?>

文件上传类

 <?php
/* 该用于文件上传
* 有4个公有方法可以在对象外部调用:
* __construct()构造方法用于初使化成员属性
* uploadFile()方法用于上传文件
* getNewFileName()方法用于获取上传成功后的文件名称
* getErrorMsg()方法用于上传失败后获取错误提示信息
* 其它属性和方法都被本类封装,不可以在对象外部调用
*/
class FileUpload {
private $filepath; // 上传文件的目的路径
private $allowtype = array('jpg','gif','png'); //充许上传文件的类型,使用小字母
private $maxsize = 1000000; //允许文件上传的最大长度1m
private $israndname = true; //是否随机重命名 false为不随机
private $originName; //源文件名
private $tmpFileName; //临时文件名
private $fileType; //文件类型(文件后缀)
private $fileSize; //文件大小
private $newFileName; //新文件名
private $errorNum = 0; //错误号
private $errorMess=""; //错误报告消息
/* 构造方法:为成员属性初使化
* 参数$options:为一个数组,数组下标为成员员属性名称字符串
* 本类需要初使化的属性有 filepath, allowtype, maxsize,israndname四个属性,其中filepath为必须设置的属性
* 使用的格式为 new FileUpload(array('filepath'=>'./uploads', 'maxsize'=>10000000)) 的格式
*/
function __construct($options=array()) {
foreach ($options as $key=>$val) {
$key=strtolower($key); //在为成员属性设置值时,不区分大小写
if (!in_array($key,get_class_vars(get_class($this))))
continue;
$this->setOption($key, $val);
}
} /* 调用该方法上传文件
* 参数: 上传文件的表单名称 例如:<input type="file" name="myfile"> 参数则为myfile
* 返回值: 如果上传成功返回数字0,如果上传失败则返回小于0的数,如:-1、-2、-3、-4、-5中的一个
*/ function uploadFile($fileField) {
$return=true;
if(!$this->checkFilePath()) {//检查文件路径
$this->errorMess=$this->getError();
return false;
}
$name=$_FILES[$fileField]['name'];
$tmp_name=$_FILES[$fileField]['tmp_name'];
$size=$_FILES[$fileField]['size'];
$error=$_FILES[$fileField]['error']; if(is_Array($name)){ //如果是多个文件上传则$file["name"]会是一个数组
$errors=array();
for($i = 0; $i < count($name); $i++){
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) {//设置文件信息
if(!$this->checkFileSize() || !$this->checkFileType()){
$errors[]=$this->getError();
$return=false;
}
}else{
$errors[]=$this->getError();
$return=false;
} if(!$return) // 如果有问题,则重新初使化属性
$this->setFiles();
} if($return){
$fileNames=array(); //存放所有上传后文件名的变量数组 for($i = 0; $i < count($name); $i++){
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) {//设置文件信息
$this->setNewFileName(); //设置新文件名
if(!$this->copyFile()){
$errors[]=$this->getError();
$return=false;
}
$fileNames[]=$this->newFileName;
} }
$this->newFileName=$fileNames; }
$this->errorMess=$errors;
return $return; } else {
if($this->setFiles($name,$tmp_name,$size,$error)) {//设置文件信息
if($this->checkFileSize() && $this->checkFileType()){
$this->setNewFileName(); //设置新文件名
if($this->copyFile()){ //上传文件 返回0为成功, 小于0都为错误
return true;
}else{
echo '3333333333333';
$return=false;
}
}else{
$return=false;
}
} else {
$return=false;
} if(!$return)
$this->errorMess=$this->getError(); return $return;
} } /* 获取上传后的文件名称
* 没有参数
* 返回值:上传后,新文件的名称
*/
public function getNewFileName(){
return $this->newFileName;
} public function getErrorMsg(){
return $this->errorMess;
} /* 上传失败后,调用该方法则返回,上传出错信息
* 没有参数
* 返回值:返回上传文件出错的信息提示
*/
private function getError() {
$str = "上传文件<font color='red'>{$this->originName}</font>时出错 : ";
switch ($this->errorNum) {
case 4: $str .= "没有文件被上传"; break;
case 3: $str .= "文件只有部分被上传"; break;
case 2: $str .= "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值"; break;
case 1: $str .= "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值"; break;
case -1: $str .= "未允许类型"; break;
case -2: $str .= "文件过大,上传的文件不能超过{$this->maxsize}个字节"; break;
case -3: $str .= "上传失败"; break;
case -4: $str .= "建立存放上传文件目录失败,请重新指定上传目录"; break;
case -5: $str .= "必须指定上传文件的路径"; break;
default: $str .= "未知错误";
}
return $str.'<br>';
} //设置和$_FILES有关的内容
private function setFiles($name="", $tmp_name="", $size=0, $error=0) {
$this->setOption('errorNum', $error);
if($error)
return false;
$this->setOption('originName', $name);
$this->setOption('tmpFileName',$tmp_name);
$aryStr = explode(".", $name);
$this->setOption('fileType', strtolower($aryStr[count($aryStr)-1]));
$this->setOption('fileSize', $size);
return true;
} //为单个成员属性设置值
private function setOption($key, $val) {
$this->$key = $val;
} //设置上传后的文件名称
private function setNewFileName() {
if ($this->israndname) {
$this->setOption('newFileName', $this->proRandName());
} else{
$this->setOption('newFileName', $this->originName);
}
} //检查上传的文件是否是合法的类型
private function checkFileType() {
if (in_array(strtolower($this->fileType), $this->allowtype)) {
return true;
}else {
$this->setOption('errorNum', -1);
return false;
}
}
//检查上传的文件是否是允许的大小
private function checkFileSize() {
if ($this->fileSize > $this->maxsize) {
$this->setOption('errorNum', -2);
return false;
}else{
return true;
}
} //检查是否有存放上传文件的目录
private function checkFilePath() {
if(empty($this->filepath)){
$this->setOption('errorNum', -5);
return false;
}
if (!file_exists($this->filepath) || !is_writable($this->filepath)) {
if (!@mkdir($this->filepath, 0755)) {
$this->setOption('errorNum', -4);
return false;
}
} return true;
}
//设置随机文件名
private function proRandName() {
$fileName=date('YmdHis')."_".rand(100,999); //获取随机文件名
return $fileName.'.'.$this->fileType; //返回文件名加原扩展名
} //复制上传文件到指定的位置
private function copyFile() {
if(!$this->errorNum) {
$filepath = rtrim($this->filepath, '/').'/';
$filepath .= $this->newFileName;
if (@move_uploaded_file($this->tmpFileName, $filepath)) {
return true;
}else{
$this->setOption('errorNum', -3);
return false;
}
} else {
return false;
} } }

FileClass

 <?php
include "FileUpload.class.php";
include "images.class.php"; $up = new FileUpload(array("filepath" => "./images/", "allowtype" => array("gif", "jpg","png"))); if($up->uploadFile("spic")){
$filename = $up->getNewFileName(); $img = new Image("./images"); $th_filename = $img->thumb($filename, 300,300, "th_"); $img->waterMark($th_filename, "gaolf/gif", 5, "wa_");
$img->waterMark($filename, "gaolf.gif", 0, "");
}else{
echo $up->getErrorMsg();
}
?>

upload

PHP.14-图片处理类的更多相关文章

  1. JavaSE学习笔记(14)---File类和IO流(字节流和字符流)

    JavaSE学习笔记(14)---File类和IO流(字节流和字符流) File类 概述 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方 ...

  2. 分享一下怎么开发一款图片视频类App,秒拍和prisma

    第一步,分解短视频App的功能 我们在秒拍官网看到如此描述: [视频拍摄及导入]支持直接拍摄及导入手机本地的视频 [照片电影]照片专属特效,轻松创作照片电影 [MV特效]10余款全新MV特效,让普通视 ...

  3. PHP编写的图片验证码类文件分享方法

    适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...

  4. Java图片工具类,完成图片的截取和任意缩放

    package com.common.util; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Renderin ...

  5. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  6. 拍照、本地图片工具类(兼容至Android7.0)

    拍照.本地图片工具类:解决了4.4以上剪裁会提示"找不到文件"和6.0动态授予权限,及7.0报FileUriExposedException异常问题. package com.hb ...

  7. Android--很实用的图片工具类

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; imp ...

  8. PHP 图片缩放类

    <?php /** * 图片压缩类:通过缩放来压缩. * 如果要保持源图比例,把参数$percent保持为1即可. * 即使原比例压缩,也可大幅度缩小.数码相机4M图片.也可以缩为700KB左右 ...

  9. Android Handler 异步消息处理机制的妙用 创建强大的图片载入类

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38476887 ,本文出自[张鸿洋的博客] 近期创建了一个群.方便大家交流,群号: ...

  10. Android 调节图片工具类

    package com.base.changeimage; import android.graphics.Bitmap; import android.graphics.Canvas; import ...

随机推荐

  1. IEnumerable<T> 用法

    //以下参考来自 http://www.cnblogs.com/wilber2013/p/4299529.html

  2. react-native 视频播放器(很不错哦)

    第一步: npm i -S react-native-af-video-player(安装前:先安装: react-native-video.react-native-keep-awake.react ...

  3. nginx-1.12.2编译安装指导

    nginx-1.12.2编译安装 下载源码包 安装 安装后配置 下载源码包 下载地址:http://nginx.org/en/download.html nginx-1.12.2:http://ngi ...

  4. Laravel 5.5 官方推荐 Nginx 配置学习

    Laravel 5.5 版本官方放出了 Nginx 服务器的配置,中文文档:服务器配置 Nginx server { listen 80; server_name example.com; root ...

  5. 使用函数BAPISDORDER_GETDETAILEDLIST读取S/4HANA中Sales Order行项目数据

    事务码MM03查看物料主数据,如下图所示的行项目数据,包含物料ID,描述信息,数量,单价等等: 使用如下代码进行行项目读取: DATA: ls_read TYPE order_view, lt_ite ...

  6. CRM和C4C product category hierarchy的可编辑性控制逻辑

    CRM 从ERP导入到CRM系统的Product Hierarchy,在CRM系统切换成编辑模式时,会收到一条提示信息: Hierarchy XXX may only be changed in th ...

  7. C++学习之虚析构函数

    什么样的情况下才需要虚析构函数? 类需要控制自己的对象执行一系列操作时发生什么样的行为,这些操作包括:创建(对象).拷贝.移动.赋值和销毁.在继承体系中,如果一个类(基类或其派生的类)没有定义拷贝控制 ...

  8. 基于LBS的多人聊天

  9. IA32的三种地址

    IA32的三种地址 逻辑地址:机器语言指令仍用这种地址指定一个操作数的地址或一条指令的地址. 这种寻址方式在Intel的分段结构中表现得尤为具体,它使得MS-DOS或Windows程序员把程序分为若干 ...

  10. 【CCPC-Wannafly Winter Camp Day4 (Div1) G】置置置换(动态规划)

    点此看题面 大致题意: 求出有多少个长度为\(n\)的排列满足对于奇数位\(a_{i-1}<a_i\),对于偶数位\(a_{i-1}>a_i\). 考虑打表? 考虑每次只有一个数\(n\) ...