Getting string from image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Getting string from image

Post by MathewByrne »

Hi,

Been trying to find a way of inserting an uploaded image into a database. The image is resized before being put into the sql database.

What I need is some way to convert the image to a string (bascially the reverse of imagecreatefromstring()). Any ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Post by MathewByrne »

Yeah not what I'm really looking for, file_get_contents takes a file and converts to a string, I already opened the file into an image resource and resized it.

I know I could save it to a temp file but I'd prefer a direct method if one exists.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Unfortunately (correct me if I'm wrong), you'll have to use output buffering.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There is no such function. You have to output the image data via one of the output streams such as imagepng(). You can, however, capture the output via output buffering (ob_start())
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

The reason you can't go right from image resource to a string is because strictly speaking, the data of an image resource isn't a picture. When you call imagejpeg(), for example, GD converts the binary data of the image resource into the format of a JPEG file. If you go right from an image resource to a string, you're just gonna get a garbled mess.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

but there's no reason they can't make a version of imagejpeg that returns instead of outputs...
Post Reply