Tutorial on using Imagick to generate PSD file thumbnails in PHP _php tips

Source: Internet
Author: User
Tags imagemagick phpinfo

First step, install ImageMagick

First you need to install ImageMagick http://imagemagick.org/script/binary-releases.php, because Imagick is a PHP extension that can be used by PHP to invoke ImageMagick functionality. (Currently the latest version is: Imagemagick-6.8.9-5-q16-x64-dll.exe)

Installation directory can not have spaces, I am personally installed in the C packing directory:

Copy Code code as follows:

C:\ImageMagick

All the way by default installation can be installed, after the installation of the CMD command-line interface, input: Convert, and press ENTER, if you can see a large pile of content to indicate the success of the installation.

If you are prompted that convert is not an internal or external command, is not a running program or batch file, or that you are prompted to enter a converted drive letter (convert is a default system command that converts FAT32 to NTFS) then you need to ImageMagick yourself. Installation path is added to the system path.

My Computer-> right-click-> Properties-> Advanced-> environment variable-> system variable-> find "path"-> Select and click Edit-> Add your installation path.

Step two, install Imagick

Click here to download, pay attention to download the corresponding PHP version, and note that the system is 32 or 64 bits, and PHP is NTS version or TS version (the current stable version is 3.1.2, about how to determine whether PHP is NTS version or TS version, you can see through the phpinfo of its In the Thread Safety item, if is enabled on behalf of the TS version, no is the NTS version)

Unzip the downloaded Compression pack, locate the Php_imagick.dll file, place it in the extended directory of PHP, and then open the php.ini file, adding the following line in the corresponding location:

Copy Code code as follows:

Extension=php_imagick.dll

Save the exit and restart the computer to ensure that it takes effect.

Step three, check

If everything is OK, you should be able to see Imagick items in Phpinfo after reboot, such as:

If it is not found, return to the first step and replace the different versions of the ImageMagick reinstall and debug again.

Step Fourth, test

OK, this step can start writing code, the process is not in detail, directly on the code

Copy Code code as follows:

Error_reporting (E_error | e_warning);

$im = new Imagick (' 1.psd ');
$im->setimageindex (0);
$im->setiteratorindex (0);
$im->stripimage (); Remove picture information
$im->setimagecompressionquality (80); Picture quality
$im->writeimage (' 1.jpg ');

The above code has been tested to correctly generate a 1.jpg picture.

Recommended thumbnail or JPG format, because the PNG format does not support compression, I was testing the use of 30M PSD file, the resulting PNG thumbnail has more than 3M, and JPG only 200KB around, and the speed of the generation of PNG image is significantly slow, about 3, 4 seconds.

In addition to the extreme test, using a nearly 600M PSD file to generate JPG thumbnails, the speed of basic in 2 seconds, the file size of less than 2.5M, because the picture does not modify the size, so the final volume of the resulting file can be reduced according to the actual situation.

Step Fifth, other formats

In addition to PSD file, I also tried some other formats, such as MP4, AVI and other video formats, but the production speed is super slow. Finally found that the more practical is to generate a PDF file thumbnail, but before processing the PDF file, you need to install Ghostscript, and then the overall code basic and generate PSD code

Copy Code code as follows:

$im = new Imagick ();
$im->readimage (' d:/soft/wamp/www/test/1.pdf[0] ');
echo $im->getnumberimages ();
$im->writeimages (' d:/soft/wamp/www/test/1.jpg ', false);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.