PHP图像魔术师在上传多个大图像时崩溃


PHP Image Magician crashing when upload multiple big images

我使用PHP Image Magician为图像创建缩略图和多上传表单。当我尝试上传10张大小约30kb的图像时,它们会被上传,但如果我尝试上传2张大小约6-7mb的图像,它只会将第一张图像保存到文件夹和数据库中,然后页面崩溃。没有将任何内容保存到拇指文件夹中。

如果我只尝试使用一个图像,它会同时保存到文件夹(普通+拇指)和数据库中。一点问题都没有。

我在php.ini中也增加了

max_execution_time: 600;
max_input_time: 600;
memory_limit: 96;
upload_max_filesize: 100;
post_max_size: 100;

问题出在哪里。如果需要,我也可以发布一些来源。

编辑:upload.php

if (isset($_POST["sub2"])) {
// include resized library
require_once('php-image-magician/php_image_magician.php');
$msg = "";
$valid_image_check = array("image/gif", "image/jpeg", "image/jpg", "image/png", "image/bmp");
if (count($_FILES["user_files"]) > 0) {
$folderName = "../../images/gallery/";
$thumbFolder = "../../gallery/thumb/";
$sql = "INSERT INTO images (image_name, image_size, image_type, image_album, image_path, image_thumb) VALUES (:img, :size, :type, :album, :path, :thumb)";
$stmt = $pdo->prepare($sql);
for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) {
  if ($_FILES["user_files"]["name"][$i] <> "") {
    $image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_FILES["user_files"]["tmp_name"][$i])));
    // if valid image type then upload
    if (in_array($image_mime, $valid_image_check)) {
      $ext = explode("/", strtolower($image_mime));
      $ext = strtolower(end($ext));
      $filename = rand(10000, 990000) . '_' . time() . '.' . $ext;
      $filepath = $folderName . $filename;
      $thumbpath = $thumbFolder . $filename;
  $fileSize = $_FILES['user_files']['size'][$i];
  $fileType = $_FILES['user_files']['type'][$i];
  $album = $_POST['image_album'];  

      if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i], $filepath)) {
        $emsg .= "Error while uploading - <strong>" . $_FILES["user_files"]["name"][$i] . "</strong>. Please, try again. <br>";
        $counter++;
      } else {
        $smsg .= "Image <strong>" . $_FILES["user_files"]["name"][$i] . "</strong> is added successfully . <br>";
        /*             * ****** insert into database starts ******** */
                    $magicianObj = new imageLib($filepath);
                    $magicianObj->resizeImage(500, 500);
                    $magicianObj->saveImage($folderName . 'thumb/' . $filename, 500);            
        try {
          $stmt->bindValue(":img", $filename);
          $stmt->bindValue(":size", $fileSize);
          $stmt->bindValue(":type", $fileType);  
          $stmt->bindValue(":album", $album);
          $stmt->bindValue(":path", $filepath);
          $stmt->bindValue(":thumb", $thumbpath);
          $stmt->execute();
          $result = $stmt->rowCount();
          if ($result > 0) {
            // file uplaoded successfully.
          } else {
            // failed to insert into database.
          }
        } catch (Exception $ex) {
          $emsg .= "<strong>" . $ex->getMessage() . "</strong>. <br>";
        }
        /*             * ****** insert into database ends ******** */
      }
    } else {
      $emsg .= "This file <strong>" . $_FILES["user_files"]["name"][$i] . "</strong> is not an image. <br>";
    }
  }
}

$msg .= (strlen($smsg) > 0) ? successMessage($smsg) : "";
$msg .= (strlen($emsg) > 0) ? errorMessage($emsg) : "";
} else {
$msg = errorMessage("You need to add at least one image.");
}
}

和形式

    <form name="f2" action="" method="post" enctype="multipart/form-data">
        <fieldset>
         <select name="image_album">
            <option></option>;
           </select><br/><br />
              <input class="files" name="user_files[]" type="file" multiple><span><a href="javascript:void(0);" class="add" >add more</a></span>
              <div><input type="submit" class="submit" name="sub2" value="Up" /> </div>
            </fieldset>
          </form>

编辑

<?php
function errorMessage($smsg) {
    return '<div style="width:50%; margin:0 auto; border:2px solid #F00;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $smsg .     '</div>';
}
function successMessage($str) {
   return '<div style="width:50%; margin:0 auto; border:2px solid #06C;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}
?>

也许是您的upload_max_filesize值?

upload_max_filesize=100M

post_max_size 尝试相同操作

upload_max_filesize=100M

我从不使用纯数值,只使用数字和M字母


其他可能的解决方案包括:

  • 我检查了您的代码,没有发现$smsg$emsg变量,它们都是未定义的
  • 使用xdebug扩展或手动调试代码(使用带有任何变量的var_dump/echo,然后退出以检查某些条件是否正常工作)

PHP魔术师库

我测试了这个库,当我尝试上传3个jpegs和2个png时,我得到了以下错误:

Warning: imagepng(): gd-png error: compression level must be 0 through 9 in C:'workspace'test'phpmagician'php-image-magician'php_image_magician.php on line 2474
Warning: imagepng(): gd-png error: compression level must be 0 through 9 in C:'workspace'test'phpmagician'php-image-magician'php_image_magician.php on line 2474
Image themajesticsombrerogalaxym104.jpg is added successfully . 
Image mitsubishievo.jpg is added successfully . 
Image 6156_alt4.png is added successfully . 
Image leftbrainrightbrain.jpg is added successfully . 
Image leo.png is added successfully . 

然后,我检查了代码,发现了一个错误:在我的情况下,png质量设置为-36,正确的必须仅在0和9之间。


对不起我的英语。