moonsflyer 4 месяцев назад
Родитель
Сommit
63c74e1f97
1 измененных файлов с 40 добавлено и 2 удалено
  1. 40 2
      app/common/service/ImageCompressService.php

+ 40 - 2
app/common/service/ImageCompressService.php

@@ -123,7 +123,44 @@ class ImageCompressService
                     $image = imagecreatefromjpeg($path);
                     break;
                 case IMAGETYPE_PNG:
-                    $image = imagecreatefrompng($path);
+                    // PNG特殊处理
+                    outFileLog('开始处理PNG图片: ' . $path, 'upload_img', 'png_process_start');
+
+                    // 检查文件大小
+                    $fileSize = filesize($path);
+                    outFileLog('PNG文件大小: ' . $fileSize . ' bytes', 'upload_img', 'png_file_size');
+
+                    // 检查内存限制
+                    $memoryLimit = ini_get('memory_limit');
+                    $currentMemory = memory_get_usage(true);
+                    outFileLog('内存限制: ' . $memoryLimit . ', 当前使用: ' . $currentMemory, 'upload_img', 'memory_info');
+
+                    // 尝试创建PNG图片资源
+                    $image = @imagecreatefrompng($path);
+
+//                    $image = imagecreatefrompng($path);
+                    if ($image === false) {
+                        // 获取详细错误信息
+                        $error = error_get_last();
+                        outFileLog('PNG创建失败,错误信息: ' . json_encode($error), 'upload_img', 'png_create_error');
+                        // 尝试验证PNG文件头
+                        $handle = fopen($path, 'rb');
+                        $header = fread($handle, 8);
+                        fclose($handle);
+                        $pngSignature = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
+
+                        if ($header !== $pngSignature) {
+                            throw new Exception('PNG文件头不正确,文件可能损坏');
+                        }
+
+                        // 尝试使用getimagesize验证
+                        $imageInfo = @getimagesize($path);
+                        if ($imageInfo === false) {
+                            throw new Exception('PNG文件无法被识别,可能已损坏');
+                        }
+
+                        outFileLog('PNG图片信息: ' . json_encode($imageInfo), 'upload_img', 'png_image_info');
+                    }
                     break;
                 case IMAGETYPE_GIF:
                     $image = imagecreatefromgif($path);
@@ -141,10 +178,11 @@ class ImageCompressService
             if (!$image) {
                 throw new Exception('创建图片资源失败,可能是内存不足或文件损坏');
             }
-            outFileLog($image,'upload_img','createImageFromType-$image');
+            outFileLog($image ? 'success' : 'failed', 'upload_img', 'createImageFromType-result');
             return $image;
 
         } catch (Exception $e) {
+            outFileLog('图片处理异常: ' . $e->getMessage(), 'upload_img', 'createImageFromType-exception');
             throw new Exception('图片处理失败: ' . $e->getMessage());
         }
     }