question about imagejpeg

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
Ben48
Forum Newbie
Posts: 5
Joined: Wed May 20, 2009 3:24 pm

question about imagejpeg

Post 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.
Last edited by Ben48 on Wed May 20, 2009 6:20 pm, edited 2 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: question about imagejpeg

Post 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.
Ben48
Forum Newbie
Posts: 5
Joined: Wed May 20, 2009 3:24 pm

Re: question about imagejpeg

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: question about imagejpeg

Post by Benjamin »

Ben48
Forum Newbie
Posts: 5
Joined: Wed May 20, 2009 3:24 pm

Re: question about imagejpeg

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: question about imagejpeg

Post 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.
Ben48
Forum Newbie
Posts: 5
Joined: Wed May 20, 2009 3:24 pm

Re: question about imagejpeg

Post by Ben48 »

Awesome!!!

Thanks so much :D
Post Reply