Jump to content

Image Base64 -- Quality Loss Issues!


crittope

Recommended Posts

Hello,

 

I'm looking to avoid storing flat files of images for a specific reason. I have nearly 500 images that I wish to store into my database to display on my website. I'm able to obviously get the binary data for the image using curl and encode it using base64. However, when I'm going to display the images they either have extremely noticeable quality loss, or corrupts and only displays half of the image. Granted, the images that I'm using are 960x540px with a file size that could be up to nearly 300kb.

 

Is there any reason for my quality loss when attempting to encode/decode using base64?

Edited by crittope
Link to comment
Share on other sites

Not with base64-encoding/decoding alone, no. There must be some other condition that causes the quality loss, and as such posting your code (this is the PHP Coding Help section, after all) would go a long way in finding out any potential reasons.

 

Though, why do you want to save the images themselves in the database? Better to store the images in the file system, and only save the name/path to them in the database.

Link to comment
Share on other sites

I agree 100% with what Christian F. said (for a change ;) ). But, I have to ask, why are you using base64-encode to store the data? That increases the amount of data to be stored by about 37% - so a 300KB image will take about 411KB of space in the column -- plus the standard column / table overhead. If you must store image data (or any other binary data) why not just store it as, oh, I don't know ... binary data? (Google "BLOB" -- Binary Large Object)

Link to comment
Share on other sites

I agree 100% with what Christian F. said (for a change ;) ). But, I have to ask, why are you using base64-encode to store the data? That increases the amount of data to be stored by about 37% - so a 300KB image will take about 411KB of space in the column -- plus the standard column / table overhead. If you must store image data (or any other binary data) why not just store it as, oh, I don't know ... binary data? (Google "BLOB" -- Binary Large Object)

 

The base64-encode was a last ditch effort in an attempt to find a working solution to my problem. It didn't help. Here is some of the code that I'm using in an attempt to achieve my goal. This function just retreives the image from the inputted url. Clearly, when I'm using this I have a valid image url.

 

$img = FetchImage("http://example.com/no/image/here.jpg");
function FetchImage($url)
{
$fetch = curl_init();
curl_setopt($fetch, CURLOPT_URL, $url);
curl_setopt($fetch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($fetch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($fetch, CURLOPT_FORBID_REUSE, true);
curl_setopt($fetch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($fetch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($fetch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($fetch);
curl_close($fetch);
return base64_encode($data);
}

 

After this function is called the binary image data is inserted into my database as a blob (which I know is getting inserted correctly as I'm using prepared statements and using send_long_data for the image).

 

I have tested echoing out the binary image data right after downloading it and the image showed up perfectly, but once it was inserted into the database it became blurry and fragmented (top half or bottom half is just missing).

 

Here is my mysql insert query:

if($query = $database->connection->prepare("INSERT INTO media (hidden, hidden, hidden, hidden, hidden, hidden, hidden, image, hidden) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"))
{
$query->bind_param("sbssssbbs", $hidden, $hidden, $hidden, $hidden, $hidden, $hidden,$hidden, $tmp['image'], $hidden);
$query->send_long_data(7, $tmp['image']);
$query->execute();
$result = $query->affected_rows;
if(isset($query->error))
 printf("Error Message: %s<br>", $query->error);
$query->close();
}

 

Suggestions?

Link to comment
Share on other sites

We'll need to see the bit of code that selects and displays the picture as well, sounds like the problem might be located in there.

I also recommend verifying that the data inserted into the DB is exactly the same as the data you retrieved.

 

Also, be careful with what you edit/anonymise in your code. You could potentially be hiding the actual problem.

 

David: How boring wouldn't it be if we agreed on everything, no? ;)

Edited by Christian F.
Link to comment
Share on other sites

I have tested echoing out the binary image data right after downloading it and the image showed up perfectly, but once it was inserted into the database it became blurry and fragmented (top half or bottom half is just missing).

 

Did you test base64_decoding the data and then displaying the image right after retrieving it? The encode/decode process should not change the image one iota. Just to confirm, write the raw image data to a file, then encode it, decode it, and write the processed image data to a different file. The two files should be identical.

 

Be aware, that besides the "send_long_data" and the packet size settings for the database server (and client), you have to consider the size of the data coming back from the database server as well. Conceptually, I understand this process; but I don't store large binary data in the db, so I don't have a lot of experience with it. I'm pretty sure I did it once, just to play with the concept, but I don't think I have any of that code laying around, or know where to start looking for it.

 

If you know the data is good when you retrieve it, you could use the mysql command-line, or phpmyadmin, to retrieve the row, copy the encoded data, stick it in a short script to decode and display it, and see if that is good. This will confirm that the stored data is correct.

 

How is the image column defined? A BLOB or TEXT column will hold only 2^16 bytes (roughly 64K). So it will need to be a MEDIUMBLOB or larger to hold 300K

 

Christian is correct, again, the problem could be in the retrieval process.

 

 

Christian: One of us would be superfluous, then. Hey, does that mean I could take a vacation? :happy-04:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.