Create image and put it into db

Hi, I was just wondering if there is a way of putting a newly created image into the db without creating the image on the harddrive first. Here is my shortend code:


  if($_FILES['uploadedpic']['tmp_name']!=''){
    $image = addslashes (file_get_contents($_FILES['uploadedpic']['tmp_name']));
  }else{/* Create a blank image */
    $im = imagecreatetruecolor(150, 200); 
    $tc = imagecolorallocate($im, 50, 50, 50);
    imagestring($im, 1, 15, 50, "no pic", $tc);
    imagejpeg($im, "temp.pic");
    $image = addslashes(file_get_contents("temp.pic"));
  }
  $result = mysql_query('INSERT INTO album (pic) VALUES ("'.$image.'")');

If the user did not specify an image, a default “no pic” image is created. $image is then put into the db. The problem is that I have to create “temp.pic” first with imagejpeg(). Can’t I skip this step and somehow put $im into $image?

Not to my knowledge… but nothing stops you from destroying temp.pic once you’re done.

I just thought its unnecessary overhead. My other concern is that another user might change the temp.pic between
imagejpeg($im, “temp.pic”); and
$image = addslashes(file_get_contents(“temp.pic”));

To prevent the wrong pic being send to the db I’m thinking of adding a random number to the name e.g.

$temp = rand().“temp.pic”;
imagejpeg($im, $temp);

but then I will have to destroy it every time.
(How do I do that again?..)

consider making the temp.pic name Username.pic , that way it’s not random.

Deleting the picture would be the unlink() command.

by the way ,whats point here storing file in database rather than file system?
any specific requirements?
otherwise article like “storing file in filesystem v/s database” may come handy

There are different arguments for and against putting files in the database; easier backups, cleaner folders, more control over who sees what etc. I’m still learning and at the moment the main reason for me was to see if I could do it.

Thanks for the help!

a really faffing big database is also a concern… slower queries as it has to output much more data, especially if the picture is big.

no problems…
but
easier backup…
many server do have certain limitations on size of the file that can be back at once…so it will actually a problem and harder to backup once your image grow in number
plus image folder backup using ftp or other tools is much easier i guess than downloading 100mb sql file

cleaner folder
it depends upon how much have structured…if you have structure in proper way,it will rather help to find the exact file and will be much cleaner

control who see it
even if you store file filesystem you will have corresponding row for it in database (without binary image)…so i dont think this advantage applies…ie you can track it with out storing binary file as well

plus big problem
if image is big,it will make database and query slow significantly which will be very harmful for you application
and there are many others…
may be reading some more article will help u…

Good points!

Thanks again for a good forum!

If you want to skip writing to the file system, you can start an output buffer, write the image to the output stream, then get the buffer contents.


ob_start();
imagejpeg($im);
$image = ob_get_clean();

Im guessing it would take more ram, but it saves having to make and then destroy a file every time.