So I built a site that creates thumbnails using a php script that uses imagick for thumbnail creation. On my personal web host (Hostmonster), this works fine. When I made a copy of my site and uploaded it to my client's site (hosted on domain.com) it failed. Asking Domain.com what was up, they said:
"We have ImageMagick binaries. But it is not complied to PHP so IMAGICK won’t work. So, you will not be able to use IMAGICK as PHP built-in class. You have to use the alternative ‘convert’ utility"
I've never used the "convert utility" before, so I'm not sure what they're talking about. Should I look into this more? Or fire them and just stick with my original host (Hostmonster) where imagick works in PHP? I feel like I should know about this alternative to be more well-rounded in my knowledge, but wondering if its worth my time to learn it, or just forget it and move on.
Suggestions about how to go forward, or resources on this alternative would be appreciated. Thanks.
What they are talking about is using PHP's exec()
function to execute ImageMagick the same way as you would do it from the command line. There are hundreds of examples on SO of using ImageMagick from the command line, just search for ImageMagick
and convert
which is the name of the main binary you would use at the command line.
The syntax is pretty simple:
convert input.png ...<processing> ... output.png
So, you might do
convert input.jpg -thumbnail x320 -quality 80 result.png
If you want to do that in PHP, your ISP is suggesting you use:
$cmd = "/usr/local/bin/convert '/path/to/input.jpg' -thumbnail x320 -quality 80 '/path/to/result.png'";
exec($cmd, $output, $retval);
I can't really comment on the relative efficiency, merits or otherwise (though it smacks of laziness or incompetence on their part), but it should work fine.
Ok, I'm attempting to implement this alternative way, but I'm not sure I'm executing it right. At the moment this is my code, and I'm getting an internal 500 error. I unfortunately can't figure out how to get error messages generated with domain.com, so I'm flying a bit blind here.
Using the above $cmd = "/usr/local/bin/convert '/path/to/input.jpg' -thumbnail x320 -quality 80 '/path/to/result.png'"; exec($cmd, $output, $retval);
I'm trying to insert a couple variables in for the input path, and the result path:
$thumbsPath = "./RecentAdditions_thumbnails/";
if (!file_exists($thumbsPath)) {
mkdir($thumbsPath, 0777, true);
}
foreach($files as $file)
if(!file_exists($thumbsPath.basename($file, ".jpg")."_thumb.jpg")){
$cmd = "/usr/local/bin/convert ''./'"$file"' -thumbnail x200 -quality 100 '"$thumbsPath.basename($file, ".jpg")."_thumb.jpg'";
exec($cmd, $output, $retval);
As I tell people start with some thing simple first. A simple resize code on an image in the same folder; if that works go onto the next step.
You need to have safe mode off and with phpsusex or whatever it is called you will need a CHMOD of 755 otherwise 777 on the folder the image is saved to.
try:
$cmd = "input.jpg -thumbnail x200 -quality 100 output.jpg";
exec("convert $cmd");
You may have to use the path to convert but if Imagemagick is installed correctly it should work with just convert.
In some cases Imagick is quicker that Imagemagick and as said if you are accepting user input make sure you do a good job of validating the data.
On anther subject I try and keep the code in the command simple so I would save all this $thumbsPath.basename($file, ".jpg") as a variable and use that in the IM code. Especialy in this case as you are using it twice in your code.
You can also echo out $cmd to confirm what it actualy contains; sometimes it is not what you expect!