PHP image resize
Posted: Fri Jan 30, 2009 10:56 am
I am trying to create an upload form where I can upload an image and it make 4 copies of the file, resizes it and puts it into 4 different folders. I found some php that works, but the image quality of the resized images is terrible. Can you make any suggestions on how to resize the images without loosing quality? Below is the php and a before and after of the image for you to look at. I am a total noob at php so any help you can offer would be much appreciated. Thanks!
http://www.michaeldfoley.com/postlet/images/18.jpg
http://www.michaeldfoley.com/postlet/images/high/18.jpg
http://www.michaeldfoley.com/postlet/images/18.jpg
http://www.michaeldfoley.com/postlet/images/high/18.jpg
Code: Select all
<?php
$idir = "images/"; // Path To Images Directory
$hdir = "images/high/"; // Path To Thumbnails Directory
$hheight = "800"; // Maximum Height For Thumbnail Images
$mdir = "images/medium/"; // Path To Thumbnails Directory
$mheight = "600"; // Maximum Height For Thumbnail Images
$ldir = "images/low/"; // Path To Thumbnails Directory
$lheight = "450"; // Maximum Height For Thumbnail Images
$tdir = "images/tiny/"; // Path To Thumbnails Directory
$theight = "300"; // Maximum Height For Thumbnail Images
if (!isset($_GET['subpage'])) { // Image Upload Form Below ?>
<form method="post" action="upload-size.php?subpage=upload" enctype="multipart/form-data">
File:<br />
<input type="file" name="imagefile" class="form">
<br /><br />
<input name="submit" type="submit" value="Submit" class="form"> <input type="reset" value="Clear" class="form">
</form>
<? } else if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') { // Uploading/Resizing Script
$url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use
if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
$file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
print 'Image uploaded successfully.<br /><br />'; // Was Able To Successfully Upload Image
$shimg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$hcurrwidth = imagesx($shimg); // Current Image Width
$hcurrheight = imagesy($shimg); // Current Image Height
$hzoom = $hheight / $hcurrheight; // Length Ratio For Width
$hnewheight = $hheight; // Height Is Equal To Max Height
$hnewwidth = $hcurrwidth * $hzoom; // Creates The New Width
$dhimg = imagecreate($hnewwidth, $hnewheight); // Make New Image For Thumbnail
imagetruecolortopalette($shimg, false, 255); // Create New Color Pallete
$hpalsize = ImageColorsTotal($shimg);
for ($i = 0; $i < $hpalsize; $i++) { // Counting Colors In The Image
$hcolors = ImageColorsForIndex($shimg, $i); // Number Of Colors Used
ImageColorAllocate($dhimg, $hcolors['red'], $hcolors['green'], $hcolors['blue']); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dhimg, $shimg, 0, 0, 0, 0, $hnewwidth, $hnewheight, $hcurrwidth, $hcurrheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dhimg, "$hdir" . $url); // Saving The Image
imagedestroy($shimg); // Destroying The Temporary Image
imagedestroy($dhimg); // Destroying The Other Temporary Image
print 'High image created successfully.<br />'; // Resize successful
$smimg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$mcurrwidth = imagesx($smimg); // Current Image Width
$mcurrheight = imagesy($smimg); // Current Image Height
$mzoom = $mheight / $mcurrheight; // Length Ratio For Width
$mnewheight = $mheight; // Height Is Equal To Max Height
$mnewwidth = $mcurrwidth * $mzoom; // Creates The New Width
$dmimg = imagecreate($mnewwidth, $mnewheight); // Make New Image For Thumbnail
imagetruecolortopalette($smimg, false, 255); // Create New Color Pallete
$mpalsize = ImageColorsTotal($smimg);
for ($i = 0; $i < $mpalsize; $i++) { // Counting Colors In The Image
$mcolors = ImageColorsForIndex($smimg, $i); // Number Of Colors Used
ImageColorAllocate($dmimg, $mcolors['red'], $mcolors['green'], $mcolors['blue']); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dmimg, $smimg, 0, 0, 0, 0, $mnewwidth, $mnewheight, $mcurrwidth, $mcurrheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dmimg, "$mdir" . $url); // Saving The Image
imagedestroy($smimg); // Destroying The Temporary Image
imagedestroy($dmimg); // Destroying The Other Temporary Image
print 'Medium image created successfully.<br />'; // Resize successful
$slimg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$lcurrwidth = imagesx($slimg); // Current Image Width
$lcurrheight = imagesy($slimg); // Current Image Height
$lzoom = $lheight / $lcurrheight; // Length Ratio For Width
$lnewheight = $lheight; // Height Is Equal To Max Height
$lnewwidth = $lcurrwidth * $lzoom; // Creates The New Width
$dlimg = imagecreate($lnewwidth, $lnewheight); // Make New Image For Thumbnail
imagetruecolortopalette($slimg, false, 255); // Create New Color Pallete
$lpalsize = ImageColorsTotal($slimg);
for ($i = 0; $i < $lpalsize; $i++) { // Counting Colors In The Image
$lcolors = ImageColorsForIndex($slimg, $i); // Number Of Colors Used
ImageColorAllocate($dlimg, $lcolors['red'], $lcolors['green'], $lcolors['blue']); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dlimg, $slimg, 0, 0, 0, 0, $lnewwidth, $lnewheight, $lcurrwidth, $lcurrheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dlimg, "$ldir" . $url); // Saving The Image
imagedestroy($slimg); // Destroying The Temporary Image
imagedestroy($dlimg); // Destroying The Other Temporary Image
print 'Low image created successfully.<br />'; // Resize successful
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
$zoom = $theight / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
imagetruecolortopalette($simg, false, 255); // Create New Color Pallete
$palsize = ImageColorsTotal($simg);
for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
print 'Tiny image created successfully.<br />'; // Resize successful
} else {
print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed
}
} else {
print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong
print $file_ext; // Show The Invalid File's Extention
print '.</font>';
}
} ?>