Jump to content

thumbnail sp


corillo181

Recommended Posts

i got this thumbnail maker

what i want to do is give my thumbnail a specifict height and width how can i do that?

this si the code.

[code]$save = "albums/tn_".$name;

$file = $path;
echo "Creating file: $save";
$size = 0.20;
list($width, $height) = getimagesize($file) ;

$modwidth = $width * $size;
$modheight = $height * $size;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
[/code]
Link to comment
Share on other sites

The current code returns an image that is 20% of the original size.  So, instead of using the width & height multiplied by this percent, simply state the height and width:

[code]
$modwidth = 50;
$modheight = 50;
[/code]

However, if you go away from a relative size (like 20%) and only spec in hard numbers for height and width, you might end up with a strange looking thumnail.  So, you can use something like the code below to specify a max height and width for your thumbnail and the actual size is still relative to the original.

[code]
<?php
                    $save = "albums/tn_".$name;

                    $file = $path;
                    echo "Creating file: $save";
                    list($width, $height) = getimagesize($file) ;

                    $max_width = 50;
                    $max_height = 50;

                    //generating smaller image
                    $x_ratio = $max_width / $width;
                    $y_ratio = $max_height / $height;

                    if (($width <= $max_width) && ($height <= $max_height)) {
                      $modwidth = $width;
                      $modheight = $height;
                    }
                    else if (($x_ratio * $height) <= $max_height) {
                      $modheight = ceil($x_ratio * $height);
                      $modwidth = $max_width;
                    }
                    else {
                      $modwidth = ceil($y_ratio * $width);
                      $modheight = $max_height;
                    }



                    $tn = imagecreatetruecolor($modwidth, $modheight) ;
                    $image = imagecreatefromjpeg($file) ;
                    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

?>[/code]
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.