PHP Code:
function generate_thumbs($fname,$link_id,$settings,$orig_id,$only_size='')
{
global $db, $current_user;
if (!($str = @file_get_contents($fname))) return "Can't read file $fname";
if (!($img = @imagecreatefromstring($str)))
return;
else
$db->query("UPDATE ".table_prefix."files SET file_ispicture=1 WHERE file_id='$orig_id'");
if (!$settings['thumb']) return;
if (!$settings['sizes']) return;
$thumb_dir = mnmpath . $settings['thdirectory'];
// load image and get image size
$width = imagesx( $img );
$height = imagesy( $img );
$error = '';
foreach ($settings['sizes'] as $size)
{
if (!strstr($size,'x') || ($only_size && $only_size!=$size)) continue;
list($maxw,$maxh) = split('[x]',$size);
if ($maxw <= 0 || $maxh <= 0) continue;
// Thumbnail file name
if (preg_match('/([^\/]+)\.[^\/]+$/',$fname,$m) || preg_match('/([^\/]+)$/',$fname,$m))
$name = $m[1];
else
$name = $fname;
$name = "$name$size";
// calculate thumbnail size
$new_width = $maxw;
$new_height = $maxh;
$ratio_w=1.0 * $new_width / $width;
$ratio_h=1.0 * $new_height / $height;
$ratio=1.0;
if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1))
{
if($ratio_w < $ratio_h) {
$ratio = $ratio_h;
}else {
$ratio = $ratio_w;
}
$inter_w=(int)($new_width / $ratio);
$inter_h=(int) ($new_height / $ratio);
$inter_img=imagecreatetruecolor($inter_w , $inter_h);
imagecopy($inter_img, $img, 0,0,0,0,$inter_w,$inter_h);
$new_img=imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($new_img,$inter_img,0,0,0,0,$new_width,$new_height,$inter_w,$inter_h);
while (file_exists("$thumb_dir/$name$i.jpg")) $i++;
$name = "$name$i.jpg";
if (!imagejpeg( $new_img, "$thumb_dir/$name",$settings['quality'] ))
$error .= "Can't create thumbnail $thumb_dir/$name";
}
else
{
$ratio=$ratio_h>$ratio_w? $ratio_h : $ratio_w;
$inter_w=(int)($width * $ratio);
$inter_h=(int) ($height * $ratio);
$inter_img=imagecreatetruecolor($inter_w , $inter_h);
imagecopyresampled($inter_img,$img,0,0,0,0,$inter_w,$inter_h,$width,$height);
$new_img=imagecreatetruecolor($new_width,$new_height);
imagecopy($new_img, $inter_img, 0,0,0,0,$new_width,$new_height);
while (file_exists("$thumb_dir/$name$i.jpg")) $i++;
$name = "$name$i.jpg";
if (!imagejpeg( $new_img, "$thumb_dir/$name",$settings['quality'] ))
$error .= "Can't create thumbnail $thumb_dir/$name";
}
if($error == '')
$db->query("INSERT INTO ".table_prefix."files
SET file_size='$size',
file_orig_id='$orig_id',
file_user_id={$current_user->user_id},
file_link_id=$link_id,
file_ispicture=1,
file_real_size='".filesize("$thumb_dir/$name")."',
file_name='".$db->escape($name)."'");
}
return $error;
}
Save and upload, upload a new image(One condition:your upload image's width and height should greater than the thumb size you've set) and see what's the difference:tongue:.