Image resize on the fly... ?
Posted: Wed Jan 06, 2010 5:30 pm
Hi all
newb here - learning as I go.
I've got an upload script that works pretty well and checks for max file size,
file type etc., but I'd really like to add a resize element which would keep aspect
ratio and resize images to a maximum heigh or width (whichever is greater).
Max allowable height is 500px, max width is 800px.
here's my script - any help on what (and where) to add some resizing very much appreciated.
newb here - learning as I go.
I've got an upload script that works pretty well and checks for max file size,
file type etc., but I'd really like to add a resize element which would keep aspect
ratio and resize images to a maximum heigh or width (whichever is greater).
Max allowable height is 500px, max width is 800px.
here's my script - any help on what (and where) to add some resizing very much appreciated.
Code: Select all
<?php
session_start();
if(isset($_GET['id']) || isset($_POST['id']));
else if (isset($_SESSION['id'])) {
$id = $_SESSION['id'];
} else {
include_once "register.php";
exit();
}
$galleryOwner= $id;
include_once "scripts/connect_to_mysql.php";
$id = $_SESSION['id'];
$acceptableextensions = array('jpg','jpeg','png','gif');
$acceptablemimetypes = array('image/jpg','image/jpeg','image/png','image/gif');
$maxfilesize = 256000; // 256000 bytes equals 250kb
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// Initialize vars
$error_msg = "";
$success_msg = "";
$firstname = "";
$lastname = "";
$user_pic = "";
// Parsing section for the picture... only runs if they attempt to upload a picture
if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 0)){
$image = $_FILES['fileField']['name'];
$filename = stripslashes($_FILES['fileField']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
//Filetype check
if (!in_array($extension,$acceptableextensions)){
exit("Upload failed.<BR>Unacceptable file type.<br. Use only jpg, jpeg, png or fig formats");
echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
$errors=1;
//Filesize check
} else if ($_FILES['fileField']['size'] > $maxfilesize ) {
$error_msg = 'Ooops - your image was too large,<br> 250kb Maximum. Please try again.';
unlink($_FILES['fileField']['tmp_name']);
} else {
// rename the picture incrementally
$extension = getExtension($filename);
$extension = strtolower($extension);
$groovy = sizeof(glob("members/$id/images/*"));
$groovy = ++$groovy;
$image_name=$groovy.'.'.$extension;
$newname="".$image_name;
$gowhere = mysql_real_escape_string($_POST['gowhere']);
// INSERT image into the DB
$sql = mysql_query("INSERT INTO pictures
(UID,filename,dirpath,gallery,dtadded)
VALUES ('$id','$newname','/members/$id/images/$newname','$gowhere',now())")
or die (mysql_error());
$place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/images/".$newname);
chmod ("members/$id/images/$newname", 0666);
}
$success_msg = '<span class="Big_Orange_Times">Your image has been uploaded, it may take a few moments to appear in your gallery, please be patient.</span>';
} else if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 4)){
//Error code 4: No image uploaded
$error_msg = 'Please browse for an image before you press Go.';
}
// close else that checks file exists
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Member Upload</title>
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<link rel="icon" href="http://www.mydomain.com/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="http://www.mydomain.com/favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php include_once "header_template.php"; ?>
<br>
<br>
<br>
<br>
<br>
<table width="975" height="366" border="0" align="center">
<td width="44%" height="328"><div align="center">
<table width="100%" height="270" border="0">
<tr>
<td height="322"><div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="426" height="320">
<param name="movie" value="images/required_site_pics/uploadBlurb.swf">
<param name="quality" value="high">
<embed src="images/required_site_pics/uploadBlurb.swf" FlashVars="user_num=<?php echo "$galleryOwner";?>"quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="426" height="320"></embed>
</object>
</div></td>
</tr>
</table>
</div> </td>
<td width="56%" rowspan="2">
<div align="center"><?php include_once "showpics.php"; ?>
<br>
</div></td>
</tr> <tr>
<td height="32"><form action="upload.php" method="post" enctype="multipart/form-data" name="form1">
<table width="70%" border="0" align="center">
<tr>
<td><div align="center">
<input name="fileField" type="file" id="fileField" size="0" maxlength="0">
</div></td>
<td><div align="center">Which Gallery?
<select name="gowhere" id="gowhere">
<option value="portraits" selected>People</option>
<option value="landscapes">Landscapes</option>
<option value="urban">Urban</option>
<option value="travel">Travel</option>
<option value="action">Action</option>
<option value="reportage">Reportage</option>
</select>
</div></td>
<td><div align="center">
<input name="button" type="submit" id="button" value="Go">
<input name="parse_var" type="hidden" value="pic" />
</div></td>
</tr>
</table>
</form></td>
</tr>
</table>
<table width="977" height="191" border="0" align="center">
<tr>
<td><span class="Big_Blue_Times"><?php print "$error_msg"; ?> </span> <div align="center"><br />
<span class="Big_Orange_Times"><?php print "$success_msg"; ?></span></div></td>
</tr>
</table>
<br>
<p>
<?php include_once "footer_template.php"; ?>
</p>
<p> </p>
</body>
</html>