Page 1 of 1

question about imagejpeg

Posted: Wed May 20, 2009 3:29 pm
by Ben48
Hey guys,

Quick question. Here is a piece of code that WORKS and outputs a given image:

Code: Select all

<?php
 
header('Content-Type: image/jpeg');
 
echo base64_decode($_POST["image"]);
 
?>
Next, this piece of code I want to do the same thing, but I think I'm using the wrong technique:

Code: Select all

<?php
 
header('Content-type: image/jpeg');
 
$image = base64_decode($_POST["image"]);
 
imagejpeg($image); //imagejpeg($image, 'simpletext.jpg');
 
imagedestroy($image);
  
?>
Eventually, I'm wanting to be able to save the image to a location on my server. That's why I'm trying to get the "imagejpeg" to work, because that seems to be able to save an image to the server.

Thanks for any help.

Re: question about imagejpeg

Posted: Wed May 20, 2009 4:13 pm
by Benjamin
How is the image being posted? File uploads will be in the $_FILES array. Your form must have the correct enctype. The image can then be saved directly to disk unless you need to resize it.

Re: question about imagejpeg

Posted: Wed May 20, 2009 4:49 pm
by Ben48
Hmm, so... I'm using this script in a flash file:
http://www.adobe.com/devnet/flash/artic ... phics.html

Then, it "outputs" the image file to the browser. Basically, if I can get it to save somewhere, I'll be satisfied.

What do I need to look into?

Also, notice how the base64 decode is necessary for the image to display.

Re: question about imagejpeg

Posted: Wed May 20, 2009 5:03 pm
by Benjamin

Re: question about imagejpeg

Posted: Wed May 20, 2009 6:18 pm
by Ben48
Hi,

So... I have the Flash application which is basically generating a random image and sending it to the PHP script. I don't know of any way to set the enctype for the form there, because there isn't really a form.

As you see in my first code example, I have the image displaying using echo. Isn't there some other way I can do this?

I've looked at the imagejpeg page on the PHP site, and example 2 is exactly what I'd like to do (with regards to the "saving" part):

http://us2.php.net/manual/en/function.imagejpeg.php

Thanks so much! I really appreciate any help you can give.

Re: question about imagejpeg

Posted: Wed May 20, 2009 7:00 pm
by Benjamin
If the flash software is sending the image as a base64_encoded POST variable, simply base64_decode it and use file_put_contents() to save it to disk.

Re: question about imagejpeg

Posted: Wed May 20, 2009 7:08 pm
by Ben48
Awesome!!!

Thanks so much :D