2

Save image to database

$query =$myClass->query("insert into tblImage(document) values(".base64_encode(file_get_contents($_FILES['imageFile']["tmp_name"])).")");   

Show image from database

$image =$myClass->query("select document from  tblImage where code=1");                                                                             
$output_bin_string = base64_decode($image[0]['thumbDocument']);
header("Content-Type: image/jpeg");
header("Content-Length: " . strlen($output_bin_string));          
echo $output_bin_string;    

I can save image to file and then save to database

$image=$myClass->query('select document form tblImage where code=1' );   
$source = imagecreatefromstring(base64_decode($image[0]["document"])) ;
$rotate = imagerotate($source,$degrees,0);
imagejpeg($rotate,'tmp/1.jpg');
$image =$myClass->query("update tblImage set document='".base64_encode(file_get_contents('tmp/1.jpg'))."'   where code=1");

Question: there is way that rotate image and save to database without save image to file like this

$image=$myClass->query('select document form tblImage where code=1' );   
$source = imagecreatefromstring(base64_decode($image[0]["document"])) ;
$rotate = imagerotate($source,$degrees,0);
$image =$myClass->query("update tblImage set document='".base64_encode($rotate)."'   where code=1");

This code say error:base64_encode() expects parameter 1 to be string, resource given

ashkufaraz
  • 5,179
  • 6
  • 51
  • 82

2 Answers2

3

Saving files in a database is a bad practice and performs pretty bad when comparing to native file system access or another storage backend like S3. Have a look at this question: Saving images in database mysql

Your approach will cause the data to go through the DB, php and the webserver before it goes to the client. While saving it directly in the file system and keeping a reference in the DB to the file will allow you to send it directly through the webserver when it is requested.

A proper approach is to have one table, lets call it file_storage for example, in the application that keeps the references to files. Then associate the rows as needed: User 1:1 Avatar, User 1:n Photo, Photo n:n Gallery for example. This is DRY, maintains SoC and even allows me to store files in different backends. You can read as well the documentation of this plugin for CakePHP that does exactly what I just described.

For image manipulation I recommend using the Imagine library. Here is an example taken from the documentation:

use Imagine\Image\Box;
use Imagine\Image\Point;

$image->resize(new Box(15, 25))
   ->rotate(45)
   ->crop(new Point(0, 0), new Box(45, 45))
   ->save('/path/to/new/image.jpg');
Community
  • 1
  • 1
floriank
  • 25,546
  • 9
  • 42
  • 66
2

You should first convert your image resource to a string (by buffering the output into a variable), and then save it to your database like this:

// ... your previous code
$rotate = imagerotate($source,$degrees,0);
// New code starts here
ob_start();
imagejpeg($rotate);
$imageData = ob_get_clean(); 
$image =$myClass->query("update tblImage set document='".base64_encode($imageData)."'   where code=1");

Note that this must be done before sending any output to the client, otherwise ob_start() will fail.

uri2x
  • 3,162
  • 1
  • 11
  • 25