PHP Classes

Image resize

Recommend this page to a friend!

      Top level forums  >  PHP Specialists  >  General  >  Image resize  
Subject:Image resize
Summary:how to resize image
Messages:6
Author:Graham Peach
Date:2009-09-19 19:28:32
Update:2009-09-22 21:54:17
 

  1. Image resize   Reply   Report abuse  
Picture of Graham Peach Graham Peach - 2009-09-19 20:50:14
Hi all,

I am having a exremely frustrating time.

I have been trying to do this one for myself and have come to the conclusion that I am thick. I just cannot get my head around this at all. I cannot fathom how it works.

Basic background - I have a MySql database, in it I want to some text information and some image information including the path the folder where the image resides.

I have a script already which is working without problems. I have even got it to change filenames to lowercase, and it allows files over 2mb to be uploaded.

The problem I am having is finding a php script or class to resize the image to 800x600 keeping the aspect ratio, There are lots of classes on this site but i ust dont know how to integrate them with my script. Any assistance would be greatly appreciated.

Many thanks in advance

Graham

My script is as follows:

<?php

$uploadDir = 'pictures/';
$fileUploaded = 0; // set a flag for use later

// connection parameters !
include('/home/sites/path-to-server.com/db_config/blog_config.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']);
//$precis = addslashes($_POST['precis']);
//$year = addslashes($_POST['year']);

// 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'];
$fileName = strtolower($fileName);

// 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) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$title', '$detail')";

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 :)
echo '<img src="'. $uploadDir .'/'.$_FILES['userfile']['name'][$i] .'" >';
$movedtolocation="pictures/".$_FILES['upload']['name'];# basically, take the location of wher the image is stored..
}

}
?>
<link href="styleadmin.css" rel="stylesheet" type="text/css">


<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">&nbsp;</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">&nbsp;</td>
</tr>
<tr>
<td width="165" valign="top">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td width="165" valign="top"><p class="subtitle"> Blog :</p>
<p align="center" class="small">&nbsp;</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 type="hidden" name="MAX_FILE_SIZE" value="11000000">
<input name="userfile" type="file" class="box" id="userfile" size="35">
<span class="small">&nbsp;&nbsp;&nbsp;&nbsp;<br>
Leave blank if no picture to upload</span><br>
<br>
</p></td>
<tr>
<td width="165">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr><td colspan="4" align="center"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>


There are 5 replies in this thread, which are not being displayed.
Browsing this forum thread replies is available only to premium subscribers.


Go to the premium subscriptions page to learn how to become a premium subscriber and have full access to this forum.