SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    properly resizing avatars/images

    Hello,

    I would like to properly resize images/avatars and keep their quality at the same time. Ideally, I would like to get the same results vBulletin gets when it shows or saves avatars in thread posts. right now I'm using this:

    PHP Code:

    $mysock 
    getimagesize($User_Avatar);
    imageResize($mysock[0], $mysock[1], 75);

    //================================================
     
    function imageResize($width$height$target) {
    //================================================

    //takes the larger size of the width and height and applies the  
    //formula accordingly...this is so this script will work  
    //dynamically with any size image

    if ($width $height) {
    $percentage = ($target $width);
    } else {
    $percentage = ($target $height);
    }

    //gets the new value and applies the percentage, then rounds the value
    $width round($width $percentage);
    $height round($height $percentage);

    //returns the new sizes in html image tag format...this is so you
    //can plug this function inside an image tag and just get the

    return "width=\"$width\" height=\"$height\"";

    }
    //end function 
    can somebody help? thanks

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use a graphic package, e.g. GD, to resize the actual image. See imagecopyresampled

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    For this matter you should use gd library to resize the image and not just force the size in html. A good idea is to do that in advance, upon uploading the avatar, not upon displaying it.

    You've already have it calculating the new size, all you need to do now is create a new image[*]:
    Code php:
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
     
    imagejpeg($image_p, null, 100);
    Saul

  4. #4
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, in fact, I was looking for a solution that would let me save the image with the size I want so I don't need to do any processing upon displaying it. thanks for the samples. I will try them out

  5. #5
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you considered using max-width in your CSS ?

    Unless you have a large number of IE6 or lower visitors still, it should do the trick & require no processing by the server at all.

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    In the blogosphere
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    GD would be a better option. You have smaller files which means less storage / bandwidth consumption.
    bLueFrogX's Blog - Random Ramblings of a NEET Techie ★

  7. #7
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok I tried your code but I'm getting a whole bunch of strange characters. this is my code:

    PHP Code:
    <?php
    include('includes/inc_common.php');

    //================================================
     
    function imageResize1($width$height$target) {
    //================================================

    //takes the larger size of the width and height and applies the  
    //formula accordingly...this is so this script will work  
    //dynamically with any size image

    if ($width $height) {
    $percentage = ($target $width);
    } else {
    $percentage = ($target $height);
    }

    //gets the new value and applies the percentage, then rounds the value
    $width round($width $percentage);
    $height round($height $percentage);

    //returns the new sizes in html image tag format...this is so you
    //can plug this function inside an image tag and just get the

    return "width=\"$width\" height=\"$height\"";

    //================================================
    }//end function
    //================================================

    $UserAvatar $GLOBALS['AvatarsFilePath'] . "test.jpg";
    $mysock getimagesize($UserAvatar);

    $new_width 75;
    $new_height 60;
    $filename $UserAvatar;
    $width $mysock[0];
    $height $mysock[1];

    echo 
    "<img src=\"$UserAvatar\" />";
    echo 
    "<img src=\"$UserAvatar\" " imageResize1($mysock[0], $mysock[1], 75)  . " />";

    $image_p imagecreatetruecolor($new_width$new_height);
    $image imagecreatefromjpeg($filename);
    imagecopyresampled($image_p$image0000$new_width$new_height$width$height);
    imagejpeg($image_pnull100);

    echo 
    "<img src=\"$UserAvatar\" />";

    ?>

  8. #8
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    In the blogosphere
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because that code actually outputs the image. If you wanted to use it that way you should be calling it [code]<img src="phpcode.php" />[code]
    bLueFrogX's Blog - Random Ramblings of a NEET Techie ★

  9. #9
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't mind fobbing off your avatar stuff to a third party provider, you can always try www.gravatar.com

    If you want to host them yourself, please ignore

  10. #10
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fixed the issue. thank you very much!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •