Passing image resource to Imagick class on initialization - php

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.

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.

Can phpthumb generate interlaced images for progressive browser display?

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)

PHP - get blob image size - FPDF - getimagesizefromstring doesn't work

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.

Why there is imageCreateFrom* if there is imageCreateFromString?

I really can't understand why GD has different function for loading images such like:
imagecreatefromjpeg()
imagecreatefrompng()
imagecreatefromgif()
While there is a single function if the image is string?
imagecreatefromstring()
Indeed it's much better to read the image into the string and pass it to the function, something like:
$imgBlob = file_get_contents($imagePath);
imagecreatefromstring($imageBlob);
unset($imgBlob); //> Free memory, but I am not interested in memory consumpation
? Or I am missing something ? This could led to potential confusion for new users
Maybe they just forgot to create a function imageCreateFromFile()?
Ps. Of course I am not interested about memory consumation using the file_get_contents method
imagecreatefromstring() runs a switch against the passed image type, checks if your system has support for that image type, and then actually runs the correct imagecreatefrom* function.
You can check out the source code to see this: https://github.com/php/php-src/blob/master/ext/gd/gd.c?source=cc (line 2280 for the function, line 2301 where it switches on the image type and calls the correct function).
So, the imagecreatefromstring() function is really just a helper wrapper. You'll get a very slight benefit from not running _php_image_type (line 2217) if you call the actual image type function.
imagecreatefromjpeg()
imagecreatefrompng()
imagecreatefromgif()
create image resource from a file - you pass file path as parameter and that's the only acceptable input.
imagecreatefromstring()
creates image resource from string, not file - it could be virtually anything, you can even type in a content. For example you can use
imagecreatefromstring(base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw=='));
to get 1x1 pixel transparent gif (useful for tracking gifs)
Granted, you could pass everything through imagecreatefromstring, but it would not be memory efficient - handling large images takes a lot of memory and with low memory limit that makes a huge difference.

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.
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.

Categories