php实现图⽚加密解密,⽀持设置密码(两年后版本!)
基于php的图⽚加密解密类,⽀持设置密码,情侣们的福⾳!
先看效果
上代码
<?php
/**
* by hello
* 84587470
*
* php ⽂件加密类,⽀持设置密码,图⽚,⽂件都可以!情侣们的福⾳
*
*/
$from = 'C:\Users\Administrator\Desktop\t\test.png';
$to = 'C:\Users\Administrator\Desktop\t\\';
/
/加密
php实例代码解密
encrpty::en($from , $to , 'xxxx');
//解密
encrpty::de('C:\Users\Administrator\Desktop\t\00f8fbb037778e31455211b4bdcb0874' , $to , 'xxxx');
class encrpty
{
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;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。