PHP GD Library and uploaded files - php

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.

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.

Related

php How to reduce file size using gd and upload to folder [duplicate]

I have a site with about 1500 JPEG images, and I want to compress them all. Going through the directories is not a problem, but I cannot seem to find a function that compresses a JPEG that is already on the server (I don't want to upload a new one), and replaces the old one.
Does PHP have a built in function for this? If not, how do I read the JPEG from the folder into the script?
Thanks.
you're not telling if you're using GD, so i assume this.
$img = imagecreatefromjpeg("myimage.jpg"); // load the image-to-be-saved
// 50 is quality; change from 0 (worst quality,smaller file) - 100 (best quality)
imagejpeg($img,"myimage_new.jpg",50);
unlink("myimage.jpg"); // remove the old image
I prefer using the IMagick extension for working with images. GD uses too much memory, especially for larger files. Here's a code snippet by Charles Hall in the PHP manual:
$img = new Imagick();
$img->readImage($src);
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(90);
$img->stripImage();
$img->writeImage($dest);
$img->clean();
You will need to use the php gd library for that... Most servers have it installed by default. There are a lot of examples out there if you search for 'resize image php gd'.
For instance have a look at this page http://911-need-code-help.blogspot.nl/2008/10/resize-images-using-phpgd-library.html
The solution provided by vlzvl works well. However, using this solution, you can also overwrite an image by changing the order of the code.
$image = imagecreatefromjpeg("image.jpg");
unlink("image.jpg");
imagejpeg($image,"image.jpg",50);
This allows you to compress a pre-existing image and store it in the same location with the same filename.

Validate an image in PHP

I'm using GD to create jpegs from files that a user uploads.
What is the best way to validate that the image the user has uploaded is valid?
By valid I mean that the file is not a corrupt image that GD won't like, I do extension testing client side so they can only upload jpegs/gifs/pngs.
Thanks
You could use getimagesize. It will return FALSE if the image could not be loaded. It has support for most image types.
getImageSize would be your best choice but, be careful, if the file results are not valid you will get a warning. Using # before built in imagesize function will be ideal.

Image upload security - reprocess with GD

I've heard that the best way to handle uploaded images is to "re-process" them using the GD library and save the processed image. see: PHP image upload security check list
My question is how do this "re-processing" in GD? What this means exactly? I don't know the GD library very well and I'm afraid I will mess it up...
So if anyone who did this before could you give me an example for this?
(I know, another other option is to use ImageMagick. For ImageMagick I found an answer here: Remove EXIF data from JPG using PHP, but I can't use ImgMagick now. By the way.. removing EXIF data means completely recreate the image in this case?)
(I'm using Zend Framework if someone interested.)
If the user uploads a JPEG file, you could do something like this to reprocess it:
$newIm = #imagecreatefromjpeg($_FILES['file']['tmp_name']);
if (!$newIm) {
// gd could not create an image from the source
// most likely, the file was not a valid jpeg image
}
You could then discard the $newIm image using imagedestroy() and use the uploaded file from the user, or save out the image from GD and use that. There could be some issues with saving the GD image as it is not the original image.
Another simple method would be to check the header (first several bytes) of the image file to make sure it is correct; for example all JPEG files begin with 0xff 0xd8.
See also imagecreatefromstring(), and you can also use getimagesize() to run similar checks on the uploaded image.
function isvalidjpeg($file)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
return is_resource($finfo) &&
(finfo_file($finfo, $file) === 'image/jpeg') &&
finfo_close($finfo);
}
if(isvalidjpeg($_FILES['file']['tmp_name'])) {
$newIm = #imagecreatefromjpeg($_FILES['file']['tmp_name']); .....

How to save a phpthumb output into a file?

I am trying to save PhpThumb output. As what I could find on-line was not sufficient or too complex, I would like to ask if any one knows how to it?
$thumb_src="\"phpThumb/phpThumb.php?src=../apartmentsPhotos/".$num['ref']."/1.JPG&h=119&q=100\"";
echo" '<'img src=".$thumb_src />";
So what I want to do is to save the img src into an Image.
(So far I was creating the thumbnails on the fly but it seems that google and my web server donĀ“t like it too much. Saving the thumbnails will ensure that in no time I will have all my thumbnails in real files and then I will use this function just for new content.)
From phpThumb's FAQ
The best way is to call phpThumb as an object and call RenderToFile() to save the
thumbnail to whatever filename you want. See /demo/phpThumb.demo.object.php for an example. The other way is to use the 'file' parameter (see /docs/phpthumb.readme.txt) but this parameter is deprecated and does not work in phpThumb v1.7.5 and newer.
Once you have generated the URL with this line you posted:
$thumb_src="\"phpThumb/phpThumb.php?src=../apartmentsPhotos/".$num['ref']."/1.JPG&h=119&q=100\"";
Pass it as a $_GET variable to another page, call it serveThumb.php:
if (!isset($_GET['img']))
exit;
header('Content-type: application/pdf');
echo file_get_contents($_GET['img']);
You might have to add your own validation to serveThumb.php. Now you can save the result of serveThumb.php as a JPG.
Alternatively, save the contents of the image as a JPG file.
if (!isset($_GET['img']))
exit;
$img = file_get_contents($_GET['img']);
file_put_contents("myImage.jpg", $img);

Preferred way to copy JPG files from a remote server using PHP

I'm using PHP to copy JPGs from a remote server to my own server. Is it best to simply use the copy() function, or are the jpeg-specific functions better? For example:
$copy = copy($remote_url, $dest_file);
-OR-
$img = imagecreatefromjpeg($remote_url);
$copy = imagejpeg($img, $dest_file);
imagedestroy($img);
What would the best option be in terms of speed and memory load? Also, would there be any difference in the resulting image quality? I should add that this script is required to copy a large number of photos (typically hundreds, but sometimes it may be a couple thousand).
Thanks, Brian
if all you want is a copy, copy() is better.
using the gd library functions (imagecreatefromjpeg/imagejpeg) will end up re-compressing the image (probably, maybe it's smart enough not to, but probably). If you wanted to convert the images to .png or something, then you'd want to use gd (or ImageMagick)

Categories