Slowness Found When Base 64 Image Select and Encode from Database

Slowness found when base 64 image select and encode from database

Since it's just personal files, your could store them in S3.

In order to be safe about file uploads, just check the file's mime type before uploading for whatever storage you choose.

http://php.net/manual/en/function.mime-content-type.php

just run a quick check on the uploaded file:

$mime = mime_content_type($file_path);
if($mime == 'image/jpeg') return true;

no big deal!

keeping files on the database is bad practise, it should be your last resource. S3 is great for many use cases, but it's expensive for high usages and local files should be used only for intranets and non-public available apps.

In my opinion, go S3.

Amazon's sdk is easy to use and you get a 1gb free storage for testing.
You could also use your own server, just keep it out of your database.

Solution for storing images on filesystem

Let's say you have 100.000 users and each one of them has 10 pics. How do you handle storing it locally?
Problem: Linux filesystem breaks after a few dozens of thousands images, therefore you should make the file structure avoid that

Solution:
Make the folder name be 'abs(userID/1000)*1000'/userID

That way when you have the user with id 989787 it's images will be stored on the folder
989000/989787/img1.jpeg
989000/989787/img2.jpeg
989000/989787/img3.jpeg

and there you have it, a way of storing images for a million users that doesn't break the unix filesystem.

How about storage sizes?

Last month I had to compress a 1.3 million jpegs for the e-commerce I work on. When uploading images, compress using imagick with lossless flags and 80% quality. That will remove the invisible pixels and optimize your storage. Since our images vary from 40x40 (thumbnails) to 1500x1500 (zoom images) we have an average of 700x700 images, times 1.3 million images which filled around 120GB of storage.

So yeah, it's possible to store it all on your filesystem.

When things start to get slow, you hire a CDN.

How will that work?

The CDN sits in front of your image server, whenever the CDN is requested for a file, if it doesn't find it in it's storage (cache miss) it will copy it from your image server. Later, when the CDN get's requested again, it will deliver the image from it's own cache.

This way no code is needed to migrate to a CDN image deliver, all you will need to do is change the urls in your site and hire a CDN, the same works for a S3 bucket.

It's not a cheap service, but it's waaaaay cheaper then cloudfront and when you get to the point of needing it, you can probably afford it.

Loading Base64 String into Python Image Library

SOLUTION:

Saving the opened PIL image to a file-like object solves the issue.

pic = cStringIO.StringIO()
image_string = cStringIO.StringIO(base64.b64decode(request.POST['file']))
image = Image.open(image_string)
image.save(pic, image.format, quality = 100)
pic.seek(0)
return HttpResponse(pic, content_type='image/jpeg')

base64_decode return null when trying to upload image

so i figure it out:
1. the path that you need to save the image must be relative to the web service (php code in my case) in the server which also mean you have to save the image at the same server.
2. file_put_contents is a lot easyier to use and replace the fopen, fwrite and fclose together and save you trubble.
i hope it will help you guys

BLOB field in database or directly in the file systeme for 1Go files?

Storing you blob in database using a BinaryField will harm the performance of your application as your querysets will have to handle a lot more data. Best practice is to store blobs in separate files and only store the file path - using models.FilePathField() field - in your database and serve the actual image as static file. See this post for more details.

Image not being Uploaded in MySQL

Please change enctyp to enctype in your HTML form.
Also update code
From

$image= addslashes($_FILES(['image']['tmp_name']));   

To

$image= addslashes($_FILES['image']['tmp_name']);

And
This line of code, From

$name= addslashes($_FILES(['image']['name']));

To

$name= addslashes($_FILES['image']['name']);

Complete code is given below...

<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />

<input type="submit" name="sum" value="upload" />
</form>
</body>
</html>
<?php
include('connection.php');

if($_POST){
if(empty($_FILES) || !isset($_FILES['image'])){
echo "Please Add an Image";
}else{
if(getimagesize($_FILES['image']['tmp_name'])){

$image= addslashes($_FILES['image']['tmp_name']);
$name= addslashes($_FILES['image']['name']);
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($name, $image);
}
}
}

function saveimage($name, $image){
$con= mysql_connect("localhost", "root", "");
mysql_select_db("hw2", $con);
$qry= "insert into images (name, image) values ('$name', '$image')";
$result= mysql_query($qry,$con);

if($result) {
echo "<br/> Image Uploaded.";
}else{
echo "Not Uploaded";
}
}
?>

I hope this code will works for you.



Related Topics



Leave a reply



Submit