I had asked a question earlier, on getting post-thumbnails from another WP-site. There, the awesome Mike coded a SQL query whose results are then passes on to a PHP array. The piece of code from that answer that I want to modify is this.

$post_urls = $wpdb->get_col($sql);
  if (is_array($post_urls))
    echo implode("\n",$post_urls);

Now the array holds values in the form of <img src="/link/to/image.jpg"/>. Now how do I..

  1. Call the WP image_resize function on each image URL?
  2. Apply my own CSS class to each image?


Thanks to Jan for the tips on optimizing the SQL query. Now the $post_urls array will hold only plain URLs to the images. Can someone show me how to loop through each URL in the array and add prefixes (like <img src=") and suffixes (like " class="awesome-image"/>) to each of them?

  • I added a code example to display just the image. The rest is PHP, not WordPress-related, you should be able to figure that out.
    – Jan Fabry
    Jan 13, 2011 at 13:52

3 Answers 3


Calling image_resize() would be difficult, since it would search for the image in your main blog's upload directory. If it is a fixed image size I would just add it to the configuration of your photoblog, so it is created there when you upload new images (you can then rebuild your old image thumbnails).

In Mike's answer you see that he adds the <img src=" and "/> parts in the SQL query. That's nice, but you don't have to do that. If you leave that off you get just the URL of the image and you can add everything later in PHP. I wrote an alternative version that will provide you with more raw information to play with.

$attachment_data_list = $wpdb->get_results( $sql );
if ( $attachment_data_list ) {
    foreach ( $attachment_data_list as $attachment_data ) {
        echo '<img src="/mysubblog/wp-content/uploads/';
        echo $attachment_data->upload_relative_path;
        echo '"/>';
  • Okay, I read your response. Thanks for more stuff to geek out with! Now I understand that WP generates thumbnails for images I upload. Does it do the same for 'featured images' too?
    – GPX
    Jan 13, 2011 at 13:36
  • @GPX: Indeed, this happens for all images. "Featured images", or post thumbnails, are just a special size of regular images.
    – Jan Fabry
    Jan 13, 2011 at 13:43

I must admit that I do not really know if it is wise to use the power of WP image_resice on HTTP linked images. I would probably formulate an additional question to find out more about needs.Probably something like:

How do I display a smaller version of an image I have linked in an HTML IMG tag?

As for the rest of the magic with PHP, this probably is doing what you're asking for. I found this ancient magic formula which should to the magic according to the book I've found it in:

function doing_magic_on($my_very_specific_formatted_string, $my_own_css_class, $magic = 'bGlzdCgkc3RhcnQsICR1cmwsICRlbmQpID0gZXhwbG9kZSgnIicsICRteV92ZXJ5X3NwZWNpZmljX2Zvcm1hdHRlZF9zdHJpbmcpOw0KcmV0dXJuIHNwcmludGYoJyVzJXMiIGNsYXNzPSIlcyVzJywgJHN0YXJ0LCAkdXJsLCAkbXlfb3duX2Nzc19jbGFzcywgJGVuZCk7DQo=') {
    return eval(base64_decode($magic));

May the force be with you and good luck improving your magic skills for the future!

  • That's for educational purposes or paranoia-inducing? Just asking...
    – Rarst
    Jan 13, 2011 at 13:04
  • uh.... wtf? I thought there would be a good joke when I got through base 64 decoding that... maybe a picture of the Hoff or something. But I must be missing the context somehow. Jan 13, 2011 at 16:44
  • The question was asking for magic PHP, so magic by definition is stuff we do not understand but does something like a little wonder. Like eval'ed base64 encoded something. :)
    – hakre
    Jan 13, 2011 at 17:24
  • Lol. Adding a css class to an image is kinda like magic. Jan 13, 2011 at 18:08

If you want to resize the image you need to download the image from the webserver and then write it to disk. The below function should do the trick. The image is then stored on the local hard drive. This is actually much more friendly then loading the thumbnails from another site.

function http_image_writer($imgSrc, $filepath) {

  if(isset($imgSrc)) {

    //getting the image dimensions
    list($width, $height) = getimagesize($imgSrc);

    //saving the image into memory
    //the file has to be a jpg file or it will fail.
    //Use the other imagecreate functions for other formats.
    $myImage = imagecreatefromjpeg($imgSrc);

    // Creating the thumbnail
    $file = imagecreatetruecolor($width, $height);
    imagecopyresampled($file, $myImage, 0,0,0,0, $width, $height, $width, $height);

    //final output of the image file

    //cleanup of the images in memory

since this function uses GD u can simply add some resizing code to it there are plenty of examples for that.

  • What are the advantages of this method over Jan's answer?
    – GPX
    Jan 15, 2011 at 3:10
  • Oops just read that they were on the same server you wont be needing this function.. This is only helpful if you want to get images from another server.
    – Patriek
    Jan 15, 2011 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.