itsachen
itsachen

Reputation: 199

PHP GD Library and uploaded files

I'm working on a project where I upload an image (jpg) and manipulate it using the PHP GD library.

I know that I can use GD functions to edit an image resource (created from imagecreatefromjpeg()) but I was wondering if there was a way I could use the file uploaded in the $_FILES array directly with the GD library. One solution I thought of was saving the uploaded file, pushing it into imagecreatefromjpeg, then deleting it afterwards.

This seems cluinky though, is there a more efficient solution?

I'm still a bit new to PHP so I'm not sure as to how files are stored in the $_FILES array. I hope I'm making sense here. Thanks.

Upvotes: 4

Views: 3947

Answers (1)

deceze
deceze

Reputation: 522015

You can simply do this:

$img = imagecreatefromjpeg($_FILES['image']['tmp_name']);
// do gd operations on $img
imagejpeg($img, '/path/to/target');

You'll have to use imagecreatefrom in some form or another, and you can use it directly on the uploaded file. Then just save the result of your manipulations using imagejpeg. The uploaded file in tmp_name will we thrown away automatically.

Having said that, you should save the original somewhere. It's always good to have it around for later use.

Upvotes: 7

Related Questions