I downloaded this free image resizing script and it's great, but I need it to do a couple more things. I'm a perl guy so I need some help here.
This script takes a file, uploads to my server, resizes it, deletes the temp. copy it made, and prompts me to download the resulting resized file.
Instead of prompting me to download the file, I need it to copy the resulting resized image file to a predefined path on the server (set in the script), with a defined filename (that I will fill in the form each time I use it), then give me a confirmation screen with the filename, new image width, new image height, and a thumb of the image.
Seems simple enough. Can someone help me out?
Thanks.
T.
P.S. I'm going to need to run 2 copies of this script because I'm also going to be creating a thumbnail of each graphic. It would be slick to have the thumbnail made at the same time with a maxheight of 100, then copied to the same path with -thumb attached to the filename. I know I'm pushing it now.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if($_FILES['image']) {
preg_match('/\.([A-Za-z]+?)$/', $_FILES['image']['name'], $matches);
if($matches[1] == 'png' && function_exists('imagecreatefrompng') || $matches[1] == 'jpg' && function_exists('imagecreatefromjpeg') || $matches[1] == 'jpeg' && function_exists('imagecreatefromjpeg') || $matches[1] == 'gif' && function_exists('imagecreatefromgif') || $matches[1] == 'bmp' && function_exists('imagecreatefromwbmp')) {
list($owidth, $oheight) = getimagesize($_FILES['image']['tmp_name']);
if($_POST['resizeby'] == 'height') {
$nheight = ($_POST['size']>1)?$_POST['size']:600;
$nwidth = $nheight / $oheight * $owidth;
$resized = imagecreatetruecolor($nwidth, $nheight);
}
else {
$nwidth = ($_POST['size']>1)?$_POST['size']:800;
$nheight = $nwidth / $owidth * $oheight;
$resized = imagecreatetruecolor($nwidth, $nheight);
}
if($matches[1] == 'png')
$original = imagecreatefrompng($_FILES['image']['tmp_name']);
if($matches[1] == 'jpg' || $matches[1] == 'jpeg')
$original = imagecreatefromjpeg($_FILES['image']['tmp_name']);
if($matches[1] == 'gif')
$original = imagecreatefromgif($_FILES['image']['tmp_name']);
if($matches[1] == 'bmp')
$original = imagecreatefromwbmp($_FILES['image']['tmp_name']);
imagecopyresampled($resized, $original, 0, 0, 0, 0, $nwidth, $nheight, $owidth, $oheight);
header('Content-Disposition: attachment; filename="'.$_FILES['image']['name'].'"');
header('Content-type: image/'.(($matches[1] == 'jpg')?'jpeg':$matches[1]));
if($matches[1] == 'png')
imagepng($resized);
if($matches[1] == 'jpg' || $matches[1] == 'jpeg')
imagejpeg($resized);
if($matches[1] == 'gif')
imagegif($resized);
if($matches[1] == 'bmpg')
imagewbmp($resized);
exit();
} else
$error = 'File type not supported!';
} else
$error = 'No image uploaded!';
}
?>
<?php print '<'.'?xml version="1.0" encoding="UTF-8"?'.'>'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>Resize Image</title>
<link rel="stylesheet" href="resize.css" type="text/css" />
</head>
<body>
<h1>Resize Image</h1>
<?php
if($error)
print '<p>'.$error.'</p>';
?>
<form action="" method="post" enctype="multipart/form-data" >
<p>Upload your JPG, JPEG, GIF, PNG or BMP image: <br />
<input type="file" name="image" /></p>
<!-- Begin BidVertiser code -->
<SCRIPT LANGUAGE="JavaScript1.1"></SCRIPT>
<!-- End BidVertiser code -->
<p>Choose a height or width you want your image to be: <br />
<select name="resizeby">
<option value="width" selected="true">Width:</option>
<option value="height">Height:</option>
</select>
<input type="text" name="size" id="size" value="300" /> pixels</p>
<p><input type="submit" value="Submit" id="submit" /></p>
</form>
</body>
</html>