Is it possible to generate interlaced images using phpthumb? Even if phpthumb doesn't generate interlaced images is there a safe walk-around that can be used to achieve this on a webapp that uses phpthumb for image processing?
I tried finding out if phpthumb has this feature and all i could come up with was this configuration directive in phpThumb.config.php file:
$PHPTHUMB_CONFIG['output_interlace'] = true; // if true: interlaced output for GIF/PNG, progressive output for JPEG; if false: non-interlaced for GIF/PNG, baseline for JPEG.
I set mine to true and it is still not generating interlaced images.
I was wondering this myself and checking the phpthumb source looks like for the GD library it is only doing the interlacing when (1) calling the OutputThumbnail() method and (2) when using ImageMagick, the ImageMagickThumbnailToGD() method. Which won't get called as far as I can tell when your using the class and calling GenerateThumbnail()
A patch has been pushed to fix this - Now both OutputThumbnail and RenderToFile will generate a properly interlaced JPEG if the setting was enabled (true)
Related
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.
Would it be possible somehow to pass image resource to Imagick class on initialization instead of passing the file name? For example:
$imageResource = imagecreatefromjpeg('test.jpg');
$imagegickObj = new Imagick($imageResource);
Because I have a class that manipulates images with GD library. And I need some functionality from Imagick class as well. So it would be great that the image once opened
could be passed as a resource even to Imagagick class on init.
Your help would be appreciated.
Short version, no. The resource is just the underlying program data structure that GD uses to store the image while it is memory. There is no way to pass this directly to Imagick to have it open it as an image.
Theoretically you could output it is as a PNG using the imagepng() function, capture it with an output buffer, and then feed it to Imagick, however this would be a bit silly. You'd almost certainly be better off by just writing it to a temp PNG* file and then re-reading it. Or converting your current library to use Imagick everywhere.
*Don't use JPEG for this - you will lose image quality as it is a lossy format.
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.
So I am using php/oracle to create a PDF via the FPDF class/plugin. I am using the Mem_Image class/script to add a blob image to my PDF, but I am having trouble determining the size of the image. I am running PHP 5.2 so getimagesizefromstring isn't working.
For the image it will have a fixed height that I am able to set in the FPDF class, but the width since it could be portrait - or - landscape image, I'll need to scale the image proportionally.
Use imagecreatefromstring and imagesx.
getimagesize() does work with stream wrappers. Unfortunately, php://memory doesn't give you a way to reference it by filename. Either create your own stream wrapper that work smarter or use the VariableStream example.
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']); .....