I recently started using imagemagick with php and
I'm relatively new with both of these, IM and PHP.
So, I'm here to ask for some help/suggestion(s).
First
If lets say a user uploads a gif or a png image on
my site and I want it converted to jpg, is there any
command like for example.$image->convert($file,'.jpg)
or the convert command is accesible only thru exec() ?
like for example exec(convert picture.png to picture.jpg)
Second
What if for again, the user uploads gif/png on the site
and I resize the image to a specified width/height and
write the image, with the writeImage function of IM like this:
$image->writeImage(basename.$ext) where $ext = jpg.
Will this work properly,is this a good practice? I assume this will
only rename it but still, I don't see a problem in this... o.O
Oh sorry one more question, I'm not very familiar with exec,
is it better using imagemagick using exec(), does it improve
speed, load etc?
I can't answer your questions directly but thought I point you to a few resources:
Regarding which is better, exec or the PHP extension, I asked this same question a few months ago:
Should I use a PHP extension for ImageMagick or just use PHP's Exec() function to run the terminal commands?
For all Image Magick PHP functions you should look up the official guide:
http://www.php.net/manual/en/book.imagick.php
I switched from Image Magick to Graphics Magick as I heard it has better performance. It is a fork of Image Magick with an emphasis on performance. Large sites like Flickr and Etsy use it:
http://www.graphicsmagick.org/
This guide got me started:
http://devzone.zend.com/1559/manipulating-images-with-php-and-graphicsmagick/
And they have their own manual on php.net:
http://php.net/manual/en/book.gmagick.php (most of the commands are identical to Image Magick's)
I prefer using exec() as it is supported a lot better than Imagick ( the example code you posted ), easier to use and supports all the operators ( depending on your version ) which again Imagick does not.
Some of the Imagick code works differntly in the different versions; there are a lot more Imagick examples around than there were a couple of years ago when I first started looking at it.
Saying that Imagick can be faster and is/can be? built into php although some people have trouble installing it.
I belive that this site http://valokuva.org/?cat=1 belongs to a Imagick developer and has lots of examples.
My site www.rubblewebs.co.uk/imagemagick has lots of examples of php with exec( )
As to your other two questions yes you can change the file type with Imagick and I am not sure about basename.$ext - why not try it? As long as basename does not have an extension it may work but you might need to include basename.$ext in quotes.
As to speed it dpends sometimes GD is faster other times Imagick or Imagemagick. You can always do some tests. I did some a year or so ago and you can try the code on your server. http://www.rubblewebs.co.uk/imagemagick/speed/Speed_tests_1.pdf
Related
Does anybody know how to write avif image files in php?
For webp files i have the function imagewebp. But avif?
Using PHP Version 7.4, 7.1 and 7.2 on different servers.
thanks a lot
EDIT:
It has been implemented in PHP 8.1 https://php.watch/versions/8.1/gd-avif
There is no dedicated function to do that at this moment. GD has implemented AVIF support but is not yet implemented in PHP. They are working on it https://bugs.php.net/bug.php?id=80828 , please vote for it.
Until that function is made, I image there can be other methods to create:
(1) using php exec/shell_exec and an command line app (like imagemagik). untested code:
if (function_exists("exec")) {
$command = "convert -size 600x600 canvas:white white.avif ";
exec($command, $output);
print("<pre>".print_r($output,true)."</pre>"); //debug
}
else {
echo "EXEC function is not available. See php.ini disabled_functions.<br />\n";
}
(2) using a php module like Imagik
https://www.php.net/manual/en/book.imagick.php
Verify ImageMagick installation
For both methods you need to make sure you have a recent version of ImageMagik compiled with proper libraries.
https://github.com/ImageMagick/ImageMagick/issues/1432
Unfortunately, there is no native PHP support (in any PHP version I know of) for the AVIF/AV1 image file formats, at this stage.
PHP normally relies on the GD, the ImageMagick, the GMagick (and Exif) image libraries, so - until these include support for the AVIF/AV1 image file formats, I'm afraid there won't be an easy (and "native") solution for you. There may be some third-party libraries, perhaps written in pure PHP, to support this, however the format is relatively new and not quite yet widely supported, so I believe it will take some time for these to be available, and a pure-PHP library for image processing is likely to be quite inefficient, performance-wise.
That said, I suppose you could consider using a workaround, which is PHP-generating an image in any of the other image formats (say JPEG or PNG), and then using a command-line tool to covert the resulting image file into AVIF/AV1?
See for example https://reposhub.com/rust/image-processing/kornelski-cavif.html
Good news - the work's been done to support AVIF in libgd, and this work has been propagated into PHP. It should be released in PHP 8.1.
Enjoy!
is there any way to connect GIMP with PHP and use its libraries?
It seems that all
i do my development use linux as a production server
example for resise image or croping
There is also the GD library which can be used with PHP directly.
http://php.net/manual/en/ref.image.php
imagecrop — Crop an image to the given rectangle
imagescale — Scale an image using the given new width and height
First thing: for resizing or cropping, you really should use some PHP library that integrates with Imagemagick, or something along that.
Now, to actually answer the question "is there any way to connect GIMP with PHP?" - The answer to that is "not directly".
Supposing you want to do extend use of Image manipulation algorithms for some serious project - GIMP engine nowadays is driven by GEGL (Generic Graphics Library) - which has support to binding with any other language through gobject introspection. Now,, gobject introspection can theoretically work with any language - but looking for Gobject Introspection for PHP yields some blog postos form 2011, and some early-stage projects on github. Therefore, although GEGL is what could provide you full image maniulation capabilities, I don't think it can be directly itegrated with PHP due to missing gobject integration. There should be a lot of ways of usig it indirectly, though - by creating some local web-service that would use GEGL to process operations described in XML, for example - such a service could be made in Python or C.
And, finally, for using GIMP's PDB itself, one could craft a Python script that would work as a local webservice relay to GIMP's PDB -so it is also possible in an indirect way.
I like the ImageMagick library, it is more powerful than the GD library. However it doesn't come pre-installed with php you have to do it manually, but if you can do it, it's easy to install with:
sudo apt-get install php5-imagick
The website: https://www.imagemagick.org
The PHP docs: http://php.net/manual/en/book.imagick.php
I need to do the following image manipulations for images uploaded by users on my site:
Resize images (if greater than a certain dimension)
Convert all image formats to jpg's
Add a watermark to the bottom of all images
Do I need to use either the MagickWand or iMagick extensions or can I just get away with running the terminal commands inside PHP's exec function?
Is there a reason why the PHP extensions would be preferred? Which would be faster and better for performance (this site may have lots of users and there would be a ton of image processing at any given time)?
I'd like to make a counterpoint to drew010's answer. In his answer he states:
You would benefit a lot using the PHP extensions instead of using exec
or similar functions. Built in extensions will be faster and use less
memory as you will not have to spawn new processes and read the output
back
For processing images, this is true. Well, I know for a fact that calling on ImageMagick binaries using PHP's exec() function (or similar functions) carries additional overhead above using embedded PHP libraries however I'm not clear on how much overhead there is.
But there's another side of the coin. If you embed the ImageMagick code into PHP via an extension, then every request using PHP takes more memory whether the request processes an image or not. If you're using Apache's mod_php then this becomes even more of an issue because now every request to your server has the ImageMagick libraries in memory even if it's serving an HTML file!
So it really depends. You really want to keep PHP's memory footprint as low as possible. If you're processing a large number of requests which require ImageMagic and you're using FastCGI or php_fpm, then you'll probably see a benefit to using embedded ImageMagick. But if you're only occasionally processing requests using ImageMagick and/or using Apache's mod_php then you may get much better performance calling ImageMagick via exec().
You would benefit a lot using the PHP extensions instead of using exec or similar functions. Built in extensions will be faster and use less memory as you will not have to spawn new processes and read the output back. The image objects will be directly available in PHP instead of having to read file output, which should make the images easier to work with.
If you have a busy site, creating lots of processes to edit images may start to slow things down and consume additional memory.
I always use PHP GD http://php.net/manual/en/book.image.php
You can accomplish resizing, converting to JPG and watermarking your images. I know your post said you have to use MagickWand or iMagick, but I just wanted to present this option in case it would work for you.
I have a folder with about 5000 images in it.
I can use php, or software on a local version of the images if required.
Whats the best way to go through and optimize/shrink their size/compress them as quickly and efficiently as possible to 20 width and 20 height?
Anything with a GUI that works on XP?
Tools like mogrify or convert (from ImageMagick) will do just fine, when it comes to resizing images.
After, if you want to optimize your images further (reducing their size in bytes), you should take a look at software like :
optipng or pngcrush for PNG,
or jpegtran for JPEG
If your goal is optimization for a website, take a look at Best Practices for Speeding Up Your Web Site - Images and the points which follow (I'm especially thinking about CSS Sprites)
Edit after edit of the OP : hu... a GUI ? No idea, sorry (I have those kind of tools run automatically, from scripts, on Linux servers).
Why not just write a script that will call those tools on all your images, and let it run for the night ?
Use either of imagemagick or GD2 .. GD2 is already bundled in the newer versions (5.0+) of PHP. This way, you won't have to install new extension.
This tutorial is what you are looking for.
I would like to extract a thumbnail from a RAW image file, like Canon's .CR2 or Nikon's .NEF. I've understood that this can somehow be done with ImageMagick, but haven't grasped if it's possible through the PHP wrapper.
Are there any good solutions? Preferably using the built in thumbnail for speed.
Yep, iMagick (the php version of ImageMagick) can handle these extensions: http://www.imagemagick.org/script/formats.php
Here's a great set of tuts that got me going with Imagick. The owner responded to a few of my questions quickly, and despite a bit of a language barrier was able to easily get me through my hurdles
As an aside, I've begun using Gallery to do image admin. No need to worry about thumbnailing, uploading, etc....it's all automatic. Then on the front end I can do jquery magic (getting photos via php query from the gallery database tables) to make it look really good.
Likely, if PHP's imagemagick libraries are to support this, they would be drawing from some functionality exposed through imagemagick's 'identify' command line tool (as the tool would be itself exposing functionality in the imagemagick libraries). Looking at the documentation for this tool, it doesn't look good. If you tried running identify -verbose, theoretically, the thumbnail information would appear in there somewhere, perhaps as an encoded value. Try it yourself: if it does, maybe you could possibly further extract the information returned from identify, either through the imagemagick functions in PHP (though I don't see any past the Exif libraries which only work on JPEG), or by scraping the return of a PHP system call to the identify tool.
Either way, doesn't look likely.
Benjamin Horn has submitted a complete example about reading the requested data and even saving it locally for later use.
Check this out:
https://benjaminhorn.io/code/extracting-thumbnails-from-camera-raw-files-cr2-and-nef-with-php/