debug
This commit is contained in:
133
code/extend/processor/ImageProcessor.php
Normal file
133
code/extend/processor/ImageProcessor.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace processor;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use think\Exception;
|
||||
|
||||
class ImageProcessor
|
||||
{
|
||||
private static $instance = null;
|
||||
private $client;
|
||||
private $key;
|
||||
|
||||
private function __construct($key)
|
||||
{
|
||||
$this->client = new Client([
|
||||
'verify' => false // 忽略证书检测
|
||||
]);
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
public static function getInstance($key = 0x88)
|
||||
{
|
||||
if (self::$instance == null) {
|
||||
self::$instance = new ImageProcessor($key = 0x88);
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function downloadAndEncryptImage($imageUrl, $directory)
|
||||
{
|
||||
try {
|
||||
// 下载图片
|
||||
$response = $this->client->get($imageUrl);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception("Failed to download image: $imageUrl");
|
||||
}
|
||||
$imageData = $response->getBody()->getContents();
|
||||
|
||||
// 加密图片
|
||||
$encryptedData = $this->encrypt($imageData);
|
||||
|
||||
// 生成目录路径
|
||||
$relativePath = $this->generatePath($directory);
|
||||
|
||||
// 保存加密后的图片
|
||||
file_put_contents(public_path() . $relativePath, $encryptedData);
|
||||
|
||||
return '/' . $relativePath;
|
||||
} catch (RequestException $e) {
|
||||
throw new Exception("Request Error: " . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function downloadImage( $intXiaoShuoSourceId,$imageUrl, $directory)
|
||||
{
|
||||
try {
|
||||
// 下载图片
|
||||
$response = $this->client->get($imageUrl);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception("Failed to download image: $imageUrl");
|
||||
}
|
||||
$imageData = $response->getBody()->getContents();
|
||||
|
||||
// 加密图片
|
||||
//$encryptedData = $this->encrypt($imageData);
|
||||
|
||||
// 生成目录路径
|
||||
$relativePath = $this->generateImgPath($directory,$intXiaoShuoSourceId);
|
||||
|
||||
// 保存图片
|
||||
file_put_contents(public_path() . $relativePath, $imageData);
|
||||
|
||||
return '/' . $relativePath;
|
||||
} catch (RequestException $e) {
|
||||
throw new Exception("Request Error: " . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function encrypt($data)
|
||||
{
|
||||
$bytes = array_values(unpack('C*', $data));
|
||||
$len = count($bytes);
|
||||
|
||||
for ($i = 0; $i < $len; $i++) {
|
||||
$bytes[$i] ^= $this->key;
|
||||
}
|
||||
|
||||
return pack('C*', ...$bytes);
|
||||
}
|
||||
|
||||
private function generateImgPath($directory,$intXiaoShuoSourceId)
|
||||
{
|
||||
$relativePath = $directory . '/pic/'.$intXiaoShuoSourceId;
|
||||
$basePath = public_path() . $relativePath;
|
||||
if (!is_dir($basePath)) {
|
||||
mkdir($basePath, 0777, true);
|
||||
}
|
||||
|
||||
$filePath = $relativePath . '.jpg'; ;
|
||||
return $filePath;
|
||||
}
|
||||
|
||||
private function generatePath($directory)
|
||||
{
|
||||
$datePath = date('Y/m/d');
|
||||
$hash = md5(uniqid(rand(), true));
|
||||
$hashPath = substr($hash, 0, 2) . '/' . substr($hash, 2, 2);
|
||||
|
||||
$relativePath = $directory . '/' . $datePath . '/' . $hashPath;
|
||||
$basePath = public_path() . $relativePath;
|
||||
if (!is_dir($basePath)) {
|
||||
mkdir($basePath, 0777, true);
|
||||
}
|
||||
|
||||
$filePath = $relativePath . '/' . uniqid() . '.jpg';
|
||||
return $filePath;
|
||||
}
|
||||
}
|
||||
|
||||
// // 示例用法
|
||||
// try {
|
||||
// $imageProcessor = ImageProcessor::getInstance();
|
||||
// $savedPath = $imageProcessor->downloadAndEncryptImage('https://example.com/path/to/image.jpg', 'abc');
|
||||
// echo "图片加密并保存到: " . $savedPath;
|
||||
// } catch (Exception $e) {
|
||||
// echo "错误: " . $e->getMessage();
|
||||
// }
|
||||
Reference in New Issue
Block a user