Skip to content

Instantly share code, notes, and snippets.

@lengpiseth
Forked from umidjons/pdf-thumbnail-php.md
Last active October 29, 2020 08:22
Show Gist options
  • Save lengpiseth/df89dbd84efbcf3bd72c52ee0c241347 to your computer and use it in GitHub Desktop.
Save lengpiseth/df89dbd84efbcf3bd72c52ee0c241347 to your computer and use it in GitHub Desktop.
Creating PDF thumbnails in PHP

Creating PDF thumbnails in PHP

Install Ghostscript

Download and install right version of ghostscript. In my case my PHP was x86 architecture, so I download Ghostscript 9.14 for Windows (32 bit).

Enable ImageMagick

Check, is imagick extension available and loaded.

This line should be present in your php.ini:

extension=php_imagick.dll

Also, check php_imagick.dll in PHP's ext directory.

PHP function

<?php
	function genPdfThumbnail($source, $target)
	{
		//$source = realpath($source);
		$target = dirname($source).DIRECTORY_SEPARATOR.$target;
		$im     = new Imagick($source."[0]"); // 0-first page, 1-second page
		$im->setImageColorspace(255); // prevent image colors from inverting
		$im->setimageformat("jpeg");
		$im->thumbnailimage(160, 120); // width and height
		$im->writeimage($target);
		$im->clear();
		$im->destroy();
	}

Call that function:

<?php
genPdfThumbnail('/uploads/my.pdf','my.jpg'); // generates /uploads/my.jpg

Successfull installation guide here:

link: http://refreshless.com/blog/imagick-pecl-imagemagick-windows/

In my fiddling, my Windows workstation serves as a my webserver. While setting up Apache, MySQL and PHP is trivial using WampServer, getting PECL extensions to work can be tricky.

Which package should I download? Prebuilt packages are available for Windows. (I went with the 32 bit version, even though my OS is 64 bit. I'm pretty sure 64 bit will work, too.)

The setup has two points of attention: installation path and environment variables. I went with a can't-fail path:

C:\img The Imagick installer will offer to set the environment variable in your path. You'll either want to check this option, or ignore it, as you'll have to manually change it anyway.

Configuring the Windows Enviroment Now for something the documentation does not mention: setting the MAGICK_HOME value in your environment. ImageMagick requires this variable so it knows where to find all relevant coders.

Open up the control pannel, and search for 'PATH'. In the results, select 'Edit the system environment variables'. Select the 'Environment variables...' option, and add a new 'System variable' (the lower list). If you've chosen a different installation path, make sure to replace C:\img with your own path.

Variable: MAGICK_HOME Value: C:\img\modules\coders Install the PECL package Next up, we'll actually install the PECL package. The maintainers have been so kind as to provide a Imagick DLL. If you are using WAMP, like me, make sure to use the Thread Save version. Pick the package that matches your PHP version and the X86/X64 version of ImageMagick you've installed.

From the download, in my case php_imagick-3.1.2-5.5-nts-vc11-x86.zip, grab the php_imagick.dll file, and move it you PHP extention folder:

C:\wamp\bin\php\php5.5.12\ext The other DLL's, all starting with CORE_RL_*, can be moved into Apache's binary folder:

C:\wamp\bin\apache\apache2.4.9\bin Finally, activate the extention in your PHP.ini file. Make sure to actually edit the loaded configuration file. (You can check with phpinfo() if you're not sure.)

C:\wamp\bin\apache\apache2.4.9\bin\php.ini Find the list of extensions, and add Imagick:

extension=php_imagick.dll Check if everything is OK Restart Apache, and check phpinfo() again. If all went well, you'll find details on the Imagick installation:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment