Creating Image Thumbnails

Discussion in 'PHP' started by MrQuidam, Feb 6, 2010.

  1. #1
    Hey,

    I was wondering if anyone knew a way to create an image thumbnail of a randomly sized image.

    I currently have it setup so that images can be uploaded, but I would like to create a second image that crops the image from the center out and makes a 38x38 thumbnail of it.

    Because the image uploaded could be any dimension, simply resizing it to 38x38 wouldn't work, and that is where I need the help.

    So the goal is to have an image uploaded as something like 200x143 and then have a second image created from that which starts at the center of it, and goes out 18px in each direction to make a 38x38 image.

    If I didn't explain that well enough, an example of what I want is like Twitter's user avatars. You upload an image with any dimension, and then your posts are accompanied by a smaller version of it that has been cropped in order to keep ratio of the original and shrink the image to correctly fit.

    Any help would be greatly appreciated!
     
    MrQuidam, Feb 6, 2010 IP
  2. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    handily enough i just went through this and know just what u need. for speedy image translation of all types look into image magick. they make proper image resampling at reasonable speeds for almost all languages. but for php the gd libraries work just fine for png and jpeg formats. a code that will work fine for resizing is
    if ($ext === "jpg"){
    $file = imagecreatefromjpeg($name);
    }
    if ($ext === "png"){
    $file = imagecreatefrompng($name);
    }

    if($ext === "gif") {
    $file = imagecreatefromgif($name);
    }
    $old_x = imageSX($file);
    $old_y = imagesy($file);
    if ($old_x > $old_y) {
    $width=$new_w;
    $height=round($old_y*($new_h/$old_x));
    }
    if ($old_x < $old_y) {
    $width=round($old_x*($new_w/$old_y));
    $height=$new_h;
    }
    if ($old_x == $old_y) {
    $width=$new_w;
    $height=$new_h;
    }
    uhh file int the file name and location ext is the extention.
    other reasources that will help are...

    http://www.imagemagick.org/script/index.php
    http://php.net/manual/en/book.image.php

    hope that helps. you'll still need some more code for proper thumbnails.
     
    dmanto2two, Feb 6, 2010 IP
  3. MrQuidam

    MrQuidam Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Looking at the code snippet, wouldn't that just set the new height and width for an image, keeping the image's ratio? Pretty much do the same thing as css' max-height/max-width?

    I guess that would create the properly sized image so that when cropped, it wouldn't have a chance of one dimension being smaller than the cropped area, correct?

    So after that, I'm getting that I just need to find a way to crop an image from the center out.

    Or am I just totally off?
     
    MrQuidam, Feb 6, 2010 IP
  4. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ohh srry i mis read. tought u wanted it resized to that size. best i can think of is to crop the top and bottom off of the photo using the rations in the first code. you can set the code to subtract the needed amounts from iton the top and bottom. i think. i'm not sure if it will crop of the right parts. but either way your going to want image magick for this. try image magick this
    http://www.php.net/manual/en/imagick.setup.php
    heres a crash course.
    http://articles.sitepoint.com/article/dynamic-images-imagemagick#
    and theres always the php manual
    i gotta run but i'll think on it for what its worth:)
     
    dmanto2two, Feb 6, 2010 IP
  5. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    btw i dont think u want to crop from the center out but from the out in.
     
    dmanto2two, Feb 6, 2010 IP
  6. MrQuidam

    MrQuidam Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I pretty much got what I wanted. I had to look around the GD php page, but I found what I was looking for: imagecopyresized()

    I edited your top code a little:

    if($old_x > $old_y) {
    	$height = $new_h;
    	$width = round($new_w*($old_x/$old_y));
    } else if($old_x < $old_y) {
    	$width = $new_w;
    	$height = round($new_h*($old_y/$old_x));
    } else {
    	$width=$new_w;
    	$height=$new_h;
    }
    PHP:
    And then added:

    imagecopyresized($dest, $src, 0, 0, 0, 0, $width, $height, $old_x, $old_y);
    PHP:
    All seems to work! The only problem is that it resizes it from the outside in, so I don't get the center of the image, instead I get it from the top left corner. So I get some off centered images.

    I'm sure there is a way to fix that, but I still don't really understand imagecopyresized()'s x and y cord parameters.
     
    MrQuidam, Feb 7, 2010 IP
  7. beacon

    beacon Peon

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
  8. MrQuidam

    MrQuidam Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Read through the class. I didn't see any documentation, so I just skimmed through the comments.

    From what I read, it doesn't look like it will crop the image correctly to fit the size, but I could be wrong.

    Actually, _resize() looked like it might do it, but it isn't very clear if it does or not. I just saw the imagecopyresized() being used, so I assume it might.
     
    MrQuidam, Feb 7, 2010 IP
  9. hostgenius

    hostgenius Guest

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    http://phpthumb.sourceforge.net/

    is really good for this.

    it has ALOT of functionalities, and will sure do your thing with less programming effort.

    I use it in many of my client's websites.
     
    hostgenius, Feb 8, 2010 IP