Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

UPDATE:

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?

share|improve this question
    
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 '11 at 13:52

3 Answers 3

up vote 3 down vote accepted

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 '"/>';
    }
}
share|improve this answer
    
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 '11 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 '11 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:

<?php
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!

share|improve this answer
    
That's for educational purposes or paranoia-inducing? Just asking... –  Rarst Jan 13 '11 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. –  goldenapples Jan 13 '11 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 '11 at 17:24
    
Lol. Adding a css class to an image is kinda like magic. –  goldenapples Jan 13 '11 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
    imagejpeg($file,$filepath);

    //cleanup of the images in memory
    imagedestroy($file);
    imagedestroy($myImage);
  }
}

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

share|improve this answer
    
What are the advantages of this method over Jan's answer? –  GPX Jan 15 '11 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 '11 at 12:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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