Using convert_uuencode to store images in MySQL DB
Posted by JimL (JimL), 3 September 2004Currently I have a page written in PHP which automatically reads images from the "photos" subdirectory and writes the correct HTML to the browser.
The PHP utilises the opendir() function, and works quite nicely.
However, the text next to the image is simply the image name minus any extension (I use regular expressions to remove the .jpg or .gif extensions etc.) This method requires the image name to also be it's description (for example I would need to name an image "photo of racing incident at Silverstone.jpg" for the text to read "Photo of racing incident at Silverstone"). This is very clumsy, and not, in my opinion, a very effective way of dealing with image text.
However, instead of storing the images in a photos folder, I am thinking instead about storing the images in a MySQL database table (which would allow a field for image description). I am considering using convert_uuencode() to convert the image into a form which is storable in the database, and convert_uudecode() to "reconstitute" the image in order to present to the browser.
Has anyone any thoughts on this? Would it work?
Posted by JimL (JimL), 3 September 2004Correction: I used the readdir() function to read in the images from the "photos" subfolder.
Posted by admin (Graham Ellis), 3 September 2004Jim, Store the images in a table with a datatype of blob. Blobs are designed to hold true binary information - I've done it and it works well if you get the "addslashes" right when you put the data in. You can then have a column of text with your labels.
I'm out of the office until Monday morning, and I think I have an example there. If an example would be useful even with the few days delay, please post a follow up and I'll see what I can find.
Posted by JimL (JimL), 6 September 2004Hi Graham, thanks for that!
However, it occured to me later that I might have problems getting my stored photos to appear on the website.
If I am relying on the HTML code <img src="photo.jpg">, then surely I will have to have the photo as a physical file on the webserver?
Whilst I like the idea of storing the photos in a database, unless I can get them to appear in my webpages, I can't see that the database idea is going to be worth doing.
Posted by admin (Graham Ellis), 6 September 2004You can very easily write a PHP script that will connect to your database, read an image and output it. Place that script in the IMG tag in the SRC= attribute. Example to follow ...
Edit - I have bumped the thread "Random image in a web page" to the top of this board as it covers much of what you need
Posted by JimL (JimL), 9 September 2004Hi Graham!
So all I need to do is place the blob data (extracted from the database) directly into the src attribute?
Posted by admin (Graham Ellis), 9 September 2004It's fairly straightforward but not quite that simple. Have a look at the "random image" thread and instead of reading from a random file, read from the database ...
Posted by JimL (JimL), 10 September 2004Hi Graham,
I need to pull my image in using <img src="image.php4">, and image.php4 will be like the following:-
/* My code to extract the image blob data from the database goes here */
So effectively image.php4 will mimic a real image sent via HTTP.
However, in the same page I want to refer to many images using this method. (In fact, I want to automatically insert all the images in my database via this method).
Short of having a plethora of image(x).php4 files (one for each image stored in the database), how can I pass variables into my image.php4 script?
There is probably a very simple answer to this, but at the moment I'm scratching my head to find one. The only method I know of passing variables from one script to another is via the get or post methods within forms.
Posted by admin (Graham Ellis), 10 September 2004You can write something like:
and then reference $_GET[name] in your PHP script
Posted by JimL (JimL), 10 September 2004Marvellous!
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho