根据文件头进制特征识别文件类型,识别为图片进行OCR识别,懂得都懂,禁止用于违法行为。
<?php
// 16进制特征码
$checkMap = [
".jpeg" => hex2bin("FFD8FF"),
".png" => hex2bin("89504E47"),
".gif" => hex2bin("47494638"),
".tif" => hex2bin("49492A00"),
".bmp" => hex2bin("424D")
];
// 传递微信加密文件
$file = './be27b0c8911ffd7294b7febc16a9cb91_t.dat';
if (!file_exists($file)) {
throw new Exception('文件' . $file . '不存在');
}
// 读取文件内容
$content = file_get_contents($file);
if (!$content) {
throw new Exception('未获取到文件内容');
}
// 读取文件前10个字节内容作为特征码
$bytesToRead = 10;
$feature = substr($content, 0, 10);
if (!$feature || strlen($feature) != 10) {
throw new Exception('未获取到文件特征码');
}
// 检查特征
$feature_array = str_split($feature, 1);
$fileRes = checkImage($feature_array);
if (empty($fileRes['0'])) {
throw new Exception('不支持的文件类型');
}
// 生成新的文件
$newFile = 'test' . $fileRes['0'];
// 将文件内容转换
$newChar = '';
for ($i = 0; $i < strlen($content); $i++) {
$newChar .= $content[$i] ^ $fileRes['1'];
}
file_put_contents($newFile, $newChar);
// 检查特征
function checkImage($in_feature)
{
global $checkMap;
foreach ($checkMap as $key => $check_feature) {
$isConform = checkConformRule($check_feature, $in_feature);
if ($isConform) {
return [$key, $isConform];
}
}
return [null, null];
}
// 检查规则
function checkConformRule($check_feature, $in_feature)
{
$initDecodeByte = $check_feature[0] ^ $in_feature[0];
for ($i = 0; $i < strlen($check_feature); $i++) {
$b = $check_feature[$i] ^ $in_feature[$i];
if ($b !== $initDecodeByte) {
return false;
}
}
return $initDecodeByte;
}(1).学习目标: 了解常见信号类型(百度PHP支持的信号类型),(2).相关函数学习: (2.1).pcntl_signal函数用于设置一个信号管理器接收进程信号,参数1:信号类型,参数2:回调函数,用于在接收到参数1类型的信...
【一】.迭代器迭代是指反复执行一个过程,每执行一次叫做一次迭代。比如下面的代码就叫做迭代:1. <?php 2. $data = ['1', '2', &...
最近在编写windows php多线程的东西,从官网下载了PHP的线程安全版,尝试开启curl扩展extension=php_curl.dllphp -m 却提示 PHP Startup: Unable to load dynamic library 'C:\php\ext\php_curl...
将jsonp转为PHP数组和对象。/** * jsonp转数组|Jsonp转json * @param string $jsonp jsonp字符串 * @param bool $as...
概念请参考w3school文章: redis watch ,redis exec (看完基本秒懂)(1)基本事务://连接本地的 Redis 服务 $redis = new Redis(); $redis->con...
elasticsearch的操作都是基于http协议的,已经有现成的php类库,composer安装即可。{ "require": { &...