PDA

View Full Version : image manipulation php mysql blob type



evas
05-29-2011, 10:39 PM
hello there! i got a question about blob mysql data.i have a button and on buttonclick i want to open a popup (and passing date parameter) and show some details and display a photo.if i try

<?php
if (isset($_GET['p'])){
$temp_date=$_GET['p'];
echo "DATE : $temp_d <br>";
$date_m=date('Y-m-d',strtotime($d));
$connect=mysql_connect("localhost","user","pass")
or die ("Check your server connection");
$db_found = mysql_select_db("mydb", $connect);
$SQL = "SELECT * FROM mytable
WHERE date =date_m";
$result = mysql_query($SQL);
$row=mysql_fetch_array($result);
echo "Details: $row[details]<br>";
$content = $row['image'];
header('Content-type:image/jpg');
echo $content;
} ?>



its not working.if i try

<?php
$connect=mysql_connect("localhost","user","pass")
or die ("Check your server connection");
$db_found = mysql_select_db("mydb", $connect);
$SQL = "SELECT * FROM mytable
WHERE id=1";
$result = mysql_query($SQL);
$row=mysql_fetch_array($result);
echo "Details: $row[details]<br>";
$content = $row['image'];
header('Content-type:image/jpg');
echo $content;
} ?>



its shows my results.how to pass the parameter and show the results to ? and does anybody knows why it doesnt work with parameter passing ? tnx in advance

traq
05-29-2011, 11:18 PM
You need to provide more details. What, exactly, is "not working"? What happens when you run the code, and what should happen? Do you receive any error messages?

just at a glance, you're trying to send headers after you've already echoed output to the browser.

in addition, you're using several variable names that don't exist: e.g., you set $temp_date and then try to use $temp_d. if these variables are set elsewhere in your code, please make a note of it, otherwise it will confuse the troubleshooting process. It is best to post all of your code.

evas
05-31-2011, 07:40 PM
i ve tried a lot...and i see that if i use header to show image i cannot echo anything else. is that true ? it doesnt seem logical to me....i can show a picture but no messages ? is there any other way to show image and echo details together ??

traq
06-01-2011, 02:06 AM
sending "Content-type: image/jpg" tell the browser that you're sending a picture - not a page with a picture in it. if you want to send content as well, put the image into regular html markup:

print '<p>'. $row['details'] .'</p><img src="data:image/jpg;base64,'. $row['content'] .'">';
// (assuming $row['content'] holds the binary source for the image)

is there a reason you're saving the images themselves in your database? i've always found more practical to save the image as a file and simply save the url in the database.

as for efficiency, printing the binary source saves an http request, but bloats the markup. it's okay for small, cacheable images, I guess.

james438
06-01-2011, 03:17 AM
In my experience databases tend to have a far smaller storage size than server storage. The largest I have seen for database storage is 1GB and regular hosting storage goes up to unlimited.

traq
06-01-2011, 03:39 AM
In my experience databases tend to have a far smaller storage size than server storage...

true also.

djr33
06-02-2011, 12:56 AM
Databases can be unlimited (although that's rare), but sometimes they are actually limited by the software-- I think 4GB is a common limit depending on the format of the drive used to store it. This is beyond any limit that your host may have.

Regardless, it's not a good idea to save them directly to the database for a few reasons: most importantly 1) it's inefficient and will slow down your database; 2) they won't act as files so things like caching in the browser won't work and you'll waste bandwidth. It's also harder to back up your database (if there are a lot of images since it will be huge) and there's no reason NOT to store them as files-- since they work as files and you can store a path in the database, that's the common (and best) method. Your choice though.

evas
06-04-2011, 10:27 AM
it just been more easy to put the image in the table;now i must worry about unigue filenames....dont decided yet ! tnx for replies thoough

djr33
06-04-2011, 03:53 PM
When storing images from users, you can do a lot of things to avoid filename duplication: you can give each user a folder, you can add a user-id prefix to each filename (27-image.jpg), or you can not use filenames-- generate a random name (or use the current date or timestamp) and store the actual filename in the database.

Think of the file system as a storage room, and the database as a catalog. But all of the details really depend on the system. Also, the specific details will become more important as your site grows. If your site never gets too big, then it may not matter how it's setup. But if you want a "perfect" system, imagine a site that stores 1,000,000 images. Then think about possible problems with the design.

traq
06-04-2011, 08:03 PM
some CMSs use a file hash as the filename. The relationships (description, owner, etc.) are stored in the database. Using a hash also has the advantage that you can use the filename as a checksum (you can automatically tell if someone is uploading a file that already exists).

djr33
06-05-2011, 02:02 AM
That's a good idea. It's a little complicated but it works well. And even if you're not using it to see if two images are the same, it is a good way to be fairly sure that two filenames won't overlap-- the odds are severely against coincidental overlap-- less than 1/1,000,000.

I've used the same idea for creating thumbnails: the user uploads an image and that is stored somewhere, and a thumbnail sized image is also generated and saved as a hashed version of that image-- so if there are two images with the same contents, the thumbnail is reused.