PHP中的配置文件图片保存选项

I have created a app in php. How i can save the profile pictures of the users? Will that be a good idea to save them in db using base64 encode, like this

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAWgBaAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAAB..." />

or anything else

Store images in your server and store the path to images in the database. This will reduce extra overload on database server.

It's a good idea/practice usually only for very small CSS images that are going to be used together (like CSS sprites) when IE compatibility doesn't matter, and saving the request is more important than cacheability.

It has a number of notable downsides:

  • Doesn't work at all in IE6 and 7.

  • Works for resources only up to 32k in size in IE8. This is the limit that applies after base64 encoding. In other words, no longer than 32768 characters.

  • It saves a request, but bloats the HTML page instead! And makes images uncacheable. They get loaded every time the containing page or style sheet get loaded.

  • Base64 encoding bloats image sizes by 33%.

  • If served in a gzipped resource, data: images are almost certainly going to be a terrible strain on the server's resources! Images are traditionally very CPU intensive to compress, with very little reduction in size.

Its better to store file path in database rather storing files in databases. Datbases are for structured data, not for blobs. Moreover database storage is usually more expensive than file system storage. Servers doesn't need any special coding or processing to access images in the file system

Copy base64 image "src" to a textbox using javascript. Post a form with base64 url textbox. Its better to save url in database rather saving a file in database. You can save your base64 image by using this PHP code:

    <?php
define('UPLOAD_DIR', 'images/');
$base64img=$_POST['base64img'];
if (stristr($base64img, "data:image/jpeg;base64,")) {
$base64img = str_replace('data:image/jpeg;base64,', '', $base64img);
$uid=time();
$data = base64_decode($base64img);
$file = UPLOAD_DIR . $uid.'.jpg';
file_put_contents($file, $data);
}elseif (stristr($base64img, "data:image/png;base64,")) {
$base64img = str_replace('data:image/png;base64,', '', $base64img);
$uid=time();
$data = base64_decode($base64img);
$file = UPLOAD_DIR . $uid.'.png';
file_put_contents($file, $data);
}elseif (stristr($base64img, "data:image/jpg;base64,")) {
$base64img = str_replace('data:image/jpg;base64,', '', $base64img); 
$uid=time();
$data = base64_decode($base64img);
$file = UPLOAD_DIR . $uid.'.jpg';
file_put_contents($file, $data);
}
?>