php如何加密图片,php实现图片加密解密,支持设置密码 (两年后版本!)

* by hello

* 84587470

*

* php 文件加密类,支持设置密码,图片,文件都可以!情侣们的福音!!!

**/

$from = 'C:\Users\Administrator\Desktop\t\test.png';$to = 'C:\Users\Administrator\Desktop\t\\';//加密

encrpty::en($from , $to , 'xxxx');//解密

encrpty::de('C:\Users\Administrator\Desktop\t\00f8fbb037778e31455211b4bdcb0874' , $to , 'xxxx');classencrpty

{const DS =DIRECTORY_SEPARATOR;const FILE = 1;const DIRECTORY = 2;const ALL = 3;/**

* 文件加密

*

* @param $from 源

* @param $to 保存位置

* @param string $slat 密码

*

* @return array*/

public static function en($from , $to , $slat = 'xxx')

{$data =['sign' => 1 ,

'msg' => '' ,];try{$imgObj = new \SplFileInfo($from);$stram = implode('' ,['@@_____' ,

$slat ,

'@@_____' ,

$imgObj->getBasename() ,

'@@_____' ,]). (file_get_contents($imgObj->getRealPath()));$stram = gzcompress($stram);file_put_contents(static::replaceToSysSeparator($to) . md5($imgObj->getRealPath()) , ($stram));

}catch (\Exception $exception)

{$data['sign'] = 0;$data['data'] = $exception->getMessage();

}return $data;

}/**

* 解密

*

* @param $from 源

* @param $to 保存位置

* @param string $slat 密码

*

* @return array*/

public static function de($from , $to , $slat = 'xxx')

{$data =['sign' => 1 ,

'msg' => '' ,];try{$imgObj = new \SplFileInfo($from);$stram = file_get_contents($imgObj->getRealPath());$stram = gzuncompress($stram);$reg = implode('' ,['#' ,

'^@@_____' ,

preg_quote($slat , '#') ,

'@@_____' ,

'(.*?)' ,

'@@_____' ,

'#i' ,]);preg_match($reg , $stram , $res);if(isset($res[1]))

{

self::mkdir_($to);$result = preg_replace($reg , '' , $stram);file_put_contents(static::endDS($to) . $res[1] , $result);

}else{$data['sign'] = 0;$data['data'] = '解密出错';

}

}catch (\Exception $exception)

{$data['sign'] = 0;$data['data'] = $exception->getMessage();

}return $data;

}/**

* 自动为路径后面加DIRECTORY_SEPARATORY

*

* @param string $path 文件夹路径

*

* @return string*/

public static function endDS($path)

{return rtrim(rtrim(static::replaceToSysSeparator($path) , '/') , '\\') . static::DS;

}/**

* @param $path

*

* @return string*/

public static function replaceToSysSeparator($path)

{return strtr($path ,['\\' => static::DS ,

'/' => static::DS ,]);

}/**

* @param $path

*

* @return string*/

public static function replaceToUrlSeparator($path)

{return strtr($path ,['\\' => '/' ,]);

}/**

* 格式化字节大小

*

* @param number $size 字节数

* @param string |int $de

*

* @return string 格式化后的带单位的大小*/

public static function byteFormat($size , $de = 2)

{$a = array("B" ,

"KB" ,

"MB" ,

"GB" ,

"TB" ,

"PB" ,);$pos = 0;while ($size >= 1024)

{$size /= 1024;$pos++;

}return round($size , $de) . " " . $a[$pos];

}/**

* 创建文件夹

*

* @param $path

* @param int $mode

*

* @return bool*/

public static function mkdir_($path , $mode = 0777)

{return !is_dir(($path)) ? mkdir(($path) , $mode , 1) : @chmod($path , $mode);

}/**

* 列出文件夹里文件信息

*

* @param $path

* @param callable|null $callback

* @param int $flag

*

* @return array*/

public static function listDir($path , callable $callback = null , $flag = self::ALL)

{$files =[];if(is_dir($path) && is_readable($path))

{try{$directory = new \FilesystemIterator ($path);$filter = new \CallbackFilterIterator ($directory , function($current , $key , $iterator) use ($flag) {switch ($flag)

{case static::FILE:

return $current->isFile();case static::DIRECTORY:

return $current->isDir();default:

return true;

}

});foreach ($filter as $info)

{if(is_callable($callback))

{$files[] = call_user_func_array($callback ,[$info ,]);

}else{$files[] = $info;

}

}

}catch (\Exception $e)

{$files =[];

}

}return $files;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值