View Full Version : Creating/Saving images to directory from MySQL BLOB data
CharlesHanna
10-2-05, 09:36 PM
Hello - I have a MySQL database with over 2000 entries, and images saved as a BLOB for each record. It has now become evident it is putting a strain on my queries, and delaying the response time .
Is there any way a script can generated to automatically call a BLOB image, then save it as a JPG or PNG to a separate directory in my htdocs/images folder? If so, any recommendations would be appreciated. I would like to do this to all the records, then delete the BLOBs from the database.
Thanks and Best Regards,
Charles
coldhead
10-3-05, 11:00 AM
http://www.onlamp.com/pub/a/php/2000/09/15/php_mysql.html?page=1
CharlesHanna
10-5-05, 01:43 PM
Thanks for your assistance, Coldhead, but I believe you've misunderstood my question.
I already know the procedure to upload to the MySQL database, and call the BLOB image via PHP to display, but I'm trying to figure out a way for a script to be executed, and automatically save the BLOB image to a directory based on its record number.
Michind, you've helped in the past -- any input on this one?
Thanks in advance,
Charles
mitchind
10-5-05, 01:51 PM
I think coldhead's link provides the starting point for you - the last page has a download script that shows you how to retrieve one file and display it/download it.
<?php
if ($id_files) {
include "open_db.inc";
$sql = "SELECT bin_data, filetype, filename, filesize FROM tbl_Files WHERE id_files=$id_files";
$result = @mysql_query($sql, $db);
$data = @mysql_result($result, 0, "bin_data");
$name = @mysql_result($result, 0, "filename");
$size = @mysql_result($result, 0, "filesize");
$type = @mysql_result($result, 0, "filetype");
header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
echo $data;
}
?>
This would have to be modified so query retrieves all results, then processes each result in a loop, writing blob to a file instead of echoing the data to the browser.
CharlesHanna
10-5-05, 01:55 PM
To expound on my last post, my ultimate goal is to replace the BLOB field with a path to the image in a directory folder, ie. http://www.mblock.com/blockhouse/products/images/1012.jpg ("1012" representing record ID#1012 in the MySQL database). I'd prefer not to upload these images all over again.
Thanks,
Charles
mitchind
10-5-05, 04:06 PM
Not sure why you're thinking you'd have to upload the images again.
Just change the output to a file on your server instead of the browser.
What are the fields called in your database? The example code should be easy to change.
mitchind
10-5-05, 04:41 PM
Something like this should work.... (EDIT: by the way - this a PHP5 script)
<?php
************* MODIFY THESE FOR YOUR SERVER AND DATABASE SETTINGS *************
// y = first letter in yourdomain.com
// o = second letter in yourdomain.com
$basedir = '/www/y/o/yourdomain.com/htdocs/images/'; // Needs trailing slash
$host = 'mysql##.powweb.com';
$user = 'db_username';
$password= 'db_password';
$dbname = 'db_name';
$blob_id = 'blob_id_fieldname';
$blob_data = 'blob_data_fieldname';
$blob_extension = 'blob_extension_fieldname'; // assumes valid extensions without "." (jpg, png)
$blob_table = 'blob_table_name';
************************************************** ***************
$db = @mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
@mysql_select_db($dbname, $db);
$sql = "SELECT $blob_id, $blob_data, $blob_extension FROM $blob_table";
$result = @mysql_query($sql);
header('Content-Type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row["$blob_id"];
$data = $row["$blob_data"];
$type = $row["$blob_extension"];
$file = "$basedir$id/.$type";
if (!file_exists($file)) {
file_put_contents($file, $data);
echo "Writing File: $file<br>";
} else {
echo "Skipping File: $file<br>";
}
sleep(1);
}
mysql_free_result($result);
?>
After you get all the files saved you can do a one time update query on your table to fill the image_file_location field - and then delete your blob field
CharlesHanna
10-5-05, 05:07 PM
The BLOB field is simply called "image".
When a product is listed in the search results, the following link is dynamically generated to see a detail on a product:
http://www.mblock.com/shop/index.php?tovid=917&c=detail
here is the script called detail.php:
<?php
require("./conf.php");
$sql="SELECT * FROM $table_prod WHERE ( id=$tovid )";
$res=mysql_query($sql);
while ( @$catt=mysql_fetch_array($res) )
{
echo"
<script language=JavaScript>
<!---
function korzyn(what)
{
var kilk=what.kilkist.value;
wk =window.open(\"./index.php?idtov=$tovid&id=$id&kil=\"+kilk+\"&c=add\",\"korz\",\"toolbar=0,scrollbars,status\");
wk.focus();
return false;
}
// -->
</script>
<center>
<form>
<table width=100% bgcolor='ddeedd' valign='top' cellspacing='1' cellpadding='1'>
<td class=detailels width=30%>Block Code: </td><td class=hd2>$catt[1] </td></tr>
<td class=detailels width=30%>Name: </td><td class=hd2>$catt[2] </td></tr>
<td class=detailels width=30%>Category: </td><td class=hd2>$catt[4] </td></tr>
<td class=detailels width=30%>Material: </td><td class=hd2>$catt[5] </td></tr>
<td class='detailels'>Product Line: </td><td class='hd2'> $catt[7]</td></tr>
<td class=detailels width=30%>Cost: </td><td class=hd2>$money $catt[8] </td></tr>
</table><p>
$catt[3] <br><p>";
if ($catt[9]!="") {echo "<img src=\"./img.php?idim=$catt[0]\" border=\"0\"><br><p>";}
echo"
</form>
<input type='button' value=\"Close window\" onclick=window.close()>
</body></html>
";
}
?>
and here is the script img.php:
<?php
Header("Content-type:image/gif");
require("./conf.php"); mysql_connect("$host_name","$db_user","$db_password");
mysql_select_db("$db_name");
$sql="SELECT * FROM $table_prod WHERE ( id=$idim)";
$type= "image/gif";
$res=mysql_query("$sql");
while ( @$catt=mysql_fetch_array($res) ){$imgsource=$catt[9];} $imgsource1=imap_base64($imgsource);
$size = XXXX; // new image width
$src = imagecreatefromstring($data);
//Get original width/height of image
$width = imagesx($src);
$height = imagesy($src);
$aspect_ratio = $height/$width;
if ($width <= $size) {
$new_w = $width;
$new_h = $height;
} else
{
// For the passed in width, calculate the new height
$new_w = $size;
$new_h = abs($new_w * $aspect_ratio);
}
// Here is the magic
$img = imagecreatetruecolor($new_w,$new_h);
imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$ width,$height);
// Based on the image type, send to client.
if ($type == "image/pjpeg") {
imagejpeg($img);
} else if ($type == "image/x-png") {
imagepng($img);
} else if ($type == "image/gif") {
imagegif($img);
}
imagedestroy($img);
?>
Hope this helps...
CharlesHanna
10-5-05, 05:14 PM
Mitchind,
I'm going over your script, and the only area I'm stumped on is
$blob_extension = 'blob_extension_fieldname'; // assumes valid extensions without "." (jpg, png)
I have no field that indicates the file extension - I believe the script img.php takes care of the image file type that is pulled from the BLOB.
Any suggestions?
mitchind
10-5-05, 05:40 PM
Your script hardcodes eveything as a gif file type in the line:
$type= "image/gif";
Remove the line from the loop:
$type = $row["$blob_extension"];
and instead put near the top:
$type = 'gif';
And remove the reference to the type field from the SQL statement
, $blob_extension
so it looks like
$sql = "SELECT $blob_id, $blob_data FROM $blob_table";
There's probably a few more edits needed, but it's a start... :)
CharlesHanna
10-5-05, 07:11 PM
OK...I've uploaded the named the script imagetransfer.php and nothing is happening (no errors, though - I guess THAT'S a good thing!):
<?php
// ************* MODIFY THESE FOR YOUR SERVER AND DATABASE SETTINGS *************
// y = first letter in yourdomain.com
// o = second letter in yourdomain.com
$basedir = '/www/m/mblock1/mblock.com/htdocs/blockhouse/products/images/'; // Needs trailing slash
$host = 'mysql01.powweb.com';
$user = '**********'; //(user information)
$password= '**********'; //(password information)
$dbname = 'blockhouse';
$type = 'jpg';
$blob_id = 'id';
$blob_data = 'image';
$blob_table = 'ecw_product';
// ************************************************** ***************
$db = @mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
@mysql_select_db($dbname, $db);
$sql = "SELECT $blob_id, $blob_data FROM $blob_table";
$result = @mysql_query($sql);
header('Content-Type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row["$blob_id"];
$data = $row["$blob_data"];
$file = "$basedir$id/.$type";
if (!file_exists($file)) {
file_put_contents($file);
echo "Writing File: $file<br>";
} else {
echo "Skipping File: $file<br>";
}
sleep(1);
}
mysql_free_result($result);
?>
As I originally uploaded JPG files to the MySQL database, that's what I'm trying to save to the directory.
What am I doing wrong?!
mitchind
10-5-05, 07:40 PM
My mistake .. change ...:
if (!file_exists($file)) {
file_put_contents($file,$data);
There might be more ...:)
mitchind
10-5-05, 07:45 PM
Your mistake ...
$basedir = '/www/m/b/mblock.com/htdocs/blockhouse/products/images/'; // Needs trailing slash
CharlesHanna
10-8-05, 04:58 PM
OK, seeing that PowWeb's running PHP v4.4.0 (not 5.0), I guess I can't use the function fileputcontents and would instead have to use fopen, fwrite, and fclose.
So I made the adjustments, but it's STILL not working:
<?php
// ************* MODIFY THESE FOR YOUR SERVER AND DATABASE SETTINGS *************
// y = first letter in yourdomain.com
// o = second letter in yourdomain.com
$basedir = '/www/m/b/mblock.com/htdocs/blockhouse/products/images/'; // Needs trailing slash
$host = 'mysql01.powweb.com';
$user = '**********'; //(user information)
$password= '**********'; //(password information)
$dbname = 'blockhouse';
$type = 'jpg';
$blob_id = 'id';
$blob_data = 'image';
$blob_table = 'ecw_product';
// ************************************************** ***************
$db = @mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
@mysql_select_db($dbname, $db);
$sql = "SELECT $blob_id, $blob_data FROM $blob_table";
$result = @mysql_query($sql);
header('Content-Type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row["$blob_id"];
$data = $row["$blob_data"];
$file = "$basedir$id/.$type";
if (!file_exists($file)) {
fopen($file, "w");
echo "Opening File: $file<br>";
fwrite($file,$data);
echo "Writing File: $file<br>";
} else {
echo "Skipping File: $file<br>";
fclose($file);
echo "Closing File: $file<br>";
}
sleep(1);
}
mysql_free_result($result);
?>
:eek: ARRRRGGGHHH!!
RTH10260
10-9-05, 10:01 AM
OK, seeing that PowWeb's running PHP v4.4.0 (not 5.0), I guess I can't use the function fileputcontents and would instead have to use fopen, fwrite, and fclose.
. . .
:eek: ARRRRGGGHHH!!
It's your choice of php4 or php5, but you need to snoop in the Powweb documentation to find the trigger :D
Use the file extension of '.PHP5' for the advanced version.
If you have many files with a plain '.php' extension and don't want to rename them, then drop this directive into a htaccess file in /htdocs (or in the subdirectory with your php5 level application):AddHandler application/x-httpd-php5 .phpsee: http://kb.powweb.com/search/php5
CharlesHanna
10-9-05, 09:32 PM
Whether it's PHP4 or PHP5 is frankly irrelevant to me at this point, as it diverts me from my goal (getting this script to work)!
Thanks for the input RTH10260, but I need guidance on a solution -- what am I doing WRONG with my script?
mitchind
10-10-05, 01:06 AM
I can guarantee you won't find anyone here able to deduce what's wrong with your script, unless you report what's happening when you say "it didn't work".
Are any debug statements being echoed to the screen?
Are you getting any files written at all?
Is your browser timing out?
Are you getting internal server errors?
Sure .. we could continue to guess what might be going on .. but it would be a lot easier if you helped us out .. otherwise our attempts to help might be greeted by such replies as your last one..
We'll wait for now ..
RTH10260
10-10-05, 04:14 AM
Whether it's PHP4 or PHP5 is frankly irrelevant to me at this point, as it diverts me from my goal (getting this script to work)!Well, when the script was originally for php5 and you are running it at level php4, then it's most likely not backward compatible, and running the script correctly may have helped.
CharlesHanna
10-10-05, 12:29 PM
I can guarantee you won't find anyone here able to deduce what's wrong with your script, unless you report what's happening when you say "it didn't work".
Are any debug statements being echoed to the screen?
- No, nothing. It appears that the file http://www.mblock.com/shop/imagetransfer5.php is called from the server, but absolutely nothing is returned back. I can't even see anything in the screen source.
Are you getting any files written at all?
- No files are written -- I've checked the folder, and it's still empty.
Is your browser timing out?
- No, no timeout errors.
Are you getting internal server errors?
- No, I checked my error log, and there's nothing there reported.
Sure .. we could continue to guess what might be going on .. but it would be a lot easier if you helped us out .. otherwise our attempts to help might be greeted by such replies as your last one..
- Forgive me, I didn't intend for my last reply to sound so abrasive - I guess my frustration was showing!!
RTH10260
10-10-05, 05:58 PM
$db = @mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
@mysql_select_db($dbname, $db);
$sql = "SELECT $blob_id, $blob_data FROM $blob_table";
$result = @mysql_query($sql);Please remove the three '@' markers that suppress the error messages to the script output. Your script is coded unsafe in respect to error handling. It ignores any errors and just continued with the next statements.
The conclusion from your prior post, that you don't see any debug statements would indicate that for some reason the database query returns no rows. The WHILE runs zero times.
mitchind
10-10-05, 08:52 PM
And perhaps echo the sql statement to the screen - then copy/paste the same code into phpMyAdmin and tell us what happens there.
$sql = "SELECT $blob_id, $blob_data FROM $blob_table";
echo $sql;
CharlesHanna
10-11-05, 12:07 PM
I have followed the advise of both of you, and now the following is reported back through the browser (attributed to the echo in the PHP script):
SELECT id, image FROM ecw_product
Other than this, nothing else happens -- I've checked the directory www.mblock.com/blockhouse/products/images, but no files have been written.
After researching a little on my end, I've modified the script to below, but still the same result:
<?php
// ************* MODIFY THESE FOR YOUR SERVER AND DATABASE SETTINGS *************
// y = first letter in yourdomain.com
// o = second letter in yourdomain.com
$basedir = '/www/m/b/mblock.com/htdocs/blockhouse/products/images/'; // Needs trailing slash
$host = 'mysql01.powweb.com';
$user = '*****'; //(user information)
$password= '*****'; //(password information)
$dbname = 'blockhouse';
$type = 'jpg';
$blob_id = 'id';
$blob_data = 'image';
$blob_table = 'ecw_product';
// ************************************************** ***************
$db = mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($dbname, $db);
$sql = "SELECT $blob_id, $blob_data FROM $blob_table";
echo $sql;
$result = mysql_query($sql);
header('Content-Type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row["$blob_id"];
$data = $row["$blob_data"];
$file = "$basedir$id/.$type";
if (!file_exists($file)) {
$fp=fopen($file,"w");
echo "Opening File: $file<br>";
fwrite($fp,$data);
echo "Writing File: $file<br>";
} else {
echo "Skipping File: $file<br>";
fclose($fp);
echo "Closeing File: $file<br>";
}
sleep(1);
}
mysql_free_result($result);
?> <?php
// ************* MODIFY THESE FOR YOUR SERVER AND DATABASE SETTINGS *************
// y = first letter in yourdomain.com
// o = second letter in yourdomain.com
$basedir = '/www/m/b/mblock.com/htdocs/blockhouse/products/images/'; // Needs trailing slash
$host = 'mysql01.powweb.com';
$user = '**********'; //(user information)
$password= '**********'; //(password information)
$dbname = 'blockhouse';
$type = 'jpg';
$blob_id = 'id';
$blob_data = 'image';
$blob_table = 'ecw_product';
// ************************************************** ***************
$db = mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($dbname, $db);
$sql = "SELECT $blob_id, $blob_data FROM $blob_table";
echo $sql;
$result = mysql_query($sql);
header('Content-Type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row["$blob_id"];
$data = $row["$blob_data"];
$file = "$basedir$id/.$type";
if (!file_exists($file)) {
$fp=fopen($file,"w");
echo "Opening File: $file<br>";
fwrite($fp,$data);
echo "Writing File: $file<br>";
} else {
echo "Skipping File: $file<br>";
fclose($fp);
echo "Closeing File: $file<br>";
}
sleep(1);
}
mysql_free_result($result);
?>
Any ideas?
Best Regards,
Charles
CharlesHanna
10-11-05, 12:17 PM
OK - I just realized that I revealed my username/pw -- I've since changed it.
Charles
mitchind
10-11-05, 12:24 PM
Have you tried that echoed sql statement in phpMyAdmin to see if it returns anything.
Check the column names to ensure they're correct.
Change these lines:
if (!file_exists($file)) {
$fp=fopen($file,"w");
echo "Opening File: $file<br>";toecho "Looking for File: $file<br>";
if (!file_exists($file)) {
$fp=fopen($file,"w");
echo "Opening File: $file<br>";
And see what it says.
RTH10260
10-11-05, 12:35 PM
Looking at this snippet of your code for further discussion: $id = $row["$blob_id"];
$data = $row["$blob_data"];
$file = "$basedir$id/.$type";
if (!file_exists($file)) {
$fp=fopen($file,"w");
echo "Opening File: $file<br>";
fwrite($fp,$data);
echo "Writing File: $file<br>";
} else {
echo "Skipping File: $file<br>";
fclose($fp);
echo "Closeing File: $file<br>";
}
You will want to have the file handling like follows, as you don't open any file if it already exists.: if (!file_exists($file)) {
$fp=fopen($file,"w");
echo "Opening File: $file<br>";
fwrite($fp,$data);
echo "Writing File: $file<br>";
fclose($fp);
echo "Closeing File: $file<br>";
} else {
echo "Skipping File: $file<br>";
}
Please check how you are building the file name:
$file = "$basedir$id/.$type";You probably don't want a slash in that position, it would indicate a further subdirectory name of 'id' (non-existend?) and a file name of '.jpg'. The effect on file_exists() is likely to be unexpected, though I would have expected to see the 'skipped' line listed.
CharlesHanna
10-11-05, 01:00 PM
I've made the script adjustments, and entered the query in phpmyadmin, and here's the result:
Database blockhouse - Table ecw_product running on mysql01.powweb.com
Error
SQL-query :
SELECT id, image
FROM ecw_product
MySQL said:
#2008 - MySQL client ran out of memory
mitchind
10-11-05, 01:13 PM
I've made the script adjustments, and entered the query in phpmyadmin, and here's the result:
Database blockhouse - Table ecw_product running on mysql01.powweb.com
Error
SQL-query :
SELECT id, image
FROM ecw_product
MySQL said:
#2008 - MySQL client ran out of memory
Hmm .. might have to adjust the query loop - you must have a whack of images in there. You also might need the multi-user patch if you have more than 72,000.
How about something like:
$sql = "SELECT $blob_id FROM $blob_table";
$result = mysql_query($sql);
header('Content-Type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row["$blob_id"];
$sql = "SELECT $blob_data FROM $blob_table WHERE $blob_id = $id";
$result_images = mysql_query($sql);
$image_row = mysql_fetch_array($result_images, MYSQL_ASSOC);
$data = $image_row["$blob_data"];
$file = "$basedir$id" . '.' . "$type";
if (!file_exists($file)) {
echo "Opening File: $file<br>";
$fp=fopen($file,"w");
echo "Writing File: $file<br>";
fwrite($fp,$data);
} else {
echo "Skipping File: $file<br>";
fclose($fp);
}
sleep(1);
}
CharlesHanna
10-11-05, 01:13 PM
OK, looks like I'm make SOME progress -- just to test, I temporarily adjusted the query to the following:
$sql = "SELECT $blob_id, $blob_data FROM $blob_table LIMIT 0, 100";
the script ran, and wrote the 100 files to the file directory, HOWEVER I clicked on on of the files (1.jpg), and displayed the following:
"The image “http://www.mblock.com/blockhouse/products/images/1.jpg” cannot be displayed, because it contains errors."
Am I using the wrong header content?
mitchind
10-11-05, 01:32 PM
Can I ask why you're still saving them as .jpg files.
Way back ... way way back .. I suggested that you need to change
$type = '.gif';
I know that you want to save them as JPEG files for some reason, but your original script that displays them to screen (and works) - outputs them as GIF files - so I have to assume that they are stored that way in the database... correct?
mitchind
10-11-05, 01:33 PM
Check to see if this works.
FTP to your image directory, manually change the extension to .gif and then input that gif filename to your browser
CharlesHanna
10-11-05, 02:12 PM
I initially changed the extention to .gif, and It comes up with a broken image.
I then changed the script to reflect
$type = 'gif';
and still got the same thing.
Interestingly enough, when all the images I originally uploaded to the MySQL database, are JPG files. Further evidence of this is when I enter the following in my browser client
http://www.mblock.com/shop/img.php?idim=202
(take a look at the page source when the image comes up).
mitchind
10-11-05, 02:24 PM
They could very well have been stored as JPG files but I'm not sure why they're served as GIF files. Your script - again way back ...
Note: Header("Content-type:image/gif");
and: $type= "image/gif";
and here is the script img.php:
<?php
Header("Content-type:image/gif");
require("./conf.php"); mysql_connect("$host_name","$db_user","$db_password");
mysql_select_db("$db_name");
$sql="SELECT * FROM $table_prod WHERE ( id=$idim)";
$type= "image/gif";
$res=mysql_query("$sql");
while ( @$catt=mysql_fetch_array($res) ){$imgsource=$catt[9];} $imgsource1=imap_base64($imgsource);
$size = XXXX; // new image width
$src = imagecreatefromstring($data);
//Get original width/height of image
$width = imagesx($src);
$height = imagesy($src);
$aspect_ratio = $height/$width;
if ($width <= $size) {
$new_w = $width;
$new_h = $height;
} else
{
// For the passed in width, calculate the new height
$new_w = $size;
$new_h = abs($new_w * $aspect_ratio);
}
// Here is the magic
$img = imagecreatetruecolor($new_w,$new_h);
imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$ width,$height);
// Based on the image type, send to client.
if ($type == "image/pjpeg") {
imagejpeg($img);
} else if ($type == "image/x-png") {
imagepng($img);
} else if ($type == "image/gif") {
imagegif($img);
}
imagedestroy($img);
?>
RTH10260
10-11-05, 02:26 PM
Comment on work in progress:
(1) Should you have lost track of what image types you have used in the database, apply function getimagesize() on the saved file, and among the resulting information, you will get the correct matching mime type. Use it to change the filename to use the correct file extension.
(2) You will have to experiment by trial and error on how many image files you can retrieve in one set so that the webserver doesn't time out on CPU usage (either its 30 or 20? seconds per web request). You will have to track progress and best submit an automatic refresh to get the next set without user interaction.
(3) A large result set with possibly huge blobs may exeed the memory allocated to the script, it's an php.ini parameter. Rather than modifying the ini file and using mysql_query() you may want to switch to mysql_unbuffered_query() for optimized processing:
$result_images = mysql_unbuffered_query($sql);
ref: http://us3.php.net/manual/en/function.mysql-unbuffered-query.php
mitchind
10-11-05, 04:29 PM
Do you have a .htaccess file somewhere above the images directory that handles .gif files?
When I tried to open a file directly from your images subdirectory it just processed it as a text file for some reason. I know that's not a POWWEB server directive.
CharlesHanna
10-11-05, 06:24 PM
(1) I read up on the getimagesize() function, but I'm not sure where to integrate it at -- I've made several attempts in different locations, but I kept getting parse errors all over the place!
(2) I'm not clear on how to invoke an automatic refresh.
(3) I will try this method, but need to address (1) and (2).
Thanks,
Charles
CharlesHanna
10-12-05, 09:17 PM
OK - it looks like I've figured this out, because I have successfully written the images to its destination path.
When I originally uploaded the JPG images to the server, it was encoded as BASE64 data. So after reviewing my scripts that created the images from the data, I figured that I needed to decode the data being received from the MySQL server by using the imap_base64() command:
<?php
// ************* MODIFY THESE FOR YOUR SERVER AND DATABASE SETTINGS *************
// y = first letter in yourdomain.com
// o = second letter in yourdomain.com
$basedir = '/www/m/b/mblock.com/htdocs/blockhouse/products/images/'; // Needs trailing slash
$host = 'mysql01.powweb.com';
$user = '**********'; //(user information)
$password= '**********'; //(password information)
$dbname = 'blockhouse';
$type = 'jpg';
$blob_id = 'id';
$blob_data = 'image';
$blob_table = 'ecw_product';
// ************************************************** ***************
$db = mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($dbname, $db);
$sql = "SELECT $blob_id, $blob_data FROM $blob_table";
echo $sql;
$result_images = mysql_unbuffered_query($sql);
header('Content-Type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($result_images, MYSQL_ASSOC)) {
$id = $row["$blob_id"];
$data = $row["$blob_data"];
$data1 = imap_base64($data); //added this line
$file = "$basedir$id.$type";
$size = getimagesize($file);
echo "Looking for File: $file<br>";
if (!file_exists($file)) {
$fp=fopen($file,"wb");
echo "Opening File: $file<br>";
fwrite($fp,$data1);
echo "Writing File: $file<br>";
fclose($fp);
echo "Closeing File: $file<br>";
} else {
echo "Skipping File: $file<br>";
}
sleep(1);
}
mysql_free_result($result);
?>
My next attempt will be to create thumbnails of the files - we'll see how that goes!! LOL
Thanks again for your assistance coldhead, Mitchind and Richard!
Best Regards,
Charles
mitchind
10-12-05, 09:19 PM
Good job perservering on the troubleshooting!
Others would have quit long ago. Glad you got it going. :D
CharlesHanna
10-14-05, 12:29 PM
OK, I've attempted to modify the script to resample and write the image to a thumbnail size, but I'm getting nothing back from the server in terms of an error or result.
<?php
// ************* MODIFY THESE FOR YOUR SERVER AND DATABASE SETTINGS *************
// y = first letter in yourdomain.com
// o = second letter in yourdomain.com
$basedir = '/www/m/b/mblock.com/htdocs/blockhouse/products/images/'; // Needs trailing slash
$host = 'mysql01.powweb.com';
$user = '**********'; //(user information)
$password= '**********'; //(password information)
$dbname = 'blockhouse';
$type = 'jpg';
$blob_id = 'id';
$blob_data = 'image';
$blob_table = 'ecw_product';
// ************************************************** ***************
$db = mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($dbname, $db);
$sql = "SELECT $blob_id, $blob_data FROM $blob_table";
echo $sql;
$result_images = mysql_unbuffered_query($sql);
header('Content-Type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($result_images, MYSQL_ASSOC)) {
$size = 90; // new image height
$id = $row["$blob_id"];
$data = $row["$blob_data"];
$data1 = imap_base64($data);
$src = imagecreatefromstring($data1);
//exit;
//Get original width/height of image
$width = imagesx($src);
$height = imagesy($src);
$aspect_ratio = $width/$height;
$new_h = $size;
$new_w = abs($new_h * $aspect_ratio);
$img = imagecreatetruecolor($new_w,$new_h);
$img1 = imagecopyresampled($img,$src,0,0,0,0,$new_w,$new_h ,$width,$height);
$file = "$basedir$id"&"_t.$type";
echo "Looking for File: $file<br>";
if (!file_exists($file)) {
$fp=fopen($file,"wb");
echo "Opening File: $file<br>";
fwrite($fp,$img1);
echo "Writing File: $file<br>";
fclose($fp);
echo "Closeing File: $file<br>";
} else {
echo "Skipping File: $file<br>";
}
sleep(1);
}
mysql_free_result($result);
?>
Any ideas on what I'm doing wrong?
Thanks in advance!
Best Regards,
Charles
mitchind
10-14-05, 12:39 PM
You've already figured this out once - and I suggested a reworked loop in
http://forum.powweb.com/showthread.php?p=338059#post338059
You shouldn't be retrieving all your blob_data in one query - get the ids first - then one image at a time.
RTH10260
10-14-05, 01:45 PM
OK, I've attempted to modify the script to resample and write the image to a thumbnail size, but I'm getting nothing back from the server in terms of an error or result.
. . .
Any ideas on what I'm doing wrong?It's all releated to timing, eg cpu usage. From some other discussions and from experimenting with a similar atsk, expect to be able to generate between 30 and 50 thumbnails until your luck runs out. You need to process in chunks.
Didn't you already re-generate jpg files from the blobs ? Why not use those rather than getting the images from the database with extra overhead ?
Use the Refresh http header to reload the webpage with the request. That will automate the processing without the need to refresh the webpage yourself. Keep the restart point for the next request in a file or database entry. Imbed your script into the full html code for the page:<html><head>
<meta http-equiv="REFRESH" content="5;url=<? echo $_SERVER['PHP_SELF']; ?>">
<title>Autorefresher sample</title>
</head><body>
<? . . . your php processing here . . . ?>
</body></html>but move the header() call to above the html code.
PS. If you are doing sufficient I/O like database access, the call to sleep() seems superfluous to me. It's mainly required with long computational bound scripts. I once recommended it's use when the same image was several times copied and resized before saving the whole set.
vBulletin v3.6.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.