0

I use Magic Fields a lot to give my clients and users the ability to upload specific content to their websites. Is there a way to call images by their size? At the moment I just put out the images, the persons uploads. But right now I need a solution that gets a specific size from the uploaded picture.

I know that Magic Fields uses the default Image-Uploader by wordpress which creates predefined sizes by default. Is there a way to get those sizes while calling the magic field with the uploaded image?

3 Answers 3

0

Magic fields doesn't necessarily attach images to posts when uploaded via the WP image uploader, and it's not so simple to get the ID associated with an image. The plugin has its own function to retrieve different sizes:

$image_attributes = "w=250&h=150&zc=c&q=90"; // 250x150px, crop to center, quality 90
echo get_image('my_field_name',1,1,1,NULL,$image_attributes);

New sizes are generated on-the-fly and then cached for future use. See get_image() in Magic Fields wiki.

1

Sorry I know that this is an old question, but I've been looking for a solution to the same question, and come up with this rather horrible solution:

global $wpdb;
$getimage = get('fieldname',$group,1,1,$post->ID); // Magic Fields 'get' syntax
$query = "SELECT ID FROM wp_posts WHERE guid='".$getimage."'";
$results = $wpdb->get_results($query);
$attachment = $results[0];
$image = wp_get_attachment_image_src($attachment->ID,'image-size');
$imageurl = $image[0];

I needed to get the image URL rather than output an img tag so I could use it as a background image. Replace 'image-size' on line 6 with whatever image size you have defined.

Hope this helps!

Edit: I've condensed into a function:

In functions.php

// Add image size - as per WP
add_image_size('image-size',50,50,true);

// Get WP image size from Magic Field
function mftoimgsize($fieldname, $group, $postID, $imagesize) {
global $wpdb;
$getimage = get($fieldname,$group,1,1,$postID);
$query = 'SELECT ID FROM '.$wpdb->base_prefix.'posts WHERE guid="'.$getimage.'"';
$results = $wpdb->get_results($query);
$attachment = $results[0];
$image = wp_get_attachment_image_src($attachment->ID,$imagesize);
$imageurl = $image[0];
return $imageurl;
}   

In template:

<?php echo mftoimgsize('mf-field-name', $group , $post->ID, 'image-size'); ?>
0
0

Without knowing anything about how the Magic Fields Plugin works specifically:

  1. If Magic Fields is using the core uploader, I assume that the images added via Magic Fields are then attached to the Post, as Attachments?
  2. If so, then all such images have a $post->ID.
  3. Thus, you simply need to query the post custom metadata added via Magic Fields, and determine the $post->ID for a given image
  4. Then, pass that $post->ID to wp_get_attachment_image( $attachment_id, $size )

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.