Am using the very handy SimpleImage PHP class for resizing images, and have run into the following problem:
While test uploading various images, certain pictures were being uploaded but not resized. Upon investigation, it appears that the actual dimensions of the image were the cause of the problem (not the file size). A 5M image would upload and resize, whereas a 1.9M image would not. The difference was the latter was 4000 X 3000 - larger dimensions than the other. I resized the succeful image to be sure, and the same failure occured.
This is a silent failure, no error messages are being generated.
I am using the exact SimpleImage class as shown here
I am using it in conjunction with Uploadify, as shown below. At the moment, I'm just trying to get a flat conversion to a 200px high image. Any help in solving this would be greatly appreciated - do I need to incrementally resize, or what is the best course to follow?
<?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.';
}
}
?>