Imagemagick and wordpress script generating two images

I am using imagemagick to process an image from a URL stored in a wordpress custom field. The script actually does what it is meant to apart from one thing, it processed an extra image every time. The code is below….

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
    $url1 = '/home/sites/public_html/mysite/wp-content/themes/mytheme/images/';
    $imagefile = get_post_meta($post->ID, 'image_url', $single = true);
    $file = basename($imagefile);
    $location = '/usr/bin/convert';
    $command = '-resize 560x';
    $convert = $location . ' ' .$command . ' ' . $imagefile . ' ' . $url1 . $file;
    exec ($convert);    
?>

<?php endwhile; ?>
<?php endif; ?>

The extra image it creates is the same one everytime and is the first post in my post list, im not sure if it being the first post is related or not.

Read More

I am also unsure if it is imagemagick that is causing the issues or wordpress, the imagemagick commands I am using are really simple so I am leaning towards wordpress.

Can anyone suggest anything?

UPDATE

I have been playing around and have found that this issue only seems to happen with a couple of posts. I have checked in the SQL database and compared them with one that isn’t affected and can’t see anything obvious.

UPDATE 2
To make things even stranger I have duplicated this setup on a local dev server, I am UNABLE to replicate it. Everything works fine! If I do the same thing on my shared host then the problem comes back. This is making me think there must be some config somewhere causing it.

On the shared host if I do the following code then it works correctly….

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
    $url1 = '/home/sites/public_html/mysite/wp-content/themes/mytheme/images/';
    $imagefile = 'http://www.mydomain.com/images/myimage.jpg';
    $file = basename($imagefile);
    $location = '/usr/bin/convert';
    $command = '-resize 560x';
    $convert = $location . ' ' .$command . ' ' . $imagefile . ' ' . $url1 . $file;
    exec ($convert);   
?>

<?php endwhile; ?>
<?php endif; ?>

But if I revert back to my original code where the image URL is being generated by the wordpress get_post_meta command then I get the extra image.

UPDATE 3
Using the wordpress plugin BlackBox, I have been able to find a bit more information out about what is happening, the logs give me this info..

0.2501 [ms] SELECT * FROM wp_users WHERE user_login = 'admin';
0.1330 [ms] SELECT user_id, meta_key, meta_value FROM wp_usermeta WHERE user_id IN (1);
0.0739 [ms] SELECT option_value FROM wp_options WHERE option_name = 'theme_switched' LIMIT 1;
0.1280 [ms] SELECT ID, post_name, post_parent FROM wp_posts WHERE post_name IN ('uncategorized','my-test-post-75') AND (post_type = 'page' OR post_type = 'attachment');
0.1481 [ms] SELECT wp_posts.* FROM wp_posts  WHERE 1=1  AND wp_posts.post_name = 'my-test-post-75' AND wp_posts.post_type = 'post'  ORDER BY wp_posts.post_date DESC ;
0.1359 [ms] SELECT t.*, tt.*, tr.object_id FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category', 'post_tag', 'post_format') AND tr.object_id IN (215) ORDER BY t.name ASC;
0.1402 [ms] SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (215);
0.1140 [ms] SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('category') AND t.slug = 'uncategorized' ORDER BY t.name ASC ;
0.1018 [ms] SELECT tr.term_taxonomy_id FROM wp_term_relationships AS tr INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN (215) AND tt.taxonomy IN ('category') ORDER BY tr.term_taxonomy_id ASC;
0.1259 [ms] SELECT p.* FROM wp_posts AS p  WHERE p.post_date < '2011-06-07 20:40:34' AND p.post_type = 'post' AND p.post_status = 'publish'  ORDER BY p.post_date DESC LIMIT 1;
0.0758 [ms] SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category') AND tr.object_id IN (204) ORDER BY t.name ASC;
0.0651 [ms] SELECT p.* FROM wp_posts AS p  WHERE p.post_date > '2011-06-07 20:40:34' AND p.post_type = 'post' AND p.post_status = 'publish'  ORDER BY p.post_date ASC LIMIT 1;
0.0849 [ms] SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category') AND tr.object_id IN (251) ORDER BY t.name ASC;

Does this new info shed any light on what could be happening?

Related posts

Leave a Reply

2 comments

  1. I have little experience with imagemagick, but I’ve experienced a similar situation using the WordPress core functionality for resizing, it’ll affect new images added, but not the older images, added prior to the new image size code.

    There is a great little utility plugin called “regenerate thumbnails”, which sorted this out for me,

    you can find it in the wordpress codex here.

    http://wordpress.org/extend/plugins/regenerate-thumbnails/

  2. All of the functionality you are looking for is currently available in the wordpress core – no need to add your own imagemagick functions:

    in functions.php of your theme:

    if ( function_exists( 'add_image_size' ) ) { 
        add_image_size( 'custom-size', 560, 560, false ); 
        // or set last option to true for cropping
    }
    

    (more info: http://codex.wordpress.org/Function_Reference/add_image_size)

    in your page.php or other theme file:

    <?php echo get_the_post_thumbnail($page->ID, 'custom-size'); ?>
    

    (more info: http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail)

    Now add the image to your post or page under ‘set featured image’, and skip using custom fields and post_get_meta.