How to resize image, not crop on WP 2.9.2

Discussion in 'WordPress' started by mixtaka, Mar 12, 2010.

  1. #1
    I use this script on funtion.php to retrieve first image of post on home page as an post thumbnail. the script will work same as i want, but the post image will crop not resize. can anyone tell me what modification i do in this code, so the image will resize.
    im new in wordpress

    // Retrieve the src of first image in a post
    function bm_extract_string($start, $end, $original) {
    $original = stristr($original, $start);
    $trimmed = stristr($original, $end);
    return substr($original, strlen($start), -strlen($trimmed));
    }
    function getFirstImage() {
    $content = get_the_content();
    $pic_string = bm_extract_string('src="','" ',$content);
    $imagesize = getimagesize($pic_string);
    list($width, $height, $type, $attr) = getimagesize($pic_string);
    $link = get_permalink();
    $title = get_the_title($post->post_title);
    echo '<a href="'.$link.'" style="background:url('.$pic_string.'); display:block; width:'.$width.'px; height:'.$height.'px;" title="'.$title.'"></a>';
    }
    
    Code (markup):
    than add this code on index.php

       <?php getFirstImage(); ?>
    
    
    Code (markup):

     
    mixtaka, Mar 12, 2010 IP
  2. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #2
    add a class to the image, and set up the css like this:

    
    .thumb
    {
    max-width:150px;
    max-heght:150px;
    }
    
    Code (markup):
    not the answer u were looking for but a efficent workaround. But looking over that code, it should not crop it, it should resize it. I believe.

    This is the same code with the max width and max height rather than width and height. It might work. Using it you will not need to add a class like I mentioned above, because we are building the conditions into the style tag. Just plug and play.
    
    // Retrieve the src of first image in a post
    function bm_extract_string($start, $end, $original) {
    $original = stristr($original, $start);
    $trimmed = stristr($original, $end);
    return substr($original, strlen($start), -strlen($trimmed));
    }
    function getFirstImage() {
    $content = get_the_content();
    $pic_string = bm_extract_string('src="','" ',$content);
    $imagesize = getimagesize($pic_string);
    list($width, $height, $type, $attr) = getimagesize($pic_string);
    $link = get_permalink();
    $title = get_the_title($post->post_title);
    echo '<a href="'.$link.'" style="background:url('.$pic_string.'); display:block; max-width:'.$width.'px; max-height:'.$height.'px;" title="'.$title.'"></a>';
    }
    
    Code (markup):
     
    adbox, Mar 13, 2010 IP
  3. Wp-Mod.Com

    Wp-Mod.Com Peon

    Messages:
    381
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    or try using timthumb, scrip that works , fine in most cases for me atleast , resizes
     
    Wp-Mod.Com, Mar 14, 2010 IP
  4. digital_helen

    digital_helen Greenhorn

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    yes you can simply re-size any image anywhere by inserting that mentioned simple width and height code in it.

    Hope it will help
     
    digital_helen, Mar 14, 2010 IP
  5. mixtaka

    mixtaka Active Member

    Messages:
    267
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #5

    I add max width and max height rather than width and height. after this none of the image will show.
    i also try many plugins but none of work same as i want.

    i use the theme - junkie channel, this is the official site of the theme : http://www.theme-junkie.com/themes/channel/

    In this theme, the auto post thumbnail is already activated and the image will crop not resize, this theme did not accept external images, and all of my blog - http://www.movieflake.com/ post have external images thats why i use this code.

    i also put this question in wordpress official forum and they reply me, visit this- http://codex.wordpress.org/Function_Reference/wp_get_attachment_image

    but i did not understand anything, hope these information help you, to help me.

    Thanks
     
    mixtaka, Mar 14, 2010 IP