我正在使用非常方便的 SimpleImage PHP 类来调整图像大小,并且遇到了以下问题:
在测试上传各种图片时,某些图片正在上传但未调整大小。经调查,图像的实际尺寸似乎是问题的原因(而不是文件大小)。5M 的图像会上传和调整大小,而 190 万的图像不会。区别在于后者是 4000 X 3000 - 比另一个更大的尺寸。我调整了成功图像的大小以确保,并且发生了同样的失败。
这是一个静默失败,不会生成任何错误消息。
我正在使用确切的 SimpleImage 类,如此处所示
我将它与 Uploadify 结合使用,如下所示。目前,我只是想将平面转换为 200 像素高的图像。解决这个问题的任何帮助将不胜感激 - 我是否需要逐步调整大小,或者最好的方法是什么?
<?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.';
}
}
?>