SimpleImage PHP脚本因大图像而失败

时间:2012-10-16 18:16:22

标签: php image-resizing

使用非常方便的SimpleImage PHP类来调整图像大小,并遇到了以下问题:

在测试上传各种图像时,某些图片正在上传但未调整大小。经过调查,似乎图像的实际尺寸是问题的原因(不是文件大小)。 5M图像将上传和调整大小,而1.9M图像则不会。不同之处在于后者是4000 X 3000 - 尺寸比另一个大。我确定了成功图像的大小,同样的失败发生了。

这是一个静默失败,没有生成错误消息。

我使用的是精确的SimpleImage类,如here

所示

我将它与Uploadify结合使用,如下所示。目前,我只是试图将平面转换为200px高图像。我将非常感谢您解决这个问题的任何帮助 - 我是否需要逐步调整大小,或者最佳方法是什么?

<?php
session_id($_REQUEST['sID']);
session_start();
require_once '../config.php';

$target_dir = $_SERVER['DOCUMENT_ROOT'].GAL_DIR.$_POST['target_dir'];

if (!empty($_FILES)) {

    require_once 'SimpleImage.php';
    $file_parts = pathinfo($_FILES['Filedata']['name']);    

    $name = preg_replace("/[^A-ZÀ-ÿ0-9._-]/i", " ",$file_parts['filename']);

            /* Just some record keeping...
    $text = "txtrec.txt";
    $att = $target_dir."txtrec.txt";
    $record= "TIME: ".date('Y-m-d H:i:s')."\n";
    $record.= "IP: ".$_SERVER['REMOTE_ADDR']."\n";
    $record.= "DATA ARRAY: ". implode(",",$_FILES['Filedata'])."\n";
    $record.= "NAME: ".$_FILES["Filedata"]["name"]."\n"; //file-name.ext
    $record.= "TYPE: ".$_FILES["Filedata"]["type"]."\n"; //application/octet
    $record.= "SIZE: ".$_FILES["Filedata"]["size"]."\n"; //bit size
    $record.= "TMP_NAME: ".$_FILES["Filedata"]["tmp_name"]."\n"; //tmp storage name
    $record.= "BASENAME: ".$file_parts['basename'] ."\n"; //file-name.ext
    $record.= "EXTENSION: ".$file_parts['extension'] ."\n"; //ext (no dot)
    $record.= "FILENAME: ".$file_parts['filename'] ."\n"; //file-name (no dot/ext)
    $record.= "DOCUMENT_ROOT: ".$_SERVER['DOCUMENT_ROOT']."\n";
    $record.= "TARGET_DIR: ".$target_dir ."\n"; //path to gallery dir (with closing slash)
    $record.= "TMP_DIR: ".$target_dir.'tmp/'."\r\r\n";
    $fh = fopen($text, 'a') or die("can't open file");
        fwrite($fh, $record);
    fclose($fh);
    $fh = fopen($att, 'a') or die("can't open att file");
        fwrite($fh, $record);
    fclose($fh);
            */

    // Validate the file type
    $ok = FALSE;
    $ok_types = array('jpg','jpeg','gif','png');
    $file_ext = strtolower($file_parts['extension']);
    if (in_array($file_ext,$ok_types)) {
        $ok = TRUE;
    }

    if($ok) {
        $fileName = $name.'.'.$file_parts['extension'];
        array_push($_SESSION['files'],$fileName);
        $tmp_file=$target_dir.'tmp/'.$name.'.'.$file_parts['extension'];
        $target_file=$target_dir.$name.'.'.$file_parts['extension'];
        move_uploaded_file($_FILES["Filedata"]["tmp_name"],$tmp_file);

        list($width, $height, $type, $attr) = getimagesize($tmp_file);
        $dimensions = $width.' '.$height.' '.$type.' '.$attr;
        $fh = fopen($text, 'a') or die("can't open file");
            fwrite($fh, $dimensions);
        fclose($fh);

        $image = new SimpleImage();
        $image->load($tmp_file);
        $image->resizeToHeight(200);
        $image->save($target_file);

        unlink($tmp_file);

            echo '1';
    } 
    else {
        echo 'Invalid file type.';
    }
}
?>

1 个答案:

答案 0 :(得分:3)

如果在真正大的图像上失败,您的PHP脚本可能会耗尽内存或运行时间。尝试将它放在脚本的顶部:

ini_set('memory_limit','32M'); // Or However Big the Image is in MB
ini_set('max_execution_time', '60'); // Extend execution time to a minute