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
https://forums.phpfreaks.com/topic/32030-thumbnail-sp/
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
https://forums.phpfreaks.com/topic/32030-thumbnail-sp/#findComment-148848
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.