Jump to content


Photo

how to create string from image?


  • Please log in to reply
3 replies to this topic

#1 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 20 April 2006 - 10:19 PM

Okay, here's a fun challenge. Users are uploading files of who knows what resolution. I am resizing them in PHP, but instead of dumping them to a file on the hard drive, i want to put them in a database.

At first I thougth this was very simple, but it appears that imagejpeg, imagegif, imagepng, etc, only dumps directly to a browser or file -- it won't return the image as a string. I've also tried referring to the image pointer
$im=imagecreatefromstring(file_get_contents($_FILES['userfile']['tmp_name']));
... resize image ...
mysql_query("INSERT INTO `table` (`image`) VALUES ('".mysql_real_escape_string($im)."')");
without success

It looks like I will need to write the image to a file, then reload the file using file_get_contents. Is there a shorter method?

Thanks!

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 20 April 2006 - 10:43 PM

Use blob as the data type.

Example script - [a href=\"http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html\" target=\"_blank\"]http://www.wellho.net/solutions/php-exampl...e-retreive.html[/a]
Legend has it that reading the manual never killed anyone.
My site

#3 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 20 April 2006 - 10:59 PM

The database works great if I don't resize the images (using mediumblob for the data type). The problem arises when I resize the images to 640x480. I can display or save the image to the hard drive -- but I'm having difficulty injecting it into mysql.

Once I've captured and resized the file, I could save it to the hard drive, then reopen it using file_get_contents() -- but that seems like a really long way around.

Looking for the right shortcut.

Thanks!

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 21 April 2006 - 01:19 AM

update:

I have applied the workaround -- which works great. PHP is now creating a temporary image in the /tmp/ folder, then reimporting it using file_get_contents. If anyone knows of a way to remove this step, they would be my hero.
$im=imagecreatefromstring(file_get_contents($_FILES['userfile']['tmp_name']));
... resize image ...
imagepng($im,"/tmp/tempimage.png");
mysql_query("INSERT INTO `table` (`image`) VALUES ('".mysql_real_escape_string(file_get_contents("/tmp/tempimage.png"))."')");





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users