php图像处理类

class Image

{

//保存目录

protected $savePath;

//随机名字

protected $randName;

//图片后缀

protected $extension;

//保存文件名

protected $saveFileName;

public function __construct(

$savePath='./',

$randName=true,

$extension='png'

)

{

$this->savePath = $savePath;

$this->randName = $randName;

$this->extension = $extension;

}

public function setOption($name,$value=null)

{

if (is_array($name)) {

foreach ($name as $k => $v) {

$this->$k = $v;

}

} else {

$this->$name = $value;

}

}

public function getSaveFile()

{

return $this->saveFileName;

}

public function waterMark($dstPath,$srcPath,$pos=9,$pct=100)

{

//1、文件目录检测

if (!is_file($dstPath)) {

return '目标大图不存在';

} else if (!is_file($srcPath)) {

return '水印小图不存在';

} else if (!is_dir($this->savePath)) {

return '保存路径不存在';

} else if (!is_writable($this->savePath)) {

return '保存路径不可写';

}

//2、判断图片尺寸

list($dstWidth,$dstHeight) = getimagesize($dstPath);

list($srcWidth,$srcHeight) = getimagesize($srcPath);

if ($srcWidth > $dstWidth || $srcHeight > $dstHeight) {

return '水印图片尺寸过大';

}

//3、计算水印在目标图片上的位置

if ($pos>=1 && $pos<=9) {

$offsetX = ($pos-1)%3 * ceil(($dstWidth-$srcWidth)/2);

$offsetY = floor(($pos-1)/3) * ceil(($dstHeight-$srcHeight)/2);

} else {

$offsetX = mt_rand(0,$dstWidth-$srcWidth);

$offsetY = mt_rand(0,$dstHeight-$srcHeight);

}

//4、将水印图片合并到目标图片上

$dstImg = $this->openImage($dstPath);

$srcImg = $this->openImage($srcPath);

imagecopymerge($dstImg, $srcImg, $offsetX, $offsetY, 0, 0, $srcWidth, $srcHeight, $pct);

//5、保存图片

$this->saveImage($dstImg,$dstPath);

//6、释放资源

imagedestroy($dstImg);

imagedestroy($srcImg);

//7、返回保存文件的路径

return $this->saveFileName;

}

protected function openImage($imagePath)

{

$info = getimagesize($imagePath);

$extension = image_type_to_extension($info[2],false);

$openFunc = 'imagecreatefrom' . $extension;

return $openFunc($imagePath);

}

protected function saveImage($image,$path)

{

//路径

$this->saveFileName = rtrim($this->savePath,'/') . '/';

$info = pathinfo($path);

//名字

if ($this->randName) {

$this->saveFileName .= uniqid();

} else {

$this->saveFileName .= $info['filename'];

}

//后缀

if (empty($this->extension)) {

$this->extension = $info['extension'];

} else {

$this->extension = ltrim($this->extension,'.');

}

//完整路径名

$this->saveFileName .= '.' . $this->extension;

//保存图片

if ($this->extension == 'jpg') {

$this->extension = 'jpeg';

}

$saveFunc = 'image' . $this->extension;

$saveFunc($image,$this->saveFileName);

//返回保存的文件路径名

return $this->saveFileName;

}

public function zoomImage($imgPath,$width,$height)

{

//1、检查文件目录

if (!file_exists($imgPath)) {

return '图片路径不存在';

} else if (!is_dir($this->savePath)) {

return '保存路径不存在';

} else if (!is_writable($this->savePath)) {

return '保存路径不可写';

}

//2、计算尺寸

list($srcWidth,$srcHeight) = getimagesize($imgPath);

$size = $this->getSize($width,$height,$srcWidth,$srcHeight);

//3、合并图片

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

$srcImg = $this->openImage($imgPath);

$this->mergeImage($dstImg,$srcImg,$size);

//4、保存图片

$this->saveImage($dstImg,$imgPath);

//5、释放资源

imagedestroy($dstImg);

imagedestroy($srcImg);

//6、返回保存的文件路径名

return $this->saveFileName;

}

protected function mergeImage($dstImg,$srcImg,$size)

{

//获取原始图片的透明色

$lucidColor = imagecolortransparent($srcImg);

if ($lucidColor == -1) {

//如果没有透明色,默认设置黑色为透明色

$lucidColor = imagecolorallocate($dstImg, 0, 0, 0);

}

//用透明色填充图片

imagefill($dstImg, 0, 0, $lucidColor);

//设置透明色

imagecolortransparent($dstImg,$lucidColor);

//合并图片

imagecopyresampled($dstImg, $srcImg, $size['offsetX'], $size['offsetY'], 0, 0, $size['newWidth'], $size['newHeight'], $size['srcWidth'], $size['srcHeight']);

}

protected function getSize($width,$height,$srcWidth,$srcHeight)

{

//保存原始尺寸

$size['srcWidth'] = $srcWidth;

$size['srcHeight'] = $srcHeight;

//计算缩放比例

$scaleWidth = $width / $srcWidth;

$scaleHeight = $height / $srcHeight;

$scaleFinal = min($scaleWidth,$scaleHeight);

//保存实际尺寸

$size['newWidth'] = $srcWidth * $scaleFinal;

$size['newHeight'] = $srcHeight * $scaleFinal;

//计算偏移尺寸

if ($scaleWidth < $scaleHeight) {

$size['offsetX'] = 0;

$size['offsetY'] = round(($height - $size['newHeight'])/2);

} else {

$size['offsetY'] = 0;

$size['offsetX'] = round(($width - $size['newWidth'])/2);

}

return $size;

}

}

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。互联网+时代,时刻要保持学习,携手千锋PHP,Dream It Possible。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

  • class Image { //保存目录 protected $savePath; //随机名字 protecte...
    php红薯阅读 412评论 0 0
  • class Upload { //保存路径 protected $savePath = './'; //日期目录 ...
    php红薯阅读 554评论 0 0
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,327评论 0 17
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,703评论 2 17
  • 新余学院,这个圆了我大学梦的地方,2016年7月,接到新余学院录取通知书的我显然是兴奋的,而在这股兴奋劲褪去之后,...
    lt1997阅读 229评论 1 2