Forum Moderators: coopster
Reduced to what I consider its bare essentials here. It works on my server at home, but not on my hosting companies server with files about 400K or greater - eg. anything from a modern digital camera.
<?
$form = "<FORM ENCTYPE='multipart/form-data' ACTION='$PHP_SELF' METHOD=POST>
<INPUT TYPE='hidden' name='MAX_FILE_SIZE' value='2000000'>
<INPUT NAME='imgfile' TYPE='file'>
<input type='submit' name='submit' value='Add Image'>
</form>";if ($submit) {
dump($_FILES);
$width = 300;
$height = 300;
$res = graphics_resize($width,$height,"TEST","Photos");
if ($res) {
print "<img src='Photos/TEST.jpg'>";
}
} else {
print $form;
}
function graphics_load_jpeg($imgname)
{
debug("Opening file $imgname");
$im = @imagecreatefromjpeg($imgname); /* Attempt to open */
if (!$im) { /* See if it failed */
debug("Failed to open image $imgname");
$im = imagecreate(150, 30); /* Create a blank image */
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an errmsg */
imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}
function graphics_resize(&$w,&$h,$name,$dir) {
Global $imgfile;
$uploaddir = "$dir/";
$uploadfile = $uploaddir . $_FILES['imgfile']['name'];
if ($_FILES['imgfile']['type'] == 'image/pjpeg' ¦¦ $_FILES['imgfile']['type'] == 'image/jpeg') {
$srcimg = graphics_load_jpeg($_FILES['imgfile']['tmp_name']);
} else { return 0;}
$dstimg = imageCreate($w,$h);
imageCopyResized($dstimg,$srcimg,0,0,0,0,$w,$h,imagesx($srcimg),imagesy($srcimg));
imageJpeg($dstimg,"$dir/$name.jpg");
imageDestroy($srcimg);
imageDestroy($dstimg);
return 1;
}
function debug($msg) {
print "<pre>$msg</pre>";
}
function dump($var) {
print "<pre>";
print_r($var);
print "</pre>";
}
?>
The results using a random file from my digi camera.
Array
(
[imgfile] => Array
(
[name] => dcp_0359.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpqGBCz7
[error] => 0
[size] => 585731
))
Opening file /tmp/phpqGBCz7
Failed to open image /tmp/phpqGBCz7
From phpinfo()
GD Supportenabled
GD Version1.6.2 or higher
FreeType Supportenabled
FreeType Linkagewith freetype
GIF Read Supportenabled
GIF Create Supportenabled
JPG Supportenabled
PNG Supportenabled
WBMP Supportenabled
max_execution_time
5000050000
memory_limit
40M40M
post_max_size
8M8M
register_globals
OnOn
safe_mode
OffOff
upload_max_filesize
20M20M
Everything seems to be ok - does anyone see anything before I try to tackle the (use--ss) support team at the "worlds largest and best host"?
Does the script work on your host - very useful if it does :) Will post the little bit to maintain aspect ratio later.
Thanks for any help...
$im = @imagecreatefromjpeg($imgname);
what about move_uploaded_file [ca3.php.net]?
Though it seems strange that it only happens once it gets over 400k.
You could also take a look at this one, it may give some insight
Image upload and Thumbnail Generator [webmasterworld.com] - msg 29
I will spend another 30 mins now trying random things - whilst waiting for the support team at my host to send some thing back..
As always - thanks for the ideas and help guys :)
I hate this type of bug.
"You can preface a (builtin php?) function call with an @ sign, as in:
@foo();
This will supress the injection of error messages into the data stream output to the web client. You might do this, for example, to supress the display of error messages were foo() a database function and the database server was down. However, you're probably better off using php configuration directives or error handling functions than using this feature.
See the section on error handling functions."
I'll double check see if the error is anything useful... cheers.
<added>Nothing useful</added>
The only change I made was to the *if ($submit) {* logic since I have register_globals OFF:
*if (isset($_POST['submit'])) {*.
Image troubleshooting is a bear. If you are able to upload and move the file, then the seemingly obvious issue lies in configuration. Notice the use of the term, seemingly. That's my disclaimer :)
Maybe you can narrow things down by determining which library might be the issue...the GD library or the jpeg library?
Since you stated that you are running GD > 1.6 and I see you have both GIF CREATE and PNG support enabled, have you tried creating a PNG or GIF image? That might discount the GD lib being the issue and allow you to focus on the JPEG support.
Just some ideas...
The gif tests work -
The jpeg tests fails on large images. Which means that the problem lies with the jpeg libraries.
Just got the technical support response. "we do not support script debugging" - I'm not asking for script debugging - I'm asking for a working setup! - really annoying...
Tech support is so bad with this company, clueless newbie rejecting support requests without having a clue what they are reading.
(different images)
1588x1191 - works
1589x1192 - fails
Different script - live site - more memory used in layout formatting etc. 1588x1191 and 1159x1192 both fail.
Regarding bit depth - I don't have an option to see/set this in Fireworks - quality is set at 70%
(I wish I wish they'd install imagemagik - tried asking a long time ago)
jpegtran command line utility to debug. See the jpegtran man pages for more info (running in debug/verbose will also let you know which jpeg library your host is running -- look for the version near the top of the verbose output).
If I take that 1589x1192 file and save it in an uncompressed format such as .bmp, it's about 5.5 megabytes in size.
Coopster - thanks the jpegtran command let me find some commands that eventually led to the work around.
Jomaxx - yep, my thoughts as well, thats why the dimesions of the image were more important than the compressed size.
So - my work around - I hope this is useful to someone sometime.
function graphics_jpeg_resize($w,$h) {
$file = $_FILES['imgfile']['tmp_name'];
$tmpfile = "/tmp/" . rand(1000,9999);
system("djpeg $file >$tmpfile");
system("pnmscale -xysize $w $h $tmpfile ¦ cjpeg -smoo 5 -qual 75 >$file");
}
Some test to ensure tmpfile is unique could be added.
I slot this in to the functions in the first post by checking that the image is a jpeg, and if it is - pre-resizing it to the requested size and then loadaing it with imagecreatefromjpeg() to do any GD post processing.
I guess there must be a limit on the image size somewhere as well - but my digital camera photos at around 3000px x 2000px are working well.
Thanks for your help guys.