Stuck on imagejpeg whilst trying to upload BLOB to MySQL!
Hi guys,
I am uploading a jpeg, resizing it, and then I want to upload it to mysql.
I am currently stuck at imagejpeg where I have resized the image, created it and now all I have to do is upload it to the db. I need the images to go into the
database as BLOB rather than the linking way of doing it.
The tricky bit is imagejpeg($new_image, '', $jpeg_quality);
What is the middle bit for? All I get is BLOB data outputted to the screen. I can't store it in a variable because then it comes out as a resource #, or a var/tmp/php whatever, all I want and need is the binary...
Here is my code:
PHP Code:
<?
if(!$submit){
?>
<html>
<title><head>Uploading, Resizing and Updating in MySQL</title></head>
<body bgcolor="white">
<form method="post" action="new_index.php" enctype="multipart/form-data">
<h1>Upload an Image File</h1>
<h3>Please fill in the details below to upload your file.</h3>
<table>
<col span="1" align="right">
Is that susposed to be an "if" statement at the start of the last line of code?
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Can I ask why you're storing images in the database itself? It's a fairly inefficient way to do it; the usual way is to keep the images in a directory and just store pointers (paths) to the images in the database. The overhead required for storing and retrieving images from a database is pretty high compared to just storing the pointers.
On the other note... Storing the images in database. The images I am storing are photos of users. They are resized before storing, so they are not that large. The benefits for me are: A. Ease B. Backed up with the rest of the data. C. I can easily send them through standard XML to other applications. This is definitely a much debated topic though. As I understand it, when you are talking about very large systems it might actually be better to keep it in a database because you more easily replicate the databases. Anyway, I haven't yet noticed a slowdown from it.
One point of note for beginners: Try not to store images and large text blocks in the same table as other data. For me I have user data in one table and I keep their images separately in a two column InnoDB table (image_id and Blob). Which a resource table (user_id, image_id) can then keep track of the instances. When your SQL is set up right with references and cascades, it can manage them for you. (as to delete images when you delete users etc.) This way when I access my user data and I am not accessing the images.
Last edited by stephen.bigelis; 11-09-07 at 04:50 PM.