I'm writing a photo album website, but I'm having problems with the upload script. It's partially something I found online that I've modified the heck out of.
My problem is that about 30% of the time, instead of getting a page that says "file uploaded; here's your thumbnail...", I just get a blank page and the HTML indicates that the code did not fully complete.
I've been trying to figure out different things, and it seems that I am dealing with a file size issue but I don't know why. Files >500KB give me a blank screen.
I've added a bunch of echo statements, just to find where the code stalls. It seems to stall at the main function block, which I've bolded. Each of those lines has a die command, but if the error is there, they don't display.
Can anyone explain where the file size becomes an issue and how I fix it?
Thanks.
<?php
$newfile_name=$_GET['filename']; //for file name
$yr=substr($newfile_name,2,2);; // short version, for correct folder
$year='20'.$yr; // long version, for titles etc
/
THIS IS THE PICTURE UPLOAD & RESIZE SCRIPT
/
$thsize = '90'; // the thumbnail height
$mnsize = '640'; // the main image height
$filedir = $year.'/pics/'; // the directory for the original image
$thumbdir = $year.'/thumbs/'; // the directory for the thumbnail image
$maindir = $year.'/slides/'; // the directory for the main image
$maxfile = '5000000';
$mode = '0666';
$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];
$stuff = array("_", "-", " ", "/", "#", "$", "@", "!", "%", "^", "&", "*", "/", "\\");
$userfile_name = str_replace($stuff, "", $userfile_name);
$userfile_name = strtolower($userfile_name);
$path = pathinfo($userfile_name);
if ($path['extension'] =="jpeg"){
trim($userfile_name,"jpeg");
$userfile_name = $userfile_name.'.jpg';
}
if ($path['extension'] !="jpg"){
echo '<br />Please use JPEG images only.';
exit();
}
/*
display variables for error resolution
echo '<br />name '.$userfile_name;
echo '<br />tmp '.$userfile_tmp;
echo '<br />size '.$userfile_size;
echo '<br />type '.$userfile_tyle;
echo '<br />path ';
print_r($path);
*/
if ($userfile_size>$maxfile){
echo '<br />File is too large.';
exit();
}elseif ($userfile_size<1){
echo '<br />File has no data.';
exit();
}else {
$prod_img = $filedir.$userfile_name; //working copy of upload
$prod_img_main = $maindir.$newfile_name; // where the main slide is saved
move_uploaded_file($userfile_tmp, $prod_img);
chmod ($prod_img, octdec($mode));
$sizes = getimagesize($prod_img);
echo '<br /> size '.$sizes;
$aspect_ratio = $sizes[1]/$sizes[0]; //1=height,0=width
if ($sizes[1] <= $mnsize && $sizes[0] <= $mnsize) // if is smaller than parameters, do nothing to size
{
$mnnew_width = $sizes[0];
$mnnew_height = $sizes[1];
}else{
if ($sizes[1]>=$sizes[0]){
$mnnew_height = $mnsize;
$mnnew_width = abs($mnnew_height/$aspect_ratio);
}else {
$mnnew_width = $mnsize;
$mnnew_height = abs($mnnew_width*$aspect_ratio);
}
}
echo '<br />mnwidth '.$mnnew_width;
echo '<br />mnheight '.$mnnew_height;
this is as far as the script executes with certain images, so I assume the error is in the following lines:
$destimg=ImageCreateTrueColor($mnnew_width,$mnnew_height) or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');
ImageCopyResized($destimg,$srcimg,0,0,0,0,$mnnew_width,$mnnew_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
ImageJPEG($destimg,$prod_img_main,90) or die('Problem In saving');
imagedestroy($destimg);
$working_img = $prod_img_main; //where the initial image is saved
$prod_img_thumb = $thumbdir.$newfile_name; // where the thumbnail is saved
//echo '<br />img '.$working_img;
//echo '<br />th '.$prod_img_thumb;
move_uploaded_file($userfile_tmp, $working_img);
chmod ($working_img, octdec($mode));
$sizes = getimagesize($working_img);
$aspect_ratio = $sizes[1]/$sizes[0]; //1=height,0=width
if ($sizes[1] <= $thsize && $sizes[0] <= $thsize)
{
$thnew_width = $sizes[0];
$thnew_height = $sizes[1];
}else{
if ($sizes[1]>=$sizes[0]){
$thnew_height = $thsize;
$thnew_width = abs($thnew_height/$aspect_ratio);
}else {
$thnew_width = $thsize;
$thnew_height = abs($thnew_width*$aspect_ratio);
}
}
echo '<br />thwidth '.$thnew_width;
echo '<br />thheight '.$thnew_height;
$destimg=ImageCreateTrueColor($thnew_width,$thnew_height) or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($working_img) or die('Problem In opening Source Image');
ImageCopyResized($destimg,$srcimg,0,0,0,0,$thnew_width,$thnew_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving');
imagedestroy($destimg);
}
unlink($prod_img);
include ('header.inc');
echo '<h3>Here\'s the thumbnail, with a link to the main image.</h3>';
echo '<img src="'.$prod_img_thumb.'" width="'.$thnew_width.'" height="'.$thnew_height.'">';
echo "\r\n";
echo '<br />Upload more pictures: HERE';
echo "\r\n";
echo '<br />Go to the Main Album Index: HERE';
echo '</body></html>';
/
END OF PIC ADD & RESIZE SCRIPT
/
?>