I was curious and created an online version and was disappointed with the PHP GD2 image quality.
I prefer to use ImageMagick
Online Demo
<?php
declare (strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'false');
$TITLE = 'John_Betong\'s ThumbNail Creator';
$forum = '#';
$date = date('l, jS M Y - H:i:s');
$hdr = <<< ____TMP
<!DOCTYPE HTML>
<html lang="en">
<head>
<title> $TITLE </title>
<style>
body{
font: 16px/1.42 Montserrat,BlinkMacSystemFont, -apple-system,
"Segoe UI",Roboto,Helvetica,Arial,sans-serif,
Oxygen,Ubuntu,Cantarell,"Fira Sans","Droid Sans",
"Helvetica Neue",Arial,sans-serif;
/*
font-smooth:always;
-webkit-font-smoothing=antialiased;
*/
border:0; margin:0; padding:0;
}
h1,h2,h3,h4,h5,h6{
font-weight:400;
font-family:-apple-system,BlinkMacSystemFont,
"Segoe UI",Roboto,Oxygen,Ubuntu,Cantarell,"Fira Sans","Droid Sans",
Inconsolata,"Helvetica Neue",Arial,sans-serif;}
a {text-decoration:none;}
.bd8 {border:solid 1px #888;}
.bge {background-color:#eee;}
.btn {margin:0 1em; padding:0.21em 0.42em; background-color:#eee; color:#000;}
.clb {clear:both;}
.flr {float: right;}
.fss {font-size:small;}
.fwb {font-weight:700;}
.mga {margin:0 auto;}
.ooo {margin:0; padding:0;}
.POF {position:fixed; left:0; bottom:0;}
.p42 {padding: 0.42em;}
.tac {text-align:center;}
.w88 {width:88%; max-width:888px;}
</style>
</head>
<body>
<h1>
<a href="?">
$TITLE
<sup class="fss">Source</sup>
</a>
</h1>
<h5 class="ooo"> $date </h5>
<h5 class="flr ooo">
<a href="$forum"> SitePoint Forum </a>
</h5>
<hr class="clb">
____TMP;
echo $hdr;
# SET GET PARAMETER
$imgParam = $_GET['fff'] ?? NULL;
if( isset($_FILES['capture']) ):
# NO PROBLEM
else:
# CREATE FILES
$_FILES['capture'] = [
'capture' => 'capture',
'name' => $imgParam,
'tmp_name' => $imgParam,
'type' => 'type',
'size' => 424242,
];
endif;
# DISPLAY LINKS
getLinksToImages();
if($imgParam):
echo '<div class="w88 mga">';
makeThumbnails($aFILES = $_FILES);
showThumbnails($aFILES['capture']['name']);
echo '</div>';// class="w88 mga">';
else:
echo '<dl>';
echo '<dt>Source: </dt>';
echo '<dd class="bd8 p42">';
highlight_file(__file__);
echo '</dd>';
echo '</dl>';
endif;
echo '<p> <br> </p>';
echo '<div class="POF tac fwb bge">';
echo 'Wonderful place for a footer';
echo '</div>';
echo '</body></html>';
# ONLY FUNCTION BELOW
//=================================================================
function makeThumbnails($aFILES)
{
if($_FILES['capture']['size'] > 0)
{
$fileName = $_FILES['capture']['name'];
$tmpName = $_FILES['capture']['tmp_name'];
$fileSize = $_FILES['capture']['size'];
$fileType = $_FILES['capture']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc()) {
$fileName = addslashes($fileName);
}
$extension = getExtension($fileName);
$extension = strtolower($extension);
# JPG, PNG OR GIF4
if($extension=="jpg" || $extension=="jpeg" )
{
$src = imagecreatefromjpeg($tmpName);
}else if($extension=="png"){
$src = imagecreatefrompng($tmpName);
}else{
$src = imagecreatefromgif($tmpName);
}
list($width, $height) = getimagesize($tmpName);
if ($height<$width){
$newwidth0 = 500; $newheight0 = ($height/$width)*$newwidth0;
$newwidth1 = 300; $newheight1 = ($height/$width)*$newwidth1;
$newwidth2 = 150; $newheight2 = ($height/$width)*$newwidth2;
$newwidth3 = 80; $newheight3 = ($height/$width)*$newwidth3;
}else{
$newheight0 =400; $newwidth0 = ($width/$height)*$newheight0;
$newheight1 =200; $newwidth1 = ($width/$height)*$newheight1;
$newheight2 =150; $newwidth2 = ($width/$height)*$newheight2;
$newheight3 = 80; $newwidth3 = ($width/$height)*$newheight3;
}
$thumb_width = 2100;
$thumb_height = 1150;
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect ){
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}else{
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$imgObj = imagecreatetruecolor( (int) $width, (int) $height );
# CREATE Image Resources
$tmp0 = imagecreatetruecolor( (int) $newwidth0, (int) $newheight0);
$tmp1 = imagecreatetruecolor( (int) $newwidth1, (int) $newheight1);
$tmp2 = imagecreatetruecolor( (int) $newwidth2, (int) $newheight2);
$tmp3 = imagecreatetruecolor( (int) $newwidth3, (int) $newheight3);
# MAYBE BETTER QUALITY
if( function_exists('imageantialias')):
imageantialias($tmp0,true);
imageantialias($tmp1,true);
imageantialias($tmp2,true);
imageantialias($tmp3,true);
endif;
imagecopyresampled($tmp0, $src,0,0,0,0, (int) $newwidth0, (int) $newheight0, $width, $height);
imagecopyresampled($tmp1, $src,0,0,0,0, (int) $newwidth1, (int) $newheight1, $width, $height);
imagecopyresampled($tmp2, $src,0,0,0,0, (int) $newwidth2, (int) $newheight2, $width, $height);
imagecopyresampled($tmp3, $src,0,0,0,0, (int) $newwidth3, (int) $newheight3, $width, $height);
# ENSURE PARAMETERS ARE ALL INTEGERS
$tmp_0 = 0 - ($new_width - $thumb_width) / 2;
$tmp_0 = (int) $tmp_0;
$tmp_1 = 0 - ($new_width - $thumb_width) / 2;
$tmp_1 = (int) $tmp_1;
$tmp_2 = 0 - ($new_height - $thumb_height) / 2;
$tmp_2 = (int) $tmp_2;
$tmp_3 = 0 - ($new_height - $thumb_height) / 2;
$tmp_3 = (int) $tmp_2;
$new_height = (int) $new_height;
$height = (int) $height;
$ok = imagecopyresampled
(
$imgObj,
$src,
$tmp_1,
$tmp_2,
0,
0,
$new_width,
$new_height,
$width,
$height
);
if($ok):
$big = "images/big_" . $_FILES['capture']['name'];
$middle = "images/middle_" . $_FILES['capture']['name'];
$small = "images/small_" . $_FILES['capture']['name'];
$ico = "images/ico_" . $_FILES['capture']['name'];
imagejpeg($tmp0, $big, 100);
imagejpeg($tmp1, $middle,100);
imagejpeg($tmp2, $small, 100);
imagejpeg($tmp3, $ico, 100);
$fp = fopen($big, 'r');
$big = addslashes(fread($fp, filesize($big)));
fclose($fp);
$fp = fopen($middle, 'r');
$middle = addslashes(fread($fp, filesize($middle)));
fclose($fp);
$fp = fopen($small, 'r');
$small = addslashes(fread($fp, filesize($small)));
fclose($fp);
$fp = fopen($ico, 'r');
$ico = addslashes(fread($fp, filesize($ico)));
fclose($fp);
else:
echo 'BIG PROBLEM WITH: ' .$_FILES['capture']['name'];
endif;
# DELETE IMAGES???
if(0):
unlink("images/big_". $_FILES['capture']['name']);
unlink("images/middle_". $_FILES['capture']['name']);
unlink("images/small_". $_FILES['capture']['name']);
unlink("images/ico_". $_FILES['capture']['name']);
endif;
}
}
//=========================================================
function getExtension($file)
{
$file = substr($file, -5);
$extension = strstr( $file, '.' );
$extension = substr( $extension, 1);
return $extension ? $extension : false;
}
//=========================================================
function showThumbnails($img)
{
# CREATE THUMBS
$sizes = ['ico', 'small', 'middle', 'big' ];
foreach( $sizes as $id => $size ):
echo $img .'<br>';
$tmp = 'images/' .$size .'_' .$img;
$dim = getimagesize($tmp);
echo '<img src="' .$tmp .'" alt="' .$tmp .'" /><br>';
echo $dim[3] .'<br><br>';
endforeach;
# ORIGNAL IMAGE
echo '<br>Original image:';
$tmp = $img;
echo '<img src="' .$tmp .'" alt="' .$tmp .'" /><br>';
$dim = getimagesize($tmp);
echo $dim[3] .'<br><br>';
}
//=========================================================
function getLinksToImages()
{
# LOAD IMAGE IN THIS FOLDER
$fileNames = glob('*.*');
echo '<div style="text-align:center;">';
echo '<ul>';
foreach($fileNames as $id => $img):
if( strpos($img, '.php') ):
// PHP IS NOT AN IMAGE
else:
echo '<li style="display:inline-block;"> '
. '<a href="?fff=' .$img .'"><b class="btn">' .$img .'</b></a>'
. '</li>';
endif;
endforeach;
echo '</ul>';
echo '</div>';
}