开发者

working with image from imagecreatefromstring PHP

So i have an iphone app that that uploads an image to my webserver and i looked around and people seem to be doing something like

$data = file_get_contents($_FILES['file']['tmp_na开发者_运维问答me']);
$image = imagecreatefromstring($data);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);

I looked at the php docs, but i still don't understand what the header() does; does it convert the image into whatever format i want?

And for the imagepng(), where is the image outputted to? memory? is that why i need the imagedestroy()?

and where would i put in

move_uploaded_file()

Thanks in advance!


This code is intended to return as output an image - you could use it as a valid src for an image tag. That is, you could do this:

<img src="thatfile.php?something=1" />

The headers tell the browser that the data the server is going to send is an image (a PNG image, specifically).

In your example code, the file never actually gets written anywhere: the data stays in memory until the script ends, then it is simply "forgotten". imagedestroy frees up the memory and is good practice, but it really isn't necessary since the memory will be garbage collected after the request ends. If you want to preserve the image in a file, you'd have to use one of the related functions such as imagepng: http://www.php.net/manual/en/function.imagepng.php. The only difference between writing the file or not in your example code is the lack of a second argument for imagepng - second argument would be the desired file path.

It would help to read through the docs on this entire subject to gain a firm grasp of how these functions work and what each does. There are plenty of demos on the doc pages that show this in action.


This particular example gets the image uploaded through POST from the $_FILES array and simply outputs it back to the browser. The header is there to inform the browser that the content following is a PNG image.

Since you create an image from a string, it doesn't have "an extension". It's just an image resource at this point. You can create an actual file from it using imagepng, imagejpeg or any of the other methods to save an image resource to a file. You decide the extension (and file name) at that stage yourself.

E.g.:

imagepng($image, 'path/to/file.png');

and where would i put in move_uploaded_file()?

You wouldn't, since you don't have an uploaded file, only a string.


Header is purely for the server to let the browser know "Oh hey this is a png image please render it so"

imagepng encodes it into the png format and "prints" to the output

imagedestroy frees the memory taken by the image resource.

If you need to force extension you can use mod_rewrite

Here's a sample couple lines from my .htaccess:

RewriteEngine on
RewriteRule images/000000/00FF00/newmyinfo.jpg images/newmyinfo.php?bgcolor=000000&color=00ff00  [L]

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜