基於php的圖片加密解密類,支持設置密碼,情侶們的福音!php
先看效果加密
上代碼spa
<?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'); 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; } }