ppp
ppp

Reputation: 2055

Create thumbnails from mysql stored images

I have a table named users and each row (user) has an image.

The image is stored as a BLOB in a mysql database using this code:

$filename = $_FILES['image']['tmp_name'];
$size = getimagesize($filename);
$handle = fopen( $filename , "rb" );
$content = fread( $handle , filesize( $filename ) );
fclose( $handle );
unlink($filename);
$image = base64_encode( $content );
//  .... send query to database ....

This worked at first, but as more and more images have to be shown at each page, it gets really loaded, so now I want to create thumbnails, saved in the server's filesystem, using php GD.

I have the following code but I have no idea what to do in order to read the image from the database so I can create its thumbnail.

$query = "SELECT * FROM users;";
$result = mysql_query( $query );
while( $row = mysql_fetch_array( $result ) ) {
    $uploaded_image = base64_decode( $row['image'] );
    $size = getimagesize($uploaded_image);  <--------------------
    $dimension_x = 73;
    $dimension_y = 73;
    $directory = 'views/images/generated/people/';
    do{
    $filename = random_32();
    $filename = $directory.$filename.'.jpg';    
    } while( file_exists($filename) );
$image = imagecreatefromjpeg( $uploaded_image );
$thumb = imagecreatetruecolor( $dimension_x , $dimension_y );
$size = getimagesize( $uploaded_image );
imagecopyresampled( $thumb , $image , 0 , 0 , 0 , 0 , $dimension_x , $dimension_y , $size['0'], $size['1']);
imagejpeg( $thumb , $filename , 100);
}

I am trying to find what should I put where the arrow is to make the script work.

All it does now is output

Warning: getimagesize(ÿØÿà): failed to open stream: No such file or directory in /var/www/play/ns4/models/create_thumbs_people.php on line 12

For every entry in the database.

---- EDIT ----

Forgot to mention, random_32() is a function that generates a 32 characters long random string so that the images are named.

Upvotes: 1

Views: 2394

Answers (2)

Patrick Shafer
Patrick Shafer

Reputation: 996

You're on the right track, the issue is that $uploaded_image is the content of the image and not a file path. getimagesize requires a file name as the first argument.

Conceptually, here's the change that needs to be made:

  • write the content of $uploaded_image to a temporary location
  • call getimagesize on that temporary location

Here's some example code (untested):

$uploaded_image = base64_decode( $row['image'] );  //this is from your code above, should work fine
$uploaded_temp_file = tempnam(sys_get_temp_dir(), 'user_image');    //generate a temporary file name in your OS's temp folder
$uploaded_temp_res = fopen($uploaded_temp_file, 'wb');    //open the file for binary write
fwrite($uploaded_temp_res, $uploaded_image);   //commit binary data to the file
fclose($uploaded_temp_res);     //close the file handle
$size = getimagesize($uploaded_temp_file);      //finally, get the image size

Note that you'll want to add error handling and what not.

Upvotes: 2

Marc B
Marc B

Reputation: 360572

And here's one of the many reasons why storing files in the database is a bad idea...

getimagesize() works on files, not strings. Since you've got the raw image in a string, use [imagecreatefromstring()][1], then the imagesx() and imagesy() functions to get the dimensions:

while( $row = mysql_fetch_array( $result ) ) {
    $uploaded_image = base64_decode( $row['image'] );
    $image = imagecreatefromstring( $uploaded_image );
    $uploaded_x = imagesx($image); // X dimension
    $uploaded_y = imagesy($image); // Y dimension
    etc....

Note that imagecreatefromstring actually has a bit of smarts and can figure out what type the image is, unlike the quite moronic createfromgif/jpg/png/etc... functions, which only work on those particular file types. [1]: https://www.php.net/imagecreatefromstring

Upvotes: 3

Related Questions