|
|
@@ -56,8 +56,9 @@ class ImageToImageJob{
|
|
|
$data["file_name"],
|
|
|
$data["outputDir"],
|
|
|
$row["new_image_url"],
|
|
|
- 1024,
|
|
|
- 2048
|
|
|
+ $row["img_name"],
|
|
|
+ 679,
|
|
|
+ 862
|
|
|
);
|
|
|
|
|
|
$resultText = ($result === true || $result === 1 || $result === '成功') ? '成功' : '失败或无返回';
|
|
|
@@ -123,8 +124,7 @@ class ImageToImageJob{
|
|
|
echo "ImageJob failed: " . json_encode($data);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public function ImageToImage($fileName, $outputDirRaw, $new_image_url, $width, $height)
|
|
|
+ public function ImageToImage($fileName, $outputDirRaw, $new_image_url,$img_name, $width, $height)
|
|
|
{
|
|
|
// 统一路径分隔符
|
|
|
$rootPath = str_replace('\\', '/', ROOT_PATH);
|
|
|
@@ -138,11 +138,10 @@ class ImageToImageJob{
|
|
|
// 完整基本路径,如:ROOT/public/uploads/operate/ai/dall-e/hua/2025-06-16/
|
|
|
$fullBaseDir = $outputDir . $dateDir;
|
|
|
|
|
|
- // 创建输出目录,包括原图目录、1024x2048目录、自定义尺寸目录
|
|
|
- foreach ([$fullBaseDir, $fullBaseDir . '1024x2048/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
|
|
|
- if (!is_dir($dir)) {
|
|
|
- mkdir($dir, 0755, true);
|
|
|
- }
|
|
|
+ // 只创建 img_679x862 目录
|
|
|
+ $saveDir = $fullBaseDir . 'new_679x862/';
|
|
|
+ if (!is_dir($saveDir)) {
|
|
|
+ mkdir($saveDir, 0755, true);
|
|
|
}
|
|
|
|
|
|
// 从数据库中查询原图记录
|
|
|
@@ -170,18 +169,55 @@ class ImageToImageJob{
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
+ // 保存图片路径
|
|
|
+ $img_name = mb_substr(preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name), 0, 30);
|
|
|
+ $filename = $img_name . '.png';
|
|
|
+ $path = $saveDir . $filename;
|
|
|
+
|
|
|
// 解码图像 base64 数据
|
|
|
- $originalBaseName = pathinfo($new_image_url, PATHINFO_FILENAME);
|
|
|
- $finalFileName = $originalBaseName . '.png';
|
|
|
+ $imgData = base64_decode($res['data']['url']);
|
|
|
+
|
|
|
+ // 解析图像内容
|
|
|
+ try {
|
|
|
+ $im = \imagecreatefromstring($imgData);
|
|
|
+ if (!$im) {
|
|
|
+ file_put_contents('/tmp/corrupted.png', $imgData);
|
|
|
+ throw new \Exception("❌ 图像无法解析,写入 /tmp/corrupted.png");
|
|
|
+ }
|
|
|
+ } catch (\Throwable $e) {
|
|
|
+ file_put_contents('/tmp/corrupted.png', $imgData);
|
|
|
+ throw new \Exception("❌ 图像处理异常:" . $e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 裁剪
|
|
|
+ $srcW = imagesx($im);
|
|
|
+ $srcH = imagesy($im);
|
|
|
+ $srcRatio = $srcW / $srcH;
|
|
|
+ $dstRatio = $width / $height;
|
|
|
+
|
|
|
+ if ($srcRatio > $dstRatio) {
|
|
|
+ $cropW = intval($srcH * $dstRatio);
|
|
|
+ $cropH = $srcH;
|
|
|
+ $srcX = intval(($srcW - $cropW) / 2);
|
|
|
+ $srcY = 0;
|
|
|
+ } else {
|
|
|
+ $cropW = $srcW;
|
|
|
+ $cropH = intval($srcW / $dstRatio);
|
|
|
+ $srcX = 0;
|
|
|
+ $srcY = intval(($srcH - $cropH) / 2);
|
|
|
+ }
|
|
|
|
|
|
- // 保存到 1024x2048 子目录
|
|
|
- $targetDir = $fullBaseDir . '1024x2048/';
|
|
|
- $savePath = $targetDir . $finalFileName;
|
|
|
- file_put_contents($savePath, base64_decode($res['data']['url']));
|
|
|
+ $dstImg = imagecreatetruecolor($width, $height);
|
|
|
+ imagecopyresampled($dstImg, $im, 0, 0, $srcX, $srcY, $width, $height, $cropW, $cropH);
|
|
|
|
|
|
- // ✅ 修正:数据库中记录相对路径一致
|
|
|
+ // 保存裁剪图
|
|
|
+ imagepng($dstImg, $path);
|
|
|
+ imagedestroy($im);
|
|
|
+ imagedestroy($dstImg);
|
|
|
+
|
|
|
+ // 更新数据库记录
|
|
|
Db::name('text_to_image')->where('id', $record['id'])->update([
|
|
|
- 'imgtoimg_url' => $outputDirRaw . '/' . $dateDir . '1024x2048/' . $finalFileName,
|
|
|
+ 'imgtoimg_url' => str_replace($rootPath . 'public/', '', $path),
|
|
|
'status_name' => '图生图',
|
|
|
'error_msg' => '',
|
|
|
'update_time' => date('Y-m-d H:i:s')
|
|
|
@@ -190,4 +226,71 @@ class ImageToImageJob{
|
|
|
return '成功';
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+// public function ImageToImage($fileName, $outputDirRaw, $new_image_url, $width, $height)
|
|
|
+// {
|
|
|
+// // 统一路径分隔符
|
|
|
+// $rootPath = str_replace('\\', '/', ROOT_PATH);
|
|
|
+//
|
|
|
+// // 输出目录,如:ROOT/public/uploads/operate/ai/dall-e/hua/
|
|
|
+// $outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
|
|
|
+//
|
|
|
+// // 当前日期目录,如:2025-06-16/
|
|
|
+// $dateDir = date('Y-m-d') . '/';
|
|
|
+//
|
|
|
+// // 完整基本路径,如:ROOT/public/uploads/operate/ai/dall-e/hua/2025-06-16/
|
|
|
+// $fullBaseDir = $outputDir . $dateDir;
|
|
|
+//
|
|
|
+// // 创建输出目录,包括原图目录、1024x2048目录、自定义尺寸目录
|
|
|
+// foreach ([$fullBaseDir, $fullBaseDir . '1024x2048/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
|
|
|
+// if (!is_dir($dir)) {
|
|
|
+// mkdir($dir, 0755, true);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 从数据库中查询原图记录
|
|
|
+// $record = Db::name('text_to_image')
|
|
|
+// ->where('old_image_url', 'like', "%{$fileName}")
|
|
|
+// ->order('id desc')
|
|
|
+// ->find();
|
|
|
+//
|
|
|
+// if (!$record) {
|
|
|
+// return json([
|
|
|
+// 'code' => 1,
|
|
|
+// 'msg' => '没有找到匹配的图像记录'
|
|
|
+// ]);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 调用图生图 API
|
|
|
+// $ai = new AIGatewayService();
|
|
|
+// $res = $ai->imgtoimgGptApi('', $new_image_url);
|
|
|
+//
|
|
|
+// // 检查返回结果
|
|
|
+// if (!isset($res['code']) || $res['code'] !== 0) {
|
|
|
+// return json([
|
|
|
+// 'code' => 1,
|
|
|
+// 'msg' => $res['msg'] ?? '图像生成失败'
|
|
|
+// ]);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 解码图像 base64 数据
|
|
|
+// $originalBaseName = pathinfo($new_image_url, PATHINFO_FILENAME);
|
|
|
+// $finalFileName = $originalBaseName . '.png';
|
|
|
+//
|
|
|
+// // 保存到 1024x2048 子目录
|
|
|
+// $targetDir = $fullBaseDir . '1024x2048/';
|
|
|
+// $savePath = $targetDir . $finalFileName;
|
|
|
+// file_put_contents($savePath, base64_decode($res['data']['url']));
|
|
|
+//
|
|
|
+// // ✅ 修正:数据库中记录相对路径一致
|
|
|
+// Db::name('text_to_image')->where('id', $record['id'])->update([
|
|
|
+// 'imgtoimg_url' => $outputDirRaw . '/' . $dateDir . '1024x2048/' . $finalFileName,
|
|
|
+// 'status_name' => '图生图',
|
|
|
+// 'error_msg' => '',
|
|
|
+// 'update_time' => date('Y-m-d H:i:s')
|
|
|
+// ]);
|
|
|
+//
|
|
|
+// return '成功';
|
|
|
+// }
|
|
|
+
|
|
|
}
|