Hi all,
Hoping you can help me out a little. I have a script that after a lot of trial and error and a little assistance along the way uploads an image file to a given directory and holds the location in a mysql database, along with some basic text information.
What I need to do is change the size of the image that is uploaded. Ideally with a thumbnail which location is also held in the database so all the info is together.
Although the thumbnail creation isnt really important, it is the changing of the image size that is.
I need the image to be resized to a maximum of 800x600. Obviously if it is already smaller than this then it doesnt want to be increased in size.
I have found a few tuorials which handle the uploading of a file to a directory and manipulating the size, I cant work out how to include it into my script.
I would be grateful if someone could take a look at my script and point me in the right direction.
The script is:
<?php
$uploadDir = 'pictures/';
$fileUploaded = 0; // set a flag for use later
// connection parameters!
include('/home/sites/xxxxxxxxxxx/db_config/xxxxxxxxx.php');
$database = mysql_connect($hostname,$username,$password) or die("Could not connect to server");
mysql_select_db($table,$database) or die("Could not find table");
////////////////////////////////////
if(isset($_POST['upload']))
{
$id = $row['id'];
$title = addslashes($_POST['title']);
$detail = addslashes($_POST['detail']);
$picinfo = addslashes($_POST['picinfo']);
// check if a file has been uploaded
if($_FILES['userfile']['error'] != 4) { // error code 4 meaning that no image is present
$fileUploaded = 1;
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
// the files will be saved in filePath
$filePath = $uploadDir . $fileName;
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
} else {
// if no file added, generate empty variables so as not to break the query
$fileName = '';
$fileSize = '';
$fileType = '';
$filePath = '';
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO tablename (name, size, type, path, title, detail, picinfo) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$title', '$detail', '$picinfo')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
mysql_close($database);
// if a file was uploaded, the fileUploaded variable will be set to 1
if($fileUploaded == 1) {
echo "<br>File uploaded<br>";
// show the image after upload - comment this out if not required - good for checking though This part isnt working properly:)
echo '<img src="'. $uploadDir .'/'.$_FILES['userfile']['name'][$i] .'" >';
$movedtolocation="pictures/".$_FILES['upload']['name'];# basically, take the location of wher the image is stored..
}
}
?>
Form is:
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<table width="609" align="center" cellpadding="1" cellspacing="1">
<tr>
<td colspan="4" align="center"> </td>
</tr>
<tr>
<td width="165"><p class="subtitle">Main Title :</p></td>
<td width="437" colspan="3"><p> <input name="title" type="text" size="35"> </td>
</tr>
<tr>
<td width="165" valign="top"> </td>
<td colspan="3"> </td>
</tr>
<tr>
<td width="165" valign="top"><p class="subtitle"> Blog :</p>
<p align="center" class="small"> </p></td>
<td colspan="3"><textarea name="detail" id="detail" cols="50" rows="20"></textarea></td>
</tr>
<tr>
<td width="165"><p class="subtitle">Select Picture:</p></td>
<td colspan="3"><p align="center">
<input name="userfile" type="file" class="box" id="userfile" size="35">
<span class="small"> <br>
Leave blank if no picture to upload</span><br>
<br>
</p></td>
<tr>
<td width="165">Picture Info</td>
<td colspan="3"><p align="left"><input name="picinfo" type="text" size="35"></p></td>
</tr>
<tr><td colspan="4" align="center"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
Thanks in advance