Modifying Image size for the home page – Wordpress

by Kishore on September 27, 2008

virantcms I have done some modification for the current theme home page. I am showing the content till the Read More.. The problem what I was facing was the size of the images. I have written a function which resizes the image according to the image page design. We do not need to bother about the size of the image when we do the post.

First We need some function to find the height and width of the posted image and then resize the image according the size ratio so that images don’t look odd.

function getImageInfo($str, $strfindwhat) {
    $widthPos = strpos($str, $strfindwhat);
    //echo "Width Position : ".$widthPos;
    $strAfterWidth = substr($str, $widthPos);
    //echo "After Width : ".$strAfterWidth;
    $afterWidthSpace = strpos($strAfterWidth, " " );
    $withInfo = substr($strAfterWidth,0, $afterWidthSpace);
    //echo " Width Info : ".$withInfo;
    $finalWidth= preg_replace("#[^0-9]#", "", $withInfo);
    //" ,"" , $withInfo); 
    return $finalWidth;
}

Then we modify the content using the following function

function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
    $content = get_the_content('');
    $content = apply_filters('the_content', $content);
    //$content = the_content_limit(100, "Read more");
    $thisimageheight = getImageInfo($content, "height");
    $thisimagewidth = getImageInfo($content, "width");    
    if ($thisimagewidth>100)  {
        $fwidth = 100;
        $adjuster = $thisimagewidth / $fwidth;
        $fheight = $thisimageheight / $adjuster;
        $thisimageheight = $fheight;
        $thisimagewidth = $fwidth;
    }
    $content = str_replace(']]>', ']]>', $content);
    $content = str_replace('img', 'img width='.$thisimagewidth.'px height = '.$thisimageheight.'px ', $content);
    //$content = strip_tags($content, "<img><b><strong><i><em><a><li><div>");
    echo $content;
}

Now we call this function to print the modified content

<div class="grid_5">
    <?php query_posts('cat=12,-29,-44&showposts=5'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>                    
    <div class="moreinfo">
        <h3><?php the_title(); ?></h3>
        <p style="font-size:9px; font-weight:200;"><?php the_time('F j, Y'); ?> by <?php the_author_posts_link(); ?> &middot; <?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?>&nbsp;<?php edit_post_link('(Edit)', '', ''); ?></p>
        <?php the_content_limit(150, "");  ?>
        <div style="font-size:9px; font-weight:200;">
            Filed Under: <?php the_category(', ') ?><br />Tagged: <?php the_tags('') ?>
        </div>
        <p class="more"><a title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark">Read More..</a></p>
    </div><!-- /moreinfo -->
    <?php endwhile; endif; ?>
</div>

I am using the VibrantCMS theme

Possibly Interesting Posts

US woman, JihadJane, used Internet for terror : A Pennsylvania woman who called herself JihadJane was tied on Tuesday to an alleged assassinat..[](0)
Paper Weights No Longer in Lok Sabha : After the Rajya Sabha chairman came under attack, the Lok Sabha Speaker calls a meeting, bans all po..[](0)
Dhiraagu Launches 3G Plus Service in Maldives : Long awaited 3G service from Dhiraagu has been launched. I just activated my 3G account, speed is go..[](0)
Free Train Travel for Cancer patients : "Cancer patients can travel free in 3-tier AC and sleeper class. Their escorts will get 75% off the ..[](0)
Fill out your e-mail address to receive our free newsletter from Kishore Blog!
E-mail address:
Name:
 

Leave a Comment

Previous post:

Next post: