3

Is there a way to output a image in a pre-determined size (Ie; using add_image_size) using just the img tag?

For example, how can I make sure that the image that is echoed using this tag is the exact size I determined in functions.php under 'slider'

<img src="<?php echo of_get_option('slideshow_1');?>" alt="#"/>

I hope that made some sort of sense.

1

How about PHP Function getimagesize()

<?
$sImageurl = of_get_option('slideshow_1');
list($iWidthinpx, $iHeightinpx) = getimagesize($sImageurl);
echo '<img 
    src="' . $sImageurl . '" 
    width="' . $iWidthinpx. '" 
    height="' . $iHeightinpx. '" 
    alt="#"/>';
?>

or if you know the attachment id instead of the attachment url you could use

<?
$aImagedata = wp_get_attachment_image_src($iAttachmentid, 'my-image-size', false);
list($sImageurl, $iWidthinpx, $iHeightinpx) = $aImagedata;
echo '<img 
    src="' . $sImageurl . '" 
    width="' . $iWidthinpx. '" 
    height="' . $iHeightinpx. '" 
    alt="#"/>';
?>

and if you don't know the $iAttachmentid you could use the following to get it
(taken from http://wordpress.org/support/topic/need-to-get-attachment-id-by-image-url):

global $wpdb;
$sImageurl = of_get_option('slideshow_1');
$sQuery = "SELECT ID FROM {$wpdb->posts} WHERE guid='$sImageurl'";
$iAttachmentid = $wpdb->get_var($sQuery );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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