This article describes how to use imagick to generate PSD file thumbnails in PHP. This article describes how to install ImageMagick, install imagick, check whether the installation is successful, and generate PSD file thumbnails, for more information, see
Step 1. install ImageMagick
First, install ImageMagick http://imagemagick.org/script/binary-releases.php#windows, because imagick is a PHP extension that allows PHP to call the ImageMagick function. (The latest version is: ImageMagick-6.8.9-5-Q16-x64-dll.exe)
The installation directory cannot contain spaces. I personally installed it in the C root directory:
The code is as follows:
C: \ ImageMagick
You can install the command by default. after the installation is complete, open the CMD command line interface, enter: convert, and press Enter. if you can see a lot of content, the installation is successful.
If the message "convert is not an internal or external command, it is neither a running program or a batch processing file "or prompts" Enter the drive letter for conversion "(convert is a default system command to convert FAT32 to NTFS) then you need to add the installation path of ImageMagick to the system path.
My computer-> right-click-> Properties-> Advanced-> environment variables-> System variables-> Find "Path"-> Select and click edit-> Install path.
Step 2. install imagick
Click here to download the corresponding PHP version. pay attention to whether the system is 32-bit or 64-bit, and whether PHP is the kV or TS version (currently the latest stable version is 3.1.2. you can view the Thread Safety items in phpinfo to determine whether PHP is the kV or TS version, if it is enabled, it indicates that it is TS, or no, it is TS)
Decompress the downloaded package, find the php_imagick.dll file, put it in the PHP extension directory, open the php. ini file, and add the following line at the corresponding location:
The code is as follows:
Extension = php_imagick.dll
Save and exit, and restart your computer to make sure it takes effect.
Step 3: Check
If everything is normal, you can see the imagick item in phpinfo after restart, such:
If the image cannot be found, the first step is returned. replace ImageMagick of different versions and reinstall it.
Step 4: Test
OK. At this step, you can start to write the code. The process will not be detailed.
The code is as follows:
Error_reporting (E_ERROR | E_WARNING );
$ Im = new Imagick ('1. psd ');
$ Im-> setImageIndex (0 );
$ Im-> setIteratorIndex (0 );
$ Im-> stripImage (); // remove image information
$ Im-> setImageCompressionQuality (80); // picture quality
$ Im-> writeImage('1.jpg ');
The above code has been tested to correctly generate a 1.jpg image.
We recommend that you use a thumbnail in jpg format because the png format does not support compression. during the test, I used a 30 M PSD file. the generated png thumbnails are more than 3 M, jpg is only about KB, and the speed of generating png images is obviously slow, which takes about 3 or 4 seconds.
In addition, an extreme test was conducted to generate a jpg thumbnail using a PSD file of nearly 600 MB. the speed was within 2 seconds and the file size was less than 2.5 MB, because the size of the image is not modified, the size of the final generated file can be reduced according to the actual situation.
Step 5. other formats
In addition to PSD files, I also tried some other formats, such as MP4 and AVI, but the generation speed is extremely slow. Finally, we found that the more practical method is to generate a thumbnail of a PDF file. before processing a PDF file, we need to install Ghostscript, and the overall code is basically the same as the code for generating a PSD.
The code is as follows:
$ Im = new Imagick ();
$ Im-> readImage ('d:/soft/wamp/www/test/1.20.[ 0] ');
// Echo $ im-> getNumberImages ();
$ Im-> writeImages ('d:/soft/wamp/www/test/1.jpg ', false );