0

I am trying to fetch the uploaded images using the link URL (The Link URL option is available in the Add Media when we upload/insert media) and display it on web. I have gone through the methods has_post_thumbnail and the_post_thumbnail which are working perfect for me. Now, i am trying to display images (other than featured image) with certain sizing options (as available in the_post_thumbnail).

I am using the Link URL associated with the uploaded image with the help of the function $im =imagecreatefromjpeg($url[image]); and imagejpeg($im); to display it on web but all it displays it is special characters !

Is there a way to do it ?

Update code for fetching a single image from media library:

$media_query = new WP_Query
(
          array(
          'post_parent' => get_the_ID(), 
          'post_type' => 'attachment', 
          'post_status' => 'inherit',
          'post_title' => $url[image]
          )
);

foreach ($media_query->posts as $post) 
{
  print_r(wp_get_attachment_image($post->ID));
  $imageFound = true;
  break;
}

$url[image] contains the post_title. The problem is this query is fetching all the images from the media library ! Am i missing something ?

5
  • I'm not clear on exactly what you're trying to do, or why you're creating a new image with GD (which seems extremely inefficient). Maybe you want wp_get_attachment_image_src?
    – Milo
    Jan 21, 2012 at 17:50
  • Well,i have uploaded an image in the media library from my hard drive. I want to fetch that uploaded image re-size it into a thumbnail and display it on the browser when i publish it..all of this programmatically.. is there a way to do it ?
    – Adithya
    Jan 22, 2012 at 10:20
  • attachments are children of the post they are attached to. see get_posts function to fetch attachment IDs for use with wp_get_attachment_image_src.
    – Milo
    Jan 22, 2012 at 16:18
  • i just want one image (based on the post_title say). So i am using 'WP_Query' to fetch the image from DB. The result though is fetching all images from the media !! Can you suggest something ?
    – Adithya
    Jan 23, 2012 at 17:22
  • I have got the solution. I went through the documentation and it said that post_title is actually recognized in DB by name attribute.I changed my query accordingly and it fetches only one image as expected. Thanks!
    – Adithya
    Jan 24, 2012 at 7:12

2 Answers 2

1

I want to fetch that uploaded image re-size it into a thumbnail and display it on the browser when i publish it..all of this programmatically.. is there a way to do it?

You don't need to do this manually. The add_image_size() function and the wp_get_attachment_image_src() function allow you to create and display alternate sizes for uploaded images.

Rather than running imagecreatefromjpeg() on every page load (which I assume is what you're doing based on your question), you can just create the images on upload (i.e., once).

0

I think what you're looking for is either wp_get_attachment_image : http://codex.wordpress.org/Function_Reference/wp_get_attachment_image or perhaps one of the related functions.

1
  • wp_get_attachment_image_src or wp_get_attachment_image takes an attachment_id as a parameter. What do i supply over here ? can i supply the link URL to it ? or the file name of the attached image ?
    – Adithya
    Jan 22, 2012 at 11:12

Your Answer

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

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