I'm having a bit of trouble. Most likely because I am horrible at Math, but anyway... I have this code that creates thumbnails to dimensions that I've set. However, it only creates the thumbnails in those dimensions without any scaling. What I'd like to know how to do is just how do I make it so that I can scale the thumbnails?
I know that I have to have the size of the original image... and divide it by something, but I'm lost. I've been using the search function on this forum trying to find something similar, and I have, but I just can't make heads or tails of it. Any help would be greatly appreciated. Here's the thumbnails code that I have so far.
if('admin.upload.php' == basename($_SERVER['SCRIPT_FILENAME']))
die('Go away.');
if($_POST[title] == '' || $_POST[descr] == '' || !$_FILES['picturefile']['name'])
die('Please make sure you have filled in all the required fields!');
if(!$_FILES['thumbfile']['name'] && get_setting(createthumbs) == no)
die('As you have set Galleristic <b>not</b> to create thumbnails, the thumbnail field is required! Please go back
and attach a thumbnail.');
$imgdir = get_setting(imgdir);
$thumbdir = get_setting(thumbdir);
$id = get_recent_image('id') + 1;
$extension = explode('.',$_FILES['picturefile']['name']);
$extension = array_pop($extension);
$thumbext = substr($_FILES['thumbfile']['name'],-3 );
$allowedext = explode(' ',get_setting(extensions));
$filename = $id . '.' . $extension;
$uploaddir = get_setting('absolutepath').'/'.$imgdir.'/'.$filename;
$thumbwidth = get_setting('thumbwidth');
$thumbheight = get_setting('thumbheight');
$date = date('Y-m-d');
if(!$_FILES['thumbfile']['name'] && $extension == swf)
die('This is a flash file! Please go back and attach a thumbnail.');
if($_FILES['thumbfile']['name'] && !in_array($thumbext,$allowedext)){
die('The format specified is not allowed! You can change allowed file formats in Options.');
}
if(in_array($extension,$allowedext)){
echo('Uploading image...');
if (move_uploaded_file($_FILES['picturefile']['tmp_name'], $uploaddir)) {
print "Image was successfully uploaded! <br />";
} else {
print "<pre>Failed uploading picture! Here's some debugging info:\n";
print_r($_FILES);
die('</pre>');
}
if($_FILES['thumbfile']['name']){
$size = getimagesize($_FILES['thumbfile']['tmp_name']);
if($size[0] > $thumbwidth && get_setting(createthumbs) == no)
die('The thumbnail image is bigger than the max thumbnail size. Please go back and select a smaller thumbnail.');
elseif($size[1] > $thumbheight && get_setting(createthumbs) == no)
die('The thumbnail image is bigger than the max thumbnail size. Please go back and select a smaller thumbnail.');
else{
if (move_uploaded_file($_FILES['thumbfile']['tmp_name'], get_setting('absolutepath').'/'.$thumbdir.'/'.$filename)) {
print "Thumbnail uploaded..<br />";
} else {
print "<pre>Failed uploading picture! Here's some debugging info:\n";
print_r($_FILES);
die('</pre>');
}
}
if($size[1] > $thumbheight || $size[0] > $thumbwidth && get_setting(createthumbs) == yes)
exec("convert -size 500x600 -resize ".$thumbwidth."x".$thumbheight."+0+0 $thumbdir/$filename $thumbdir/$filename");
}
else{
echo('Creating thumbnail... ');
if(exec("convert -size 500x600 -resize ".$thumbwidth."x".$thumbheight."+0+0 $imgdir/$filename $thumbdir/$filename")){
echo('Thumbnail created!<br />');
} else {
if (function_exists('ImageCopyResized')) { // If GD is enabled..
if($extension == jpg || $extension == jpeg){
$img1=ImageCreate($thumbwidth,$thumbheight) or die('Could not create image!');
$img2=ImageCreateFromJPEG($imgdir.'/'.$filename) or die('Could not open uploaded image!');
ImageCopyResized($img1,$img2,0,0,0,0,$thumbwidth,$thumbheight,ImageSX($img2),ImageSY($img2)) or die('Could not resize image!');
ImageJPEG($img1,$thumbdir.'/'.$filename) or die('Could not save image!');
echo('Thumbnail created!<br />');
} elseif($extension == png) {
$img1=ImageCreate($thumbwidth,$thumbheight) or die('Could not create image!');
$img2=ImageCreateFromPNG($imgdir.'/'.$filename) or die('Could not open uploaded image!');
ImageCopyResized($img1,$img2,0,0,0,0,$thumbwidth,$thumbheight,ImageSX($img2),ImageSY($img2)) or die('Could not resize image!');
ImagePNG($img1,$thumbdir.'/'.$filename) or die('Could not save image!');
echo('Thumbnail created!<br />');
}
}
}
}
echo('Now adding info into the database.. <br />');
mysql_query("INSERT INTO `$tablenamep` ( `id` , `description` , `title` , `filename` , `dimensions` , `artist` , `category` , `hits` , `picdate` )VALUES ('', '$_POST[descr]', '$_POST[title]', '$filename', '', '$_POST[artist]', '$_POST[category]', 0, '$date')") or die(mysql_error());
echo('Success! Your image has been added.<br />
<a href="admin.php">» Go back</a><br /><br />');
echo('<img src="'.get_setting('siteurl').'/'.$imgdir.'/'.$filename.'" /> ');
echo('<img src="'.get_setting('siteurl').'/'.$thumbdir.'/'.$filename.'" />');
} else {
echo('The format specified is not allowed! You can change allowed file formats in Options.');
}