Forum Moderators: coopster

Message Too Old, No Replies

GD Upload and Resize Script.

But it doesn't work on my host?!? (after 400k ish)

         

gethan

9:24 pm on Feb 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. So this is an upload and resize script that will produce thumb nails etc. Built mainly with examples from php.net

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...

jatar_k

2:52 am on Feb 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I wonder if you have permission to access it in tmp

$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

gethan

10:58 am on Feb 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep initially my thoughts also - but the move_uploaded_file works fine. The error occurs at the imagecreatefromjpeg function.

My only guess is that there is a bug in the version of php GD on my hosts installation.

coopster

1:06 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There is a code snippet on the imagecreatefromjpeg [php.net] manual page that may help you troubleshoot. See example 1.

gethan

1:23 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put in the snippet with the fix set to true and still no joy. I will try taking this up with the support team at my host, if it ever gets resolved I'll post what was going on.

jatar_k

4:13 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you tried move_uploaded_file and then imagecreatefromjpeg when the image has been moved into a dir you have control over?

Does you get the same error/behaviour then?

gethan

9:54 am on Feb 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep. I tried that. I removed it from the posted script to try and get to the absolute core of the problem.

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.

mykel79

10:36 am on Feb 4, 2004 (gmt 0)

10+ Year Member



Just a thought:
$im = @imagecreatefromjpeg($imgname);

If it's @ then it doesn't return anything right? So $im is ALWAYS empty. At least I think it is :)
Have you tried without the @?

gethan

2:48 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep... tried that -

"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>

coopster

4:04 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



For the record, I took your script and ran it just fine using .jpeg images of 37k as well as 532k with no issue on one of my test servers.

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...

gethan

4:41 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Coopster thank you.

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.

coopster

4:58 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It does work with smaller jpegs though?

gethan

5:06 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes - I haven't found what the limit is, but over 400k consistantly fails and 100kish is fine. Really puzzled.

gethan

5:18 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I have something.

The dimensions of the image cause failing rather than the size. A 1.5mb high quality 1152x864 image was successful.

But a 570k 70% quality at 2000x1500 failed.

Is there a size limit with createimagefromjpeg that I'm not aware of?

coopster

5:39 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's probably not the size limit, but the bit depth. What is the bit depth of the jpeg that fails and what are the bit depths of the jpegs that succeed?

gethan

5:54 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think it must be memory related.

(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)

coopster

6:02 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, if you have access to the command line, you could FTP the file to the server, then use the
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).

jomaxx

8:31 pm on Feb 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a point to consider - the file size of the .jpg file may not be the most relevant statistic, because the file has been compressed. Presumably the server must UNcompress the file in memory before doing any manipulation on it.

If I take that 1589x1192 file and save it in an uncompressed format such as .bmp, it's about 5.5 megabytes in size.

gethan

8:29 am on Feb 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well I didn't solve this problem - but made a work around.

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.