Define WordPress image size in img tag

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="https://wordpress.stackexchange.com/questions/67520/<?php echo of_get_option("slideshow_1');?>" alt="#"/>

I hope that made some sort of sense.

1 Answer
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 );

Leave a Comment