try changing this line fputs($fd,$Images,strlen($Images)); to fputs($fd,$Images); as you don't need to specify the length. If you don't specify the length, it'll write the entire string. see how that goes...
-----Original Message----- From: Mike Gifford [mailto:[EMAIL PROTECTED]] Sent: Friday, November 30, 2001 3:47 PM To: [EMAIL PROTECTED] Subject: [PHP] Saving A Database Image to a File (Thumbnail Problems) Hello All, I'd really appreciate some help figuring out what's wrong with this code. I can't seem to successfully save a MySQL JPG Image to a physical jpg file (well atleast not a valid one) The critical pieces of the code look like this: // 1. Get Image from DB to String $sql = "SELECT Image FROM Images WHERE ID='$ID'"; $res = dbi_query ( $sql ); if ( $res ) { if($row = dbi_fetch_row( $res )) dbi_free_result ( $res ); $Images = stripslashes($row[0]); } $File = "ReThumbnail.jpg"; // 2. Write string to File $fd = fopen($File, "wb"); fputs($fd,$Images,strlen($Images)); fclose($fd); // 3. Create Thumbnail system("djpeg -pnm $File | pnmscale -xscale $xscale -yscale $yscale | cjpeg > $File.tmb"); // 4. Read New Thumbnail into a new string $fd = fopen( "$File.tmb", "rb"); $tmb = addslashes(fread($fd,filesize("$File.tmb"))); fclose($fd); // 5. Update Database $sql = "UPDATE Images SET Thumbnail='$tmb' WHERE ID=$ID"; if(dbi_query($sql)) { echo "ReThumbnailing Successful"; exit; } The problem seems to be in step 2 from what I can figure. I know that the Image file in the database is valid and outputs the appropriate info to display the jpg.. However, the image that is saved to the hard drive doesn't seem to be valid. Nor can I run the system command from the command line on the server: [mike@madras caravan2001]$ djpeg -pnm ReThumbnail.jpg | pnmscale -xscale .1 -yscale .1 | cjpeg > ReThumbnail.jpg.tmb Corrupt JPEG data: 67 extraneous bytes before marker 0xc2 Quantization table 0x01 was not defined pnmscale: Command not found. Empty input file I'm not sure what those extra 67 bytes are about.. Is the Oxc2 marker the beginning or the end? I couldn't figure this out either.. However, if step 2 isn't working, It's bungling up everything else... I thought that this might be an issue of strlen balking over the binary file. The documentation doesn't seem to think this would be a problem (with binary files) http://www.php.net/manual/en/function.strlen.php I found an alternate resource here http://www.php.net/manual/en/function.mysql-field-len.php and attempted to incorporate it with // Alternate way to determine strlen $result = mysql_query($sql); $ImageSize = mysql_field_len ($result, 0); // $ImageSize = strlen($Images); echo $ImageSize; That didn't work either.. However I wqas able to get some more useful information about the validity of strlen DB Result -rwxrwxrwx 1 mike www 17159 Nov 29 23:27 ReThumbnail.jpg -rwxrwxrwx 1 mike www 0 Nov 29 23:27 ReThumbnail.jpg.tmb 16777215 Temp Thumb File: ReThumbnail.jpg.tmb Thumbnail: ReThumbnailing Successful [mike@madras caravan2001]$ djpeg -pnm ReThumbnail.jpg | pnmscale -xscale .1 -yscale .1 | cjpeg > ReThumbnail.jpg.tmb Corrupt JPEG data: 67 extraneous bytes before marker 0xc2 Quantization table 0x01 was not defined pnmscale: Command not found. Empty input file strlen Result -rwxrwxrwx 1 mike www 17159 Nov 29 23:24 ReThumbnail.jpg -rwxrwxrwx 1 mike www 0 Nov 29 23:24 ReThumbnail.jpg.tmb 17159 Temp Thumb File: ReThumbnail.jpg.tmb Thumbnail: ReThumbnailing Successful [mike@madras caravan2001]$ djpeg -pnm ReThumbnail.jpg | pnmscale -xscale .1 -yscale .1 | cjpeg > ReThumbnail.jpg.tmb Corrupt JPEG data: 67 extraneous bytes before marker 0xc2 Quantization table 0x01 was not defined pnmscale: Command not found. Empty input file Any suggestions would be appreciated.. Really, all I'm trying to do is take a MySQL jpg image and shring it 10% and then save it again as another MySQL Image (a thumbnail)... Mike -- Mike Gifford, OpenConcept Consulting, http://www.openconcept.ca Offering everything your organization needs for an effective web site. New PHP/MySQL Photogallery & Great Pictures http://genevilleneuve.com In all things it is better to hope than to despair.Wolfgang von Goethe -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]