/    Sign up×
Community /Pin to ProfileBookmark

Thumbnailing to a specific size

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;
}
?>

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @mateobus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...