{"id":5,"date":"2012-11-27T07:30:53","date_gmt":"2012-11-27T07:30:53","guid":{"rendered":"http:\/\/nuclearprojects.com\/blog\/?p=5"},"modified":"2012-11-27T12:22:37","modified_gmt":"2012-11-27T20:22:37","slug":"using-imagemagick-with-php","status":"publish","type":"post","link":"https:\/\/nuclearprojects.com\/blog\/using-imagemagick-with-php\/","title":{"rendered":"Using ImageMagick with PHP"},"content":{"rendered":"<p>On one of my sites, I needed to generate thumbnail images from various uploaded files. There are several options out there for doing that, including the GD and ImageMagick libraries. Since GD only supports a limited number of file types, the clear choice was to go with IM. ImageMagick itself does not have native PHP support. However, you can install <a title=\"Imagick\" href=\"http:\/\/pecl.php.net\/package\/imagick\" target=\"_blank\">Imagick<\/a>, which is a PHP extension that allows you to use ImageMagick via PHP. Unfortunately, since all my sites are on a shared server, my hosting company will not install the Imagick extension. Which means I&#8217;m stuck using ImageMagick without the fancy tools Imagick provides.<\/p>\n<p>But there is no need to fear, as working with ImageMagick through PHP is still pretty easy.<br \/>\n<!--more--><br \/>\nIn PHP, you can run ImageMagick commands by using the <strong><a title=\"exec()\" href=\"http:\/\/php.net\/manual\/en\/function.exec.php\" target=\"_blank\">exec()<\/a><\/strong> command. Here is an example:<\/p>\n<p><code lang=\"php\"><br \/>\n\/\/ path to ImageMagick convert program<br \/>\n$convert = \"\/usr\/bin\/convert\";<\/p>\n<p>exec(\"$convert before.jpg after.png\");<br \/>\n<\/code><\/p>\n<p>This example will convert the image file <em>before.jpg<\/em> to <strong>.png<\/strong> format and save it as <em>after.png<\/em>. Both images would be located in the same folder as the PHP file. However, you can use relative paths like this:<\/p>\n<p><code lang=\"php\">exec(\"$convert images\/before.jpg images\/after.png\");<\/code><\/p>\n<p>&nbsp;<\/p>\n<h2>Resize<\/h2>\n<p>You can resize an image like this:<\/p>\n<p><code lang=\"php\">exec(\"$convert big.jpg -resize 200x200 small.jpg\");<\/code><\/p>\n<p>This will resize the image to a maximum dimension of 200 pixels. The image will maintain its same aspect ratio and proportions, just the largest dimension will become 200px.<\/p>\n<p>Some great documentation and examples can be found here:<br \/>\n<a href=\"http:\/\/www.imagemagick.org\/Usage\/\" target=\"_blank\">http:\/\/www.imagemagick.org\/Usage\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>On one of my sites, I needed to generate thumbnail images from various uploaded files. There are several options out there for doing that, including the GD and ImageMagick libraries. Since GD only supports a limited number of file types, the clear choice was to go with IM. ImageMagick itself does not have native PHP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":39,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,9,5,6,4,8],"class_list":["post-5","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-programming","tag-convert","tag-image","tag-imagemagick","tag-imagick","tag-php","tag-resize"],"_links":{"self":[{"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/posts\/5","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/comments?post=5"}],"version-history":[{"count":30,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/posts\/5\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/posts\/5\/revisions\/40"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/media\/39"}],"wp:attachment":[{"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/media?parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/categories?post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/tags?post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}