开发者

PHP GD Library and uploaded files

开发者 https://www.devze.com 2023-03-16 10:29 出处:网络
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())

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开发者_StackOverflow 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.


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.

0

精彩评论

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