0

I have finished cropping and resizing an image that user uploaded to my server, now I want to insert it to the database. How should i do?

My problem is can't find the real data to insert to 'data' field.

// File and new size
// May the user upload from POST
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width*$percent;
$newheight = $height*$percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);

// What is actually data that I've to insert to 'data' field?
// I think $thumb but it wasn't.

$query = "
INSERT INTO `image` (
    `name`, `mime`, `size`, `data`, `created`, `width`, `height`
)
VALUES (
     '{$name}', '{$mime}', {$size}, '{....}', NOW(), '{$width}', '{$height}'
)";

Thanks for your help.

Marcus
  • 295
  • 4
  • 16
  • Not sure, but maybe you can do this: `$data = imagejpeg($thumb);` – Jonan Sep 14 '14 at 14:47
  • 1
    @Marcus - Did you read [this post](http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay)? I know what you're actually asking but it's sometime good idea to changing the way we're doing things while trying to find an answer to it so if I was you I would go for saving path in DB instead based on my experience. – BentCoder Sep 14 '14 at 14:49

0 Answers0