【问题标题】:PHP Image Magician crashing when upload multiple big images上传多个大图像时PHP Image Magician崩溃
【发布时间】:2015-05-24 05:08:42
【问题描述】:

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

【问题讨论】:

  • 编辑您的问题并插入上传代码。
  • 你的 PHP 版本是什么?

标签: php image


【解决方案1】:

也许是你的upload_max_filesize 值?

upload_max_filesize = 100M

post_max_size尝试同样的操作

upload_max_filesize = 100M

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


其他可能的解决方案是:

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

PHP 魔术师库

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


对不起我的英语。

【讨论】:

  • 也是100M。我认为问题出在库中,因为我可以上传多个小图像。
  • 我现在要测试这个库
  • 好的,我想我找到了问题所在。我添加了memory_limit: 96;.. 我尝试使用memory_limit: 256;,但我现在无法上传更多内容。所以问题不在于代码,而在于服务器。
  • 感谢您的帮助!
  • 很高兴为您提供帮助。但我有一个观察结果(再次查看我的问题)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多