Coder, Need little Code Change in my Wordpress Blog

Status
Not open for further replies.

Ryza

Banned
Banned
278
2011
0
0
Ok, I dont know where should I start but I guess should try my best to make it clear.

here's the story,

When I want to post in my wordpress blog. I need to upload the image so it will appear as thumbnail of the post..

like these:
[slide]http://www.imgcafe.com/view/uploads/edit20tft.jpg[/slide]
[slide]http://www.imgcafe.com/view/uploads/downloadin.jpg[/slide]

Ok here's what I want.

Instead of uploading pic to make it a thumbnail of every post.
I want to make my [ IMG ] [ /IMG ] in the top of my post template to automatically make it the thumbnail..

For more information or clarification please post here.

also please tell me how much for this to be changed.

Thank you.
 
33 comments
You dont actually need to "make thumbails". With a bit of CSS you can hardlink the picture and resize it to fit your theme. If your theme use a class or whatever to identify a cover, you can use an external code to place in your wordpress that just search for the first image in your post and give it the class or ID you need.
 
I not understand exactly what you need, your explanation is a little short.

Something like into my blog (VideoRipX.com) , i upload the image with any size but them is resized and the tumbnail is created (i´m using timthumb.php).

You can use css as suggested but you will waste bandwith and increase loading time, poor option from my point of view (is like kill flies with cannons).

At the same time i´m reading BBCODE into your wordpress post in HTML edit mode... weird...
 
@iniuria
truth is I dont really get any of it of your explanation m8. simply because I'm noob at this that's why im hiring.

please do understand.

Thank you.

@maniac,
actually thats part of the thing I want to change.

I want it to auto detect my first image like iniura says.

@zeo
yep my thumbnail is auto resize.

but I think inuira gets my point though.
 
so basically if I prefer auto detect [ IMG ] [ /IMG ] as thumbnail it will make my blog slow??

hmm I forgot to answer, thats why I'm using Bbcode in HTML edit mode its because I have a plugin BBcode to html parser so its not a problem..
 
As i said before, you have to resize the image to build the tumbnail why you say that your tumbnail is autoresize???

PM me your blog link... anyways if you want to make a thumbnail from a larger image you have to resize it with css (wasting bandwith, because the size of the image remains the same you only rescale dimensions) with any php script (save banbwith, for example timthumb.php).

As example you can see this to be more clear:

Full size image: http://www.videoripx.com/wp-content/uploads/2011/02/nice-and-naughty-4.jpg

Thumbnail: http://www.videoripx.com/wp-content...11/02/nice-and-naughty-4.jpg&h=190&w=145&zc=1

Not sure of you are searching for something like that...
 
I believe Wordpress automatically creates thumbnails and even gives you the option to insert a thumbnailed version of the image you uploaded. Am I missing something?
 
yep they automatically creates thumbnails if you upload them..

but I dont want that.

what I want is, I dont upload anymore. and in my post it detects my first [ IMG ] [ /IMG ] image and make it a thumbnail.
 
Prazsky, his blog is using timthumb. mRaza posted a modified timthumb script somewhere, just use the search button and you're all good.
 
Here is the code for timthumb.php that accepts the remote images, it was re-written by litewarez to accept remote images.

Code:
<?php

// TimThumb script created by Tim McDaniels and Darren Hoyt with tweaks by Ben Gillbanks
// http://code.google.com/p/timthumb/

// MIT License: http://www.opensource.org/licenses/mit-license.php

/* Parameters allowed: */

// w: width
// h: height
// zc: zoom crop (0 or 1)
// q: quality (default is 75 and max is 100)

// HTML example: <img src="/scripts/timthumb.php?src=/images/whatever.jpg&w=150&h=200&zc=1" alt="" />

error_reporting(E_ALL);

if(!isset($_REQUEST["src"])) {
    die("no image specified");
}

// clean params before use
$src = clean_source( $_REQUEST[ "src" ] );

// set document root
$doc_root = get_document_root($src);

// get path to image on file system
if(!preg_match('/http\:\/\//',$src)){
    $src = $doc_root . '/' . $src;
}
$new_width = preg_replace( "/[^0-9]+/", "", get_request( 'w', 100 ) );
$new_height = preg_replace( "/[^0-9]+/", "", get_request( 'h', 100 ) );
$zoom_crop = preg_replace( "/[^0-9]+/", "", get_request( 'zc', 1 ) );
$quality = preg_replace( "/[^0-9]+/", "", get_request( '9', 80 ) );

// set path to cache directory (default is ./cache)
// this can be changed to a different location
$cache_dir = './cache';

// get mime type of src
$mime_type = mime_type($src);

// check to see if this image is in the cache already
//check_cache($cache_dir, $mime_type);

// make sure that the src is gif/jpg/png
if(!valid_src_mime_type($mime_type)) {
    die("Invalid src mime type: $mime_type");
}

// check to see if GD function exist
if(!function_exists('imagecreatetruecolor')) {
    die("GD Library Error: imagecreatetruecolor does not exist");
}

if(strlen($src)) {

    // open the existing image
    $image = open_image($mime_type, $src);
    if($image === false) {
        die('Unable to open image : ' . $src);
    }

    // Get original width and height
    $width = imagesx($image);
    $height = imagesy($image);

    // don't allow new width or height to be greater than the original
    if( $new_width > $width ) {
        $new_width = $width;
    }
    if( $new_height > $height ) {
        $new_height = $height;
    }

    // generate new w/h if not provided
    if( $new_width && !$new_height ) {
    
        $new_height = $height * ( $new_width / $width );
        
    } elseif($new_height && !$new_width) {
    
        $new_width = $width * ( $new_height / $height );
        
    } elseif(!$new_width && !$new_height) {
    
        $new_width = $width;
        $new_height = $height;
        
    }
    
    // create a new true color image
    $canvas = imagecreatetruecolor( $new_width, $new_height );

    if( $zoom_crop ) {

        $src_x = $src_y = 0;
        $src_w = $width;
        $src_h = $height;

        $cmp_x = $width  / $new_width;
        $cmp_y = $height / $new_height;

        // calculate x or y coordinate and width or height of source

        if ( $cmp_x > $cmp_y ) {

            $src_w = round( ( $width / $cmp_x * $cmp_y ) );
            $src_x = round( ( $width - ( $width / $cmp_x * $cmp_y ) ) / 2 );

        } elseif ( $cmp_y > $cmp_x ) {

            $src_h = round( ( $height / $cmp_y * $cmp_x ) );
            $src_y = round( ( $height - ( $height / $cmp_y * $cmp_x ) ) / 2 );

        }
        
        imagecopyresampled( $canvas, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h );

    } else {

        // copy and resize part of an image with resampling
        imagecopyresampled( $canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

    }
        
    // output image to browser based on mime type
    show_image( $mime_type, $canvas, $quality, $cache_dir );
    
    // remove image from memory
    imagedestroy( $canvas );
    
} else {

    if(strlen($src)) {
        die($src . ' not found.');
    } else {
        die('no source specified.');
    }
    
}

function show_image( $mime_type, $image_resized, $quality, $cache_dir ) {

    // check to see if we can write to the cache directory
    $is_writable = 0;
    $cache_file_name = $cache_dir . '/' . get_cache_file();            

    if(@touch($cache_file_name)) {
    
        // give 666 permissions so that the developer 
        // can overwrite web server user
        chmod($cache_file_name, 0666);
        $is_writable = 1;
        
    } else {
    
        $cache_file_name = NULL;
        header('Content-type: ' . $mime_type);
        
    }
    
    if(stristr($mime_type, 'gif')) {
    
        imagegif($image_resized, $cache_file_name);
        
    } elseif(stristr($mime_type, 'jpeg')) {
    
        imagejpeg($image_resized, $cache_file_name, $quality);
        
    } elseif(stristr($mime_type, 'png')) {
    
        $quality = floor($quality * 0.09);        
        imagepng($image_resized, $cache_file_name, $quality);
        
    }
    
    if($is_writable) {
        show_cache_file( $cache_dir, $mime_type );
    }

    die();

}

function get_request( $property, $default = 0 ) {
    
    if( isset($_REQUEST[$property]) ) {
        return $_REQUEST[$property];
    } else {
        return $default;
    }
    
}

function open_image($mime_type, $src) {

    if(stristr($mime_type, 'gif')) {
    
        $image = imagecreatefromgif($src);
        
    } elseif(stristr($mime_type, 'jpeg')) {
    
        @ini_set('gd.jpeg_ignore_warning', 1);
        $image = imagecreatefromjpeg($src);
        
    } elseif( stristr($mime_type, 'png')) {
    
        $image = imagecreatefrompng($src);
        
    }
    
    return $image;

}

function mime_type($file) {

    $os = strtolower(php_uname());
    $mime_type = '';

    // use PECL fileinfo to determine mime type
    if( function_exists('finfo_open')) {
        $finfo = finfo_open(FILEINFO_MIME);
        $mime_type = finfo_file($finfo, $file);
        finfo_close($finfo);
    }

    // try to determine mime type by using unix file command
    // this should not be executed on windows
    if(!valid_src_mime_type($mime_type) && !(strstr('windows', $os))) {
        if(preg_match("/freebsd|linux/", $os)) {
            $mime_type = trim(@shell_exec('file -bi $file'));
        }
    }

    // use file's extension to determine mime type
    if(!valid_src_mime_type($mime_type)) {

        // set defaults
        $mime_type = 'image/jpeg';
        // file details
        $fileDetails = pathinfo($file);
        $ext = strtolower($fileDetails["extension"]);
        // mime types
        $types = array(
             'jpg'  => 'image/jpeg',
             'jpeg' => 'image/jpeg',
             'png'  => 'image/png',
             'gif'  => 'image/gif'
         );
        
        if(strlen($ext) && strlen($types[$ext])) {
            $mime_type = $types[$ext];
        }
        
    }
    
    return $mime_type;

}

function valid_src_mime_type($mime_type) {

    if(preg_match("/jpg|jpeg|gif|png/i", $mime_type)) {
        return true;
    }
    return false;

}

function check_cache($cache_dir, $mime_type) {

    // make sure cache dir exists
    if(!file_exists($cache_dir)) {
        // give 777 permissions so that developer can overwrite
        // files created by web server user
        mkdir($cache_dir);
        chmod($cache_dir, 0777);
    }

    show_cache_file($cache_dir, $mime_type);

}

function show_cache_file($cache_dir, $mime_type) {

    $cache_file = $cache_dir . '/' . get_cache_file();

    if( file_exists( $cache_file ) ) {
        
        if( isset( $_SERVER[ "HTTP_IF_MODIFIED_SINCE" ] ) ) {
        
            // check for updates
            $if_modified_since = preg_replace( '/;.*$/', '', $_SERVER[ "HTTP_IF_MODIFIED_SINCE" ] );                    
            $gmdate_mod = gmdate( 'D, d M Y H:i:s', filemtime( $cache_file ) );
            
            if( strstr( $gmdate_mod, 'GMT' ) ) {
                $gmdate_mod .= " GMT";
            }
            
            if ( $if_modified_since == $gmdate_mod ) {
                header( "HTTP/1.1 304 Not Modified" );
                exit;
            }

        }
        
        $fileSize = filesize($cache_file);
                
        // send headers then display image
        header("Content-Type: " . $mime_type);
        //header("Accept-Ranges: bytes");
        header("Last-Modified: " . gmdate('D, d M Y H:i:s', filemtime($cache_file)) . " GMT");
        header("Content-Length: " . $fileSize);
        header("Cache-Control: max-age=9999, must-revalidate");
        header("Expires: " . gmdate("D, d M Y H:i:s", time() + 9999) . "GMT");
        
        readfile($cache_file);
        
        die();

    }
    
}

function get_cache_file () {

    global $quality;

    static $cache_file;
    if(!$cache_file) {
        $frags = explode(".", $_REQUEST['src'] );
        $ext = strtolower( $frags[ count( $frags ) - 1 ] );
        if(!valid_extension($ext)) { $ext = 'jpg'; }
        $cachename = get_request( 'src', 'timthumb' ) . get_request( 'w', 100 ) . get_request( 'h', 100 ) . get_request( 'zc', 1 ) . get_request( '9', 80 );
        $cache_file = md5( $cachename ) . '.' . $ext;
    }
    return $cache_file;

}

function valid_extension ($ext) {

    if( preg_match( "/jpg|jpeg|png|gif/i", $ext ) ) return 1;
    return 0;

}

function clean_source ( $src ) {

    // remove http/ https/ ftp
    //$src = preg_replace("/^((ht|f)tp(s|):\/\/)/i", "", $src);
    // remove domain name from the source url
    $host = $_SERVER["HTTP_HOST"];
    $src = str_replace($host, "", $src);
    $host = str_replace("www.", "", $host);
    $src = str_replace($host, "", $src);
    
    //$src = preg_replace( "/(?:^\/+|\.{2,}\/+?)/", "", $src );
    //$src = preg_replace( '/^\w+:\/\/[^\/]+/', '', $src );

    // don't allow users the ability to use '../' 
    // in order to gain access to files below document root

    // src should be specified relative to document root like:
    // src=images/img.jpg or src=/images/img.jpg
    // not like:
    // src=../images/img.jpg
    $src = preg_replace( "/\.\.+\//", "", $src );

    return $src;

}

function get_document_root ($src) {
    if( @file_exists( $_SERVER['DOCUMENT_ROOT'] . '/' . $src ) ) {
        return $_SERVER['DOCUMENT_ROOT'];
    }
    // the relative paths below are useful if timthumb is moved outside of document root
    // specifically if installed in wordpress themes like mimbo pro:
    // /wp-content/themes/mimbopro/scripts/timthumb.php
    $paths = array( '..', '../..', '../../..', '../../../..' );
    foreach( $paths as $path ) {
        if( @file_exists( $path . '/' . $src ) ) {
            return $path;
        }
    }

}

?>

Upload the above file into your web directory (theme root), now edit your theme files a bit. Let me guide you through. From what I read on this thread, you want the first image to be auto grabbed. Well, do this.

Open your theme's functions.php file and add the following to it:
Code:
//Get the First Image
function catch_that_image() {
global $post, $posts;
$first_img = '';
$url = get_bloginfo('url');
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
 
$not_broken = @fopen("$first_img","r"); // checks if the image exists
if(empty($first_img) || !($not_broken)){ //Defines a default image
unset($first_img);
} else {
$first_img = str_replace($url, '', $first_img);
}
return $first_img;
}

Then edit your theme file, basically index.php where you want to display your first resized image
Code:
<?php $cti = catch_that_image(); if(isset($cti)){ ?>
<img src="<?php bloginfo('template_url'); ?>/timthumb.php?src=<?php echo $cti; ?>&h=60&w=60&zc=1" alt="Link to <?php the_title(); ?>" class="thumbnail"/>
<?php } else {} ?>

Thats it (Y)



__________________
Added after 9 minutes:

Also, don't forget to change the default size for the re-sized images :D
 
Last edited:
ok I will upload it in theme root not in /wp-content/plugins ?

or here? /wp-content/themes

or here: wp-content/themes/BlueCorner

thank you so much proto will try it.
 
or here: wp-content/themes/BlueCorner

There :D and by "Edit index.php" I mean not your wordpress index.php thats in the root :P The theme's index.php :D

hope you got it all right :D
 
or you may also use wordpress custom fields, in this you just have to put the url of that thumb. you just need to put these snippets
Code:
<?php $thumb = get_post_meta($post => ID, 'thumb', true); ?>
<a href="anylink"><img src="<?php echo $thumb; ?>"></a>
"thumb" is your custom field
 
now I get these after editing index.php in wp-content/themes/BlueCorner

Parse error: syntax error, unexpected '<' in /home/blahblah/public_html/domain.org/wp-content/themes/BlueCorner/index.php on line 3

I put it in the top in index.php

[slide]http://www.imgcafe.com/view/uploads/cpanelrmr.jpg[/slide]

Please help.
 
Status
Not open for further replies.
Back
Top