Support » Alpha/Beta/RC » ImageMagick for thumbnails

  • I love WordPress RC3. It is just wonderful. New support for uploading pictures right where you post stuff is plain awesome.

    I had one problem, though – my host does not have PHP compiled with GD, so thumbnail generation were not working for me. At the same time, I have shell access, and it was really easy for me to download ImageMagick command line utility – which I did. I’m sure some users might be in the same situation, so I’ll share my findings.

    Maybe the developers of WordPress will even consider including support for ImageMagick in the final release – since it should be very easy (took me only 1 hour).

    But first – why use ImageMagick?
    1) ImageMagick does not have to be compiled with PHP – very easy option when you don’t have GD.
    2) ImageMagick is faster than GD and generates better quality thumbnails. It’s the library of choice for many gallery projects.

    How do I get ImageMagick to work on my site?
    a) Download ImageMagick package from
    b) Untar, put in a directory where the web server can execute the binaries.
    c) Make minor (almost tiny!) changes to wp-admin/admin-functions.php:

    1) Find the function wp_create_thumbnail(). Add the following as its first line:
    return alex_hack_thumbnail_using_imagemagick($file, $max_side, $filter);

    2) Copy the following function above wp_create_thumbnail():

    function alex_hack_thumbnail_using_imagemagick($file, $max_side, $filter = '') {
    // Make sure that the web server can execute scripts from this directory.
    $PATH_TO_CONVERT = "/home/alex94040/imagemagick/bin/";

    if (file_exists($file)) {
    $image_attr = getimagesize($file);

    $image_attr = getimagesize($file);

    // figure out the longest side

    if ($image_attr[0] > $image_attr[1]) {
    $image_width = $image_attr[0];
    $image_height = $image_attr[1];
    $image_new_width = $max_side;

    $image_ratio = $image_width / $image_new_width;
    $image_new_height = $image_height / $image_ratio;
    //width is > height
    } else {
    $image_width = $image_attr[0];
    $image_height = $image_attr[1];
    $image_new_height = $max_side;

    $image_ratio = $image_height / $image_new_height;
    $image_new_width = $image_width / $image_ratio;
    //height > width

    // If no filters change the filename, we'll do a default transformation.
    if ( basename($file) == $thumb = apply_filters('thumbnail_filename', basename($file)) )
    $thumb = preg_replace('!(\.[^.]+)?$!', __('.thumbnail').'$1', basename($file), 1);

    $thumbpath = str_replace(basename($file), $thumb, $file);

    $convert_str = $PATH_TO_CONVERT.'convert'.
    ' -sample '.$image_new_width.'x'.$image_new_height. // new size
    ' "'.$file.'"'. // source file to convert
    ' "'.$thumbpath.'"'; // destination file = thumbnail
    exec($convert_str, $array_output, $return_value); // call ImageMagick command line utility to do the conversion
    if($return_value != 0) {
    $error = __('Error while creating image '.$file.'! <br>Return Value: '.$return_value.', output:<br>');
    $error .= __("<br>Command: ".$convert_str.'<br>');
    } else {
    $error = __('File not found');

    if (!empty ($error)) {
    return $error;
    } else {
    return $thumbpath;

    3) Correct the path to the imagemagick installation in the $PATH_TO_CONVERT variable in the code that you just pasted.

    Good luck!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thanks!!
    You saved me a looot of hassle.

    Alex, one huge change you didn’t make was using the “-thumbnail” switch instead of the “-size” switch. Try this, it makes a huge difference. Or, try -density xx (where x is something pretty big >300) and then “-geometry” instead of “-size”. These changes make a HUGE quality difference over “-size”. It takes away the grainess. Try it for yourself.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘ImageMagick for thumbnails’ is closed to new replies.