一个我经常使用的图片处理类,很好用的。给大家推荐一下!主要包括图片的裁剪,缩放,加水印等。下面是一些简单的使用方法。
include("imageclass.php"); //这里注意imageclass.php的路径
$imgs=new image();
$imgs->param($tempFile);
if(!$imgs->thumb($targetFile,600,500))die('--图像上传失败!'); //对图片进行缩放
$imgs->water($targetFile,$water,$pos=0,$pct=50);主要包括两个参数,$targetFile为已经上传到服务器的文件路径,$water为水印图片,$pos为水印图片位置,$pct为透明度;
$pos位置说明:
0://随机
1://1为顶端居左
2://2为顶端居中
3://3为顶端居右
4://4为中部居左
5://5为中部居中
6://6为中部居右
7://7为底端居左
8://8为底端居中
9://9为底端居右
将//里边的内容保存在imageclass.php里边,就可以调用了。
///////////////////////////////////////////////////////////////
<?php
class image {
// 当前图片
protected $img;
// 图像types 对应表
protected $types = array(
1 => 'gif',
2 => 'jpg',
3 => 'png',
6 => 'bmp'
);
// image
public function __construct($img=''){
!$img &&$this->param($img);
}
// Info
public function param($img){
$this->img = $img;
return $this;
}
// imageInfo
public function getImageInfo($img){
$info = @getimagesize($img);
if(isset($this->types[$info[2]])){
$info['ext'] = $info['type'] =$this->types[$info[2]];
} else{
$info['ext'] = $info['type'] = 'jpg';
}
$info['type'] == 'jpg' &&$info['type'] = 'jpeg';
$info['size'] = @filesize($img);
return $info;
}
// thumb(新图地址, 宽, 高, 裁剪, 允许放大)
public functionthumb($filename,$new_w=160,$new_h=120,$cut=0,$big=0){
// 获取原图信息
$info =$this->getImageInfo($this->img);
if(!empty($info[0])) {
$old_w = $info[0];
$old_h = $info[1];
$type = $info['type'];
$ext =$info['ext'];
unset($info);
// 如果原图比缩略图小 并且不允许放大
if($old_w < $new_h&& $old_h < $new_w&& !$big){
return false;
}
// 裁剪图片
if($cut == 0){ // 等比列
$scale = min($new_w/$old_w, $new_h/$old_h); // 计算缩放比例
$width = (int)($old_w*$scale); // 缩略图尺寸
$height = (int)($old_h*$scale);