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

时间:2015-03-20 17:29:13

标签: php image

我使用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>';
}
?>

1 个答案:

答案 0 :(得分:1)

也许是您的upload_max_filesize值?

upload_max_filesize = 100M

post_max_size

尝试相同的操作

upload_max_filesize = 100M

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


其他可能的解决方案是:

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

PHP Magician lib

我测试了这个lib,当我尝试上传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之间。


抱歉我的英文。