Jump to content

Thumbnailing to a specific size


Mateobus

Recommended Posts

OK i have found the following code which works for me.  However, i want to thumbnail the images to a specific size, so they will look nicer when displayed on my page.  If I use just width and height attributes on the html img the picture will obviously be distorted unless it fits those proportions.  Is there a way to create a thumbnail of specific length and width that crops a small piece of the picture (either vertically or horizontally)?  That would be awesome.  Any help is appreciated.

<?php
function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {
    $s_path = trim($s_path);
    $o_path = trim($o_path);
    $save = $s_path . $save;
    $file = $o_path . $file;
    $ext = strtolower(end(explode('.',$save)));
    list($width, $height) = getimagesize($file) ;
    if(($width>$t_w) OR ($height>$t_h)) {
        $r1 = $t_w/$width;
        $r2 = $t_h/$height;
        if($r1<$r2) {
          $size = $t_w/$width;
        }else{
          $size = $t_h/$height;
        }
    }else{
        $size=1;
    }
    $modwidth = $width * $size;
    $modheight = $height * $size;
    $tn = imagecreatetruecolor($modwidth, $modheight) ;
    switch ($ext) {
        case 'jpg':
        case 'jpeg':
                    $image = imagecreatefromjpeg($file) ;
        break;
        case 'gif':
                    $image = imagecreatefromgif($file) ;
        break;
        case 'png':
                    $image = imagecreatefrompng($file) ;
        break;
    }
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
    imagejpeg($tn, $save, 100) ;
    return;
}
?>
Link to comment
Share on other sites

I don't know of any way to do this, however, there is another option.

Resize your image to the largest size possible that will fit within the constraints of your thumbnail size while keeping the original proportions.


For example, lets say you want a thumbnail that is 100*200 and your original image is 600*300. 
thumb_width=100
thumb_height=200
thumbnail_ratio=thumb_width/thumb_height (0.5)

orig_width=600
orig_height=300
original_ratio=orig_width/orig_height (2)

if(original_ratio<thumbnail_ratio)
    new_width=(thumb_height/orig_height)*orig_width
    new_height=thumb_height
else
    new_height=(thumb_width/orig_width)*orig_height
    new_width=thumb_width

so long as you don't have images width very large or very small width/height ratios, this can work fairly well
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.