php图像处理类,上传,压缩,添加文字、图片水印
2011-08-27 18:07
1251 查看
<?php
require_once 'Fun/Common.php';
/**
* 图片处理类
* @author dxk http://hi.baidu.com/1024114660
* @version 2011-08-25
*/
class Class_Image
{
private $error = '';
private $maxSize = - 1;
private $basedir = 'image';
private $allowTypes = array('image/gif', 'image/jpg', 'image/jpeg',
'image/pjpeg', 'image/png');
private $savePath;
/**
* 上传文件
* @param array $file 上传的文件
* @param string $fileName 要设置的文件名
* @param string $dir 保存的目录路径
* @param mixed $allowTypes 允许上传的文件类型
* @param int $maxSize 允许上传的最大文件大小
* @return mixed 成功是返回文件保存位置,失败返回false
*/
public function upload ($file, $fileName = '', $dir = '', $allowTypes = '',
$maxSize = '')
{
if (! empty($maxSize) && is_numeric($maxSize)) {
$this->maxSize = $maxSize;
}
if (! empty($allowTypes)) {
if (is_array($allowTypes)) {
$this->allowTypes = array_map('strtolower', $allowTypes);
} else {
$this->allowTypes = explode(',', strtolower($allowTypes));
}
}
//检查文件合法性
if (! $this->check($file)) {
return false;
}
//设置目录路径
if (empty($dir)) {
$dir = $this->basedir;
}
//如果目标目录不存在,则创建它
if (! file_exists($dir)) {
if (! make_dir($dir)) {
//创建目录失败
$this->error = '创建目录失败' . $dir;
return false;
}
}
//设置图片保存路径
if (empty($fileName)) {
$this->savePath = $dir . '/' . $file['name'];
} else {
$pathinfo = pathinfo($file['name']);
$this->savePath = $dir . '/' . $fileName . '.' .
$pathinfo['extension'];
}
//保存图片(如果存在同名图片会覆盖掉)
if ($this->move_file($file, $this->savePath)) {
return $this->savePath;
} else {
return false;
}
}
/**
* 生成缩略图
* @param string $img_path 原图路径
* @param int $thumb_width 新图宽度
* @param int $thumb_height 新图高度
* @param string $image_type 新图图片格式
* @param bool $is_equal 是否等比缩放
* @param string $target_path 图片存储目标位置,不包括后缀名,如果为空则和原图在一个目录下如果和原图图片格式一致则覆盖原图
* @return mixed 成功是返回图片保存位置,失败返回false
*/
function make_thumb ($img_path, $thumb_width = 0, $thumb_height = 0,
$image_type = 'png', $is_equal = true, $target_path = '')
{
//检查原图是否存在
if (empty($img_path) || ! is_file($img_path)) {
$this->error = '所要处理的图片不存在' . $img_path;
return false;
}
//是否覆盖原图
if (! empty($target_path)) {
//如果目标目录不存在,则创建它
$dir = dirname($target_path);
if (! file_exists($dir)) {
if (! make_dir($dir)) {
//创建目录失败
$this->error = '创建目录失败' . $dir;
return false;
}
}
$this->savePath = $target_path . '.' . $image_type;
} else {
$this->savePath = dirname($img_path) . '.' . $image_type;
}
$image = new Imagick($img_path);
//根据是否等比缩放生成缩略图
if (! $image->thumbnailimage($thumb_width, $thumb_height,
$is_equal)) {
$this->error = '压缩失败';
return false;
}
//设置图片格式
if (! $image->setimageformat($image_type)) {
$this->error = '设置图片格式失败' . $image_type;
return false;
}
if ($image->writeimage($this->savePath)) {
$image->destroy();
return $this->savePath;
} else {
$image->destroy();
$this->error = '压缩图片失败';
return false;
}
}
/**
* 添加水印
* @param string $img_path 原图路径
* @param mixed $water_info 水印图片路径或者水印文字信息数组array('text'=>'','color'=>'#666666',size=20)
* @param string $target_path 目标图片存放完整路径,如果为空覆盖原图,包括文件名
* @param int $water_place 水印位置 1-9
* @param float $water_alpha 水印透明度
* @return mixed 成功返回图片路径,失败返回false
*/
public function make_water ($img_path, $water_info, $target_path = '',
$water_place = 1, $water_alpha = 0.6)
{
//检查原图是否存在
if (empty($img_path) || ! is_file($img_path)) {
$this->error = '所要处理的图片不存在' . $img_path;
return false;
}
//是否覆盖原图
if (! empty($target_path)) {
//如果目标目录不存在,则创建它
$dir = dirname($target_path);
if (! file_exists($dir)) {
if (! make_dir($dir)) {
//创建目录失败
$this->error = '创建目录失败' . $dir;
return false;
}
}
$this->savePath = $target_path;
} else {
$this->savePath = $img_path;
}
$image = new Imagick($img_path);
$dw = new ImagickDraw();
if (is_string($water_info)) {
//检查水印图片是否存在
if (empty($water_info) || ! is_file($water_info)) {
$this->error = '水印图片不存在' . $water_info;
$image->destroy();
$dw->destroy();
return false;
}
//加图片水印
$water = new Imagick($water_info);
//设置水印透明度
$water->setimageopacity($water_alpha);
$dw->setgravity($water_place); //设置水印位置
if (! $dw->composite($water->getimagecompose(), 0, 0, 0,
0, $water)) {
$this->error = '添加水印失败';
$water->destroy();
return false;
}
$water->destroy();
} elseif (is_array($water_info)) {
//加文字水印
$dw->setfontsize($water_info['size']);
$dw->setfillcolor($water_info['color']);
$dw->setfont('fonts/simkai.ttf');
$dw->setgravity($water_place);
$dw->annotation(0, 0, $water_info['text']);
} else {
$image->destroy();
$dw->destroy();
return false;
}
$image->drawimage($dw);
if (! $image->writeimage($this->savePath)) {
$this->error = '生成缩略图失败';
$image->destroy();
$dw->destroy();
return false;
} else {
$image->destroy();
$dw->destroy();
return $this->savePath;
}
}
/**
* 检查上传的文件是否合法
* @param array $file 上传的文件
*/
private function check ($file)
{
if ($file['error'] !== 0) {
//文件上传失败
//捕获错误代码
$this->error = $file['error'];
return false;
}
//文件上传成功,进行自定义规则检查
//检查文件大小
if (! $this->checkSize($file['size'])) {
$this->error = '上传文件大小不符!';
return false;
}
//检查文件Mime类型
if (! $this->checkType($file['type'])) {
$this->error = '上传文件MIME类型不允许!';
return false;
}
//检查是否合法上传
if (! $this->checkUpload($file['tmp_name'])) {
$this->error = '非法上传文件!';
return false;
}
return true;
}
/**
* 检查文件大小是否合法
* @param int $size 文件大小
*/
private function checkSize ($size)
{
return ! ($size > $this->maxSize) || (- 1 == $this->maxSize);
}
/**
* 检查文件类型
* @param string $type 文件类型
*/
private function checkType ($type)
{
if (! empty($this->allowTypes))
return in_array(strtolower($type), $this->allowTypes);
return true;
}
/**
* 检查是否非法提交
* @param string $filename 文件名
*/
private function checkUpload ($filename)
{
return is_uploaded_file($filename);
}
/**
* 根据目录创建唯一随即文件名
* @param string $dir 文件存放的目录名
*/
function unique_name ($dir = '')
{
$filename = '';
while (empty($filename)) {
$filename = rand_string();
if (file_exists($dir . $filename . '.jpg') ||
file_exists($dir . $filename . '.gif') ||
file_exists($dir . $filename . '.png')) {
$filename = '';
}
}
return $filename;
}
/**
* 获取错误消息
*/
public function getErrorMsg ()
{
return $this->error;
}
/**
* 移动文件到目标位置
* @param array $file 文件
* @param string $target 目标文件
*/
function move_file ($file, $target)
{
if (isset($file['error']) && $file['error'] > 0) {
return false;
}
if (move_uploaded_file($file['tmp_name'], $target)) {
return true;
} elseif (copy($file['tmp_name'], $target)) {
return true;
}
return true;
}
}
require_once 'Fun/Common.php';
/**
* 图片处理类
* @author dxk http://hi.baidu.com/1024114660
* @version 2011-08-25
*/
class Class_Image
{
private $error = '';
private $maxSize = - 1;
private $basedir = 'image';
private $allowTypes = array('image/gif', 'image/jpg', 'image/jpeg',
'image/pjpeg', 'image/png');
private $savePath;
/**
* 上传文件
* @param array $file 上传的文件
* @param string $fileName 要设置的文件名
* @param string $dir 保存的目录路径
* @param mixed $allowTypes 允许上传的文件类型
* @param int $maxSize 允许上传的最大文件大小
* @return mixed 成功是返回文件保存位置,失败返回false
*/
public function upload ($file, $fileName = '', $dir = '', $allowTypes = '',
$maxSize = '')
{
if (! empty($maxSize) && is_numeric($maxSize)) {
$this->maxSize = $maxSize;
}
if (! empty($allowTypes)) {
if (is_array($allowTypes)) {
$this->allowTypes = array_map('strtolower', $allowTypes);
} else {
$this->allowTypes = explode(',', strtolower($allowTypes));
}
}
//检查文件合法性
if (! $this->check($file)) {
return false;
}
//设置目录路径
if (empty($dir)) {
$dir = $this->basedir;
}
//如果目标目录不存在,则创建它
if (! file_exists($dir)) {
if (! make_dir($dir)) {
//创建目录失败
$this->error = '创建目录失败' . $dir;
return false;
}
}
//设置图片保存路径
if (empty($fileName)) {
$this->savePath = $dir . '/' . $file['name'];
} else {
$pathinfo = pathinfo($file['name']);
$this->savePath = $dir . '/' . $fileName . '.' .
$pathinfo['extension'];
}
//保存图片(如果存在同名图片会覆盖掉)
if ($this->move_file($file, $this->savePath)) {
return $this->savePath;
} else {
return false;
}
}
/**
* 生成缩略图
* @param string $img_path 原图路径
* @param int $thumb_width 新图宽度
* @param int $thumb_height 新图高度
* @param string $image_type 新图图片格式
* @param bool $is_equal 是否等比缩放
* @param string $target_path 图片存储目标位置,不包括后缀名,如果为空则和原图在一个目录下如果和原图图片格式一致则覆盖原图
* @return mixed 成功是返回图片保存位置,失败返回false
*/
function make_thumb ($img_path, $thumb_width = 0, $thumb_height = 0,
$image_type = 'png', $is_equal = true, $target_path = '')
{
//检查原图是否存在
if (empty($img_path) || ! is_file($img_path)) {
$this->error = '所要处理的图片不存在' . $img_path;
return false;
}
//是否覆盖原图
if (! empty($target_path)) {
//如果目标目录不存在,则创建它
$dir = dirname($target_path);
if (! file_exists($dir)) {
if (! make_dir($dir)) {
//创建目录失败
$this->error = '创建目录失败' . $dir;
return false;
}
}
$this->savePath = $target_path . '.' . $image_type;
} else {
$this->savePath = dirname($img_path) . '.' . $image_type;
}
$image = new Imagick($img_path);
//根据是否等比缩放生成缩略图
if (! $image->thumbnailimage($thumb_width, $thumb_height,
$is_equal)) {
$this->error = '压缩失败';
return false;
}
//设置图片格式
if (! $image->setimageformat($image_type)) {
$this->error = '设置图片格式失败' . $image_type;
return false;
}
if ($image->writeimage($this->savePath)) {
$image->destroy();
return $this->savePath;
} else {
$image->destroy();
$this->error = '压缩图片失败';
return false;
}
}
/**
* 添加水印
* @param string $img_path 原图路径
* @param mixed $water_info 水印图片路径或者水印文字信息数组array('text'=>'','color'=>'#666666',size=20)
* @param string $target_path 目标图片存放完整路径,如果为空覆盖原图,包括文件名
* @param int $water_place 水印位置 1-9
* @param float $water_alpha 水印透明度
* @return mixed 成功返回图片路径,失败返回false
*/
public function make_water ($img_path, $water_info, $target_path = '',
$water_place = 1, $water_alpha = 0.6)
{
//检查原图是否存在
if (empty($img_path) || ! is_file($img_path)) {
$this->error = '所要处理的图片不存在' . $img_path;
return false;
}
//是否覆盖原图
if (! empty($target_path)) {
//如果目标目录不存在,则创建它
$dir = dirname($target_path);
if (! file_exists($dir)) {
if (! make_dir($dir)) {
//创建目录失败
$this->error = '创建目录失败' . $dir;
return false;
}
}
$this->savePath = $target_path;
} else {
$this->savePath = $img_path;
}
$image = new Imagick($img_path);
$dw = new ImagickDraw();
if (is_string($water_info)) {
//检查水印图片是否存在
if (empty($water_info) || ! is_file($water_info)) {
$this->error = '水印图片不存在' . $water_info;
$image->destroy();
$dw->destroy();
return false;
}
//加图片水印
$water = new Imagick($water_info);
//设置水印透明度
$water->setimageopacity($water_alpha);
$dw->setgravity($water_place); //设置水印位置
if (! $dw->composite($water->getimagecompose(), 0, 0, 0,
0, $water)) {
$this->error = '添加水印失败';
$water->destroy();
return false;
}
$water->destroy();
} elseif (is_array($water_info)) {
//加文字水印
$dw->setfontsize($water_info['size']);
$dw->setfillcolor($water_info['color']);
$dw->setfont('fonts/simkai.ttf');
$dw->setgravity($water_place);
$dw->annotation(0, 0, $water_info['text']);
} else {
$image->destroy();
$dw->destroy();
return false;
}
$image->drawimage($dw);
if (! $image->writeimage($this->savePath)) {
$this->error = '生成缩略图失败';
$image->destroy();
$dw->destroy();
return false;
} else {
$image->destroy();
$dw->destroy();
return $this->savePath;
}
}
/**
* 检查上传的文件是否合法
* @param array $file 上传的文件
*/
private function check ($file)
{
if ($file['error'] !== 0) {
//文件上传失败
//捕获错误代码
$this->error = $file['error'];
return false;
}
//文件上传成功,进行自定义规则检查
//检查文件大小
if (! $this->checkSize($file['size'])) {
$this->error = '上传文件大小不符!';
return false;
}
//检查文件Mime类型
if (! $this->checkType($file['type'])) {
$this->error = '上传文件MIME类型不允许!';
return false;
}
//检查是否合法上传
if (! $this->checkUpload($file['tmp_name'])) {
$this->error = '非法上传文件!';
return false;
}
return true;
}
/**
* 检查文件大小是否合法
* @param int $size 文件大小
*/
private function checkSize ($size)
{
return ! ($size > $this->maxSize) || (- 1 == $this->maxSize);
}
/**
* 检查文件类型
* @param string $type 文件类型
*/
private function checkType ($type)
{
if (! empty($this->allowTypes))
return in_array(strtolower($type), $this->allowTypes);
return true;
}
/**
* 检查是否非法提交
* @param string $filename 文件名
*/
private function checkUpload ($filename)
{
return is_uploaded_file($filename);
}
/**
* 根据目录创建唯一随即文件名
* @param string $dir 文件存放的目录名
*/
function unique_name ($dir = '')
{
$filename = '';
while (empty($filename)) {
$filename = rand_string();
if (file_exists($dir . $filename . '.jpg') ||
file_exists($dir . $filename . '.gif') ||
file_exists($dir . $filename . '.png')) {
$filename = '';
}
}
return $filename;
}
/**
* 获取错误消息
*/
public function getErrorMsg ()
{
return $this->error;
}
/**
* 移动文件到目标位置
* @param array $file 文件
* @param string $target 目标文件
*/
function move_file ($file, $target)
{
if (isset($file['error']) && $file['error'] > 0) {
return false;
}
if (move_uploaded_file($file['tmp_name'], $target)) {
return true;
} elseif (copy($file['tmp_name'], $target)) {
return true;
}
return true;
}
}
相关文章推荐
- C#中基于GDI+(Graphics)图像处理系列之UEditor上传图片自动压缩优化(添加水印)
- PHP 给上传到网页的图片添加水印(文字和图像)
- php 之 图片水印 (支持批量图片处理)(支持添加图片水印,文字水印)
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
- PHP图片上传加水印(文字水印和图像水印)
- php 使用GD库压缩图片,添加文字图片水印
- 上传 添加图片水印 添加文字水印 图片剪裁 压缩图片 返回文件类型
- PHP图像处理函数:文字水印,图片水印,缩略图(等比缩放)
- php实现图片上传时添加文字和图片水印技巧
- javacpp-opencv图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体、位置、大小、粗度、翻转、平滑等操作
- javacpp-opencv图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体、位置、大小、粗度、翻转、平滑等操作
- php实现图片上传时添加文字和图片水印技巧
- php 图片添加文字水印 以及 图片合成(微信快码传播)
- ImagesUtil 图片处理 缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印
- PHP图片处理函数 类 (水印图,缩略图)[关于等比例压缩与裁剪压缩]
- 利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现
- C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示)。。。。。。。。。。。。
- PHP 图片上传,添加字符串水印,添加图片(指定四个角) 图片压缩
- php图片添加文字水印实现代码