Support » Fixing WordPress » Thumnail creation and GD version

  • First of all: sorry for my english. Now we can go on.
    I’ve seen that in 1.2 alpha there is some support for thumbnails creation. Great idea! But the code could be improved for GD library.
    GD library makes some difference in function between 2.xx version and the old 1.xx version. In your code (admin-functions.php) you used the following line
    $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height);
    this code only works in GD 2.xx.
    But the next line of code is
    @imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);
    and imagecopyresized is not the best solution for gd 2.xx.
    So this code is a mix of legacy library code (imagecopyresized) and new library code (imagecreatetruecolor).
    Quoting from the GD web site (FAQ section): [em] When creating truecolor images, you need to call gdImageCreateTrueColor, not gdImageCreate. This used to “work” in gd 1.x, but only in the sense that gd 1.x had no support for real truecolor at all, so it faked it by dithering your image. gd 2.x supports real truecolor> [/em]
    Maybe is better to split the code and let it works at its best in both version.
    1.xx code:
    $thumbnail = imagecreate($image_new_width, $image_new_height);
    @imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);

    2.xx code:
    $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height);
    @imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);

    Now is time to check which version of the GD library is installed. This issue is not so simple. Or, at least, it wasn’t simple to me.
    There are some method.
    1) check if imagetruecolor exist: but someone reported that not always works
    2) check if extension_loaded(‘php_gd2’) return true. This (good) method don’t work on windows.
    3) use phpinfo() and then parsing the result. Mmmmhhhh… this one should work everywhere.
    So if you decide for #3 I’ve found a lot of code around the internet but my preferred one is the one from Christian Heilmann:
    function checkgd(){
    $phpinfo=stristr($phpinfo,"gd version");
    if ($gd[0]=='2'){$gd2="yes";}
    return $gd2;

    Hope it can help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Maybe this is the reason why automatic thumbnail generation doesn’t work for me. When I upload an image, WordPress says that everything was done sucessfully – in fact, the file is uploaded, but without changing any properties.
    Cheers, Sven

    LaughingLizard had a hack where he checked the version of GD and used the corresponding function for that version. I tried searching for it, but can’t find it now.

    Paolino– thanks to your findings, my thumbnail quality has gotten SIGNIFICANTLY better. I just wanted to thank you for figuring this out. Didn’t even think it was a WP issue (I had been too busy doing research into GD to look at the WP code)
    Hopefully the developers realize this and put in a switch statement or something…?

    Is this issue being adressed in the new updates of the alpha?

    My server – scabby as it is – haven’t got around to installing GD or Imagemagick and probably never will. Is there a way you could run a quick check in upload.php so that if these libraries weren’t there, there wouldn’t be an option to create a thumbnail?
    I have a few tech-lite writers, and in its current state it confuses them and generally makes them sweaty.
    BTY, I’m loving the software. I’ve just finished the final design here – cheers!

    I packaged the code, if anyone is interested in a simple solution (and isn’t bold enough to edit the functions):

    mel: try the imagebrower plugin:
    I’m pretty sure that it will create a thumb to popup image if you select that option.

    It doesn’t support popup windows yet, but I added it to the todo list.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Thumnail creation and GD version’ is closed to new replies.