设为首页 收藏本站
查看: 551|回复: 0

[经验分享] PHP图片水印缩放类实现

[复制链接]

尚未签到

发表于 2017-4-5 06:34:09 | 显示全部楼层 |阅读模式
  


<?php
class Image{
private $path;

function __construct($path='./'){
$this->path=rtrim($path,'/').'/';
}
//水印函数,参数:背景图,水印图,位置,前缀,TMD透明度
public function water($b,$l,$pos=9,$prefix='wa_',$tmd=100){
//拼接路径
$b=$this->path.$b;
$l=$this->path.$l;
//判断路径
if(file_exists($b)&&file_exists($l)){
//获取信息
$binfo=self::getImageInfo($b);
$linfo=self::getImageInfo($l);
//判断大小
if(!self::checkSize($binfo,$linfo)){
exit('背景图片大小小于水印图片大小');
}
//取得位置
$position=self::getPosition($binfo,$linfo,$pos);
//打开资源
$bRes=self::openImg($b,$binfo['type']);
$lRes=self::openImg($l,$linfo['type']);
//合成
$newRes=self::mergeImg($bRes,$lRes,$linfo,$position,$tmd);
//保存
if($name=self::saveImg($newRes,$this->path,$prefix,$binfo)){
return $name;
}
}else{
echo '路径不存在';
exit;
}
}

//thumb 等比缩放方法
public function thumb($img,$width,$height,$prefix='th_'){
$img=$this->path.$img;
if(!file_exists($img)){
exit('楼下有美女,提示你图片路径不存在');
}
$info=self::getImageInfo($img);
$newSize=self::getNewSize($width,$height,$info);
$res=self::openImg($img,$info['type']);
$newRes=self::kidOfImage($res,$newSize,$info);
if($name=self::saveImg($newRes,$this->path,$prefix,$info)){
return $name;
}

}
static private function saveImg($res,$path,$prefix,$binfo){
$name=$path.$prefix.$binfo['name'];
switch($binfo['type']){
case 'image/jpeg':
case 'image/jpg':
case 'image/pjpeg':
imagejpeg($res,$name);
break;
case 'image/png':
case 'image/x-png':
imagepng($res,$name);
break;
case 'image/gif':
imagegif($res,$name);
break;
case 'image/wbmp':
imagewbmp($res,$name);
break;
default:
echo '不支持这个图片类型解析';
break;
}
return $name;
}
static private function mergeImg($b,$l,$linfo,$p,$tmd){
imagecopymerge($b,$l,$p['x'],$p['y'],0,0,$linfo['width'],$linfo['height'],$tmd);
return $b;
}
static private function openImg($path,$type){
switch($type){
case 'image/jpeg':
case 'image/jpg':
case 'image/pjpeg':
$res=imagecreatefromjpeg($path);
break;
case 'image/gif':
$res=imagecreatefromgif($path);
break;
case 'image/wbmp':
$res=imagecreatefromwbmp($path);
break;
case 'image/png':
case 'image/x-png':
$res=imagecreatefrompng($path);
break;
default:
echo '图片类型不支持';
exit;
}
return $res;
}
static private function getPosition($b,$l,$pos){
switch($pos){
case 1:
$x=0;
$y=0;
break;
case 2:
$x=floor(($b['width']-$l['width'])/2);
$y=0;
break;
case 3:
$x=$b['width']-$l['width'];
$y=0;
break;
case 4:
$x=0;
$y=ceil(($b['height']-$l['height'])/2);
break;
case 5:
$x=floor(($b['width']-$l['width'])/2);
$y=ceil(($b['height']-$l['height'])/2);
break;
case 6:
$x=$b['width']-$l['width'];
$y=ceil(($b['height']-$l['height'])/2);
break;
case 7:
$x=0;
$y=$b['height']-$l['height'];
break;
case 8:
$x=floor(($b['width']-$l['width'])/2);
$y=$b['height']-$l['height'];
break;
case 9:
$x=$b['width']-$l['width'];
$y=$b['height']-$l['height'];
break;
default:
$x=mt_rand(0,$b['width']-$l['width']);
$y=mt_rand(0,$b['height']-$l['height']);
break;
}
return array('x'=>$x,'y'=>$y);
}
static private function checkSize($b,$l){
if($b['width']<$l['width']||$b['height']<$l['height']){
return false;
}else{
return true;
}
}
static private function getImageInfo($path){
$info=getimagesize($path);
$data['width']=$info[0];
$data['height']=$info[1];
$data['type']=$info['mime'];
$data['name']=basename($path);
return $data;
}
static private function kidOfImage($srcImg,$size, $imgInfo){
$newImg = imagecreatetruecolor($size["width"], $size["height"]);
$otsc = imagecolortransparent($srcImg);
if( $otsc >= 0 && $otsc < imagecolorstotal($srcImg)) {
$transparentcolor = imagecolorsforindex( $srcImg, $otsc );
$newtransparentcolor = imagecolorallocate(
$newImg,
$transparentcolor['red'],
$transparentcolor['green'],
$transparentcolor['blue']
);
imagefill( $newImg, 0, 0, $newtransparentcolor );
imagecolortransparent( $newImg, $newtransparentcolor );
}

imagecopyresized( $newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"] );
imagedestroy($srcImg);
return $newImg;
}
static private function getNewSize($width, $height,$imgInfo){
$size["width"]=$imgInfo["width"];          //将原图片的宽度给数组中的$size["width"]
$size["height"]=$imgInfo["height"];        //将原图片的高度给数组中的$size["height"]
if($width < $imgInfo["width"]){
$size["width"]=$width;             //缩放的宽度如果比原图小才重新设置宽度
}
if($width < $imgInfo["height"]){
$size["height"]=$height;            //缩放的高度如果比原图小才重新设置高度
}
if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){
$size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);
}else{
$size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);
}
return $size;
}

}
?>

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-360241-1-1.html 上篇帖子: 国内三大PHP主流CMS横向比较 下篇帖子: php header Content-Type类型小结
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表