一個簡單的圖片加解密函數(shù)
使用client跑,不要使用瀏覽器跑
<?php /** * Created by hello. * User: qq 845875470 * Date: 2016/4/2 * Time: 11:21 */ $notice = <<<A 為了穩(wěn)定性,必須在客戶端跑 格式 :php path=D:/xxx/uuu type=en is_copy=1 salt=xxx 參數(shù)使用空格分開 path -- 路徑 必須寫 type -- en加密, de為解密 必須寫 is_copy -- 1為復(fù)制,0為轉(zhuǎn)移, 不寫默認(rèn)為轉(zhuǎn)移 salt -- 加密鑰匙 加密用什么,解密就用什么 不寫默認(rèn)為salt A; //如果不是客戶端 if(PHP_SAPI != 'cli') {echo $notice;die;} //獲取參數(shù) $arr = parse_parameter($argv); //如果路徑?jīng)]設(shè)置 if(!isset($arr['path']) || !isset($arr['type'])) {echo $notice;die;} //如果is_dir沒設(shè)置 if(!isset($arr['is_copy'])) {$arr['is_copy'] = '';} //如果salt沒設(shè)置 if(!isset($arr['salt'])) {$arr['salt'] = '';} //type為en就加密 if($arr['type'] == "en") img_enconde($arr['path'], $arr['is_copy'], $arr['salt']); //type為de就解密 if($arr['type'] == "de") img_deconde($arr['path'], $arr['is_copy'], $arr['salt']); function parse_parameter($argv) { $arr = array(); //獲取參數(shù) for($len=count($argv)-1; $len--; ) { list($key, $val) = explode('=', $argv[$len]); $arr[$key] = $val; } return $arr; } //圖片加密函數(shù) //路徑文件夾 //是否為復(fù)制(默認(rèn)不復(fù)制) //鹽(默認(rèn)為salt) function img_enconde($path, $is_copy = 0, $salt = 'salt') { $time1 = microtime(1); $handle = opendir($path); if(!$salt) $salt = 'salt'; if($handle) { echo "路徑:" . $path . "\r\n\r\n"; //在指定文件夾下創(chuàng)建臨時文件夾 $temp_dir = $path . '\\' . 'temp'; @mkdir($temp_dir, 0777, 1); while ($file = readdir($handle)) { $time2 = microtime(1); //構(gòu)造當(dāng)前文件絕對地址 $dir_path = $path . '\\' . $file; //獲取文件后綴 $suffix = strrchr($file, '.'); //圖片后綴 $fix = array('.jpg', '.gif', '.bmp', '.png', '.jpeg', '.JPG', '.GIF', '.BMP', '.PNG', 'JPEG'); if(is_file($dir_path) && in_array($suffix, $fix)) { //打開當(dāng)前文件 $fh = fopen($dir_path, 'r'); //打開文件為流 $stream = fread($fh, filesize($dir_path)); //輸出 file_put_contents($temp_dir . '\\' . uniqid('',1), $file . '!' . $salt . '@' . $stream); //關(guān)閉句柄 fclose($fh); //是否為復(fù)制 //1為復(fù)制,0為刪除(默認(rèn)) if(!$is_copy) { echo "加密并刪除 : " . $dir_path . "\r\n"; @unlink($dir_path); } else { echo "加密 : " . $dir_path . "\r\n"; } $time3 = microtime(1); echo "此圖用時 ", ($time3 - $time2), " S\r\n", "已經(jīng)用時 ", ($time3 - $time1), " S\r\n\r\n"; } } echo "加密完成\r\n"; } else { echo "path invalid "; return false; } } //圖片解密函數(shù) //路徑文件夾 //是否為復(fù)制(默認(rèn)不復(fù)制) //鹽(默認(rèn)為salt)加密寫什么,這里就寫什么 function img_deconde($path, $is_copy = 0, $salt = '') { $time1 = microtime(1); $handle = opendir($path); if($handle) { echo "路徑:" . $path . "\r\n\r\n"; if(!$salt) $salt = 'salt'; //在指定文件夾下創(chuàng)建臨時文件夾 $temp_dir = $path . '\\' . 'temp'; @mkdir($temp_dir, 0777, 1); //核心正則 $reg = "#^(.+?[jpgifbmne]{3,4})!(" . $salt . ")@#im"; $res = array(); $count = 0; while ($file = readdir($handle)) { $time2 = microtime(1); //構(gòu)造當(dāng)前文件絕對地址 $file_path = $path . '\\' . $file; if(is_file($file_path)) { //文件句柄 $hf = fopen($file_path, 'r'); //返回流 $stream = fread($hf, filesize($file_path)); fclose($hf); //匹配加的密碼 if(preg_match_all($reg, $stream, $res)) { $count++; //清空鹽 $stream = str_replace($res[0][0], '', $stream); //輸出文件 file_put_contents($temp_dir . '\\' . $res[1][0], $stream); //是否為復(fù)制 //1為復(fù)制,0為刪除(默認(rèn)) if(!$is_copy) { echo "成功解密刪除 : " . $temp_dir . '\\' . $res[1][0] . "\r\n"; @unlink($file_path); } else { echo "解密 : " . $temp_dir . '\\' . $res[1][0] . "\r\n"; } } $time3 = microtime(1); echo "此圖用時 ", ($time3 - $time2), " S\r\n", "已經(jīng)用時 ", ($time3 - $time1), " S\r\n\r\n"; } } if(!$count) { echo "沒有有效的加密文件\r\n"; return false; } echo "解密完成\r\n"; } else { echo "path invalid "; return false; } } ?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Grass Wonder Build Guide | Uma Musume Pretty Derby
1 months ago
By Jack chen
Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them
4 weeks ago
By DDD
Uma Musume Pretty Derby Banner Schedule (July 2025)
1 months ago
By Jack chen
RimWorld Odyssey Temperature Guide for Ships and Gravtech
3 weeks ago
By Jack chen
Windows Security is blank or not showing options
1 months ago
By 下次還敢

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)