Generating Thumbnails using phpthumb

by Kishore on April 30, 2009

generating thumbnails wordpress I had written a post on automatically updating the thumbnail custom field value which had a lot of response and discussion. I recently changed the thumbnail generation to phpthumb which has lots of more option. You can generate thumbnails with different sizes and use different filters like sepia, gray, etc.

Check out the full information on the options that you can use with this script. The installation is simple, but make sure that the cache folder is made writeable (Chmod 777).

If you are using Wordpress, you can use the autothumb plugin which will ease your job. You can get the attached images using this function. I have added this function to the autothumb.php of this plugin.

function kish_autothumb($postId) {
    // Get the post ID
    $iPostID = $post->ID;
 
    // Get images for this post
    $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $postId );
 
    // If images exist for this page
    if($arrImages) {
 
        // Get array keys representing attached image numbers
        $arrKeys = array_keys($arrImages);
 
    // Put all image objects into new array with standard numeric keys (new array only needed while we sort the keys)
        foreach($arrImages as $oImage) {
            $arrNewImages[] = $oImage;
        }
 
        // Bubble sort image object array by menu_order TODO: Turn this into std "sort-by" function in functions.php
        for($i = 0; $i < sizeof($arrNewImages) - 1; $i++) {
            for($j = 0; $j < sizeof($arrNewImages) - 1; $j++) {
                if((int)$arrNewImages[$j]->menu_order > (int)$arrNewImages[$j + 1]->menu_order) {
                    $oTemp = $arrNewImages[$j];
                    $arrNewImages[$j] = $arrNewImages[$j + 1];
                    $arrNewImages[$j + 1] = $oTemp;
                }
            }
        }
 
        // Reset arrKeys array
        $arrKeys = array();
 
        // Replace arrKeys with newly sorted object ids
        foreach($arrNewImages as $oNewImage) {
            $arrKeys[] = $oNewImage->ID;
        }
 
        // Get the first image attachment
        $iNum = $arrKeys[0];
 
        // Get the thumbnail url for the attachment
        $sThumbUrl = wp_get_attachment_thumb_url($iNum);
        strlen(wp_get_attachment_url($iNum))?$sImageUrl = wp_get_attachment_url($iNum):$sImageUrl = wp_get_attachment_thumb_url($iNum);        
    }    
    return $sImageUrl;
}

You can call this function on the home.php

<div class="hpfeatured">
            <h3>Bollywood News</h3>
            <?php $counter = 1; ?>    
            
                <?php $recent = new WP_Query("cat=19,-107&showposts=3"); while($recent->have_posts()) : $recent->the_post();?>
                <?php strlen(get_post_meta($post->ID, "Thumbnail", true))?$kishimg=get_post_meta($post->ID, "Thumbnail", true) : $kishimg = kish_autothumb($post->ID); ?>
                <?php if(strlen($kishimg) ): ?>        
                <?php //echo $kishimg; ?>
                <a href="<?php the_permalink() ?>" rel="bookmark"><img style="float:left; border:1px solid #CACACA; margin:0px 10px 0px 0px;" src="<?php echo getphpthumburl($kishimg, 'w=40&h=40&zc=1');  ?>" alt="<?php the_title(); ?>" /></a>    
                <?php endif; ?>    
                <b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></b><span style="font-size:9px;margin-left:5px;"><?php wp_delete_post_link(); ?></span>    
                <?php the_content_limit(60, ""); ?>
                <div style="border-bottom:1px dotted #94B1DF; margin-bottom:10px; padding:0px 0px 5px 0px; clear:both;"></div>
                
                <?php endwhile; ?>
                <b><a href="http://www.kisaso.com/news/category/indian-news/bollywood/" rel="bookmark">Read More Posts From Bollywood</a></b>
                
            </div>    

There are many options available to change the displayed thumbnail. You can adjust the sizes, use different filters.

Possibly Interesting Posts

New Guidelines for Plugin Developers - Wordpress : Mark Jaquith has written on his blog on the Wordpress plugin repository where Plugin Authors can hos..[](0)
Adobe Plugs 13 Security Holes in Critical Update : Adobe Systems embarks on a new schedule for security updates with 13 critical fixes in tow for Reade..[](1)
Security Expert Suggests Twitter Focus on Output Escaping not Input Filtering : Twitter's status blog this morning announced that Twitter has addressed the most recent variant of..[](1)
Checklist for submitting your plugin to Wordpress Extension Directory : You will be knowing that Wordpress can host your plugin at the Wordpress Extension Directory. You c..[](57)

Leave a Comment

Previous post:

Next post: