Hi everyone. I have a pretty unique system set in place and I need a hand getting this image resize script to work. I'll explain what I have in place now, and then post my code examples. I will warn you though, there is a lot of explanation and code below.
I currently have a system set up, where a user can enter a unique number, a description of the image, and the image itself. The number, description, and name (only the name, a temporary one) are sent to a database, the actual file gets renamed and put into a folder on the server. The file gets renamed from whatever the unique number was that the person enters in the appropriate text box.
Right now, I have in the display script an image tag that calls the unique number to display the image, since the image is renamed to that number. I would like to get a resize image script to work so each image doesn't have to be the exact same size, but i'm having little success in doing so.
Okay, here a couple of examples of my code...
The input form
Insert number<br>
<input name="prodNum" type="text" id="prodNum2">
<br>
Insert description<br>
<input type="text" name="itemText" id="itemText2">
<br>
Image:
<input type="file" name="userfile">
<br>
<input type="submit" value="submit">
Upload Script
//image upload
// $userfile is where the file went on the webserver
$userfile = $_FILES['userfile']['tmp_name'];
// $userfile_name is the original file name
$userfile_name = $_FILES['userfile']['name'];
// new file rename here
$userfile_name = $prodNum.strrchr($userfile_name, '.');
// $userfile_size is size in bytes
$userfile_size = $_FILES['userfile']['size'];
// $userfile_type is mime type
$userfile_type = $_FILES['userfile']['type'];
// $userfile_error is any error encountered
$userfile_error = $_FILES['userfile']['error'];
// check for errors
if($userfile_error > 0)
{
echo 'Problem';
switch ($userfile_error)
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}
// put the file where we'd like it
$upfile = '../files/images/'.$userfile_name;
// check if the file is uploaded and then move it to the correct directory
if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo 'Problem: could not move the file to the correct directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: '.$userfile_name;
exit;
}
require('../product_added.php');
//reformat the file contents
$fp = fopen($upfile, 'r');
$contents = fread($fp, filesize ($upfile));
fclose ($fp);
$fp = fopen($upfile, 'w');
fwrite($fp, $contents);
fclose($fp);
//end image upload
The insert statment:
mysql_select_db('');
$query = "insert into table (prod_num, description, image) values
('".$prodNum."', '".$itemText."', '".$userfile."')";
$result = mysql_query($query);
And here is the script that currently displays the image and description. Note that $name and $desc are renamed variables, part of a for loop that loops through the tables of the database.
for($i=0; $i < $num_results; $i++){
$name=mysql_result($result, $i, "prod_num"); //name of the picture
$desc=mysql_result($result, $i, "description"); //the name of description
<td><img src="files/images/'. $name .'.JPG" width="100" height="75"/></td><td>. $desc .</td>
And finally, the image resize script I have. I'm just not sure how to implement it.
<?php
$image = $HTTP_GET_VARS['userfile_name'];
if (!$max_width)
$max_width = 100;
if (!$max_height)
$max_height = 100;
$size = getImageSize($image);
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if ( ($width <= $max_width) && ($height <= $max_height) ) {
$tn_width = $width;
$tn_height = $height;
}
else if (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * height);
$tn_width = $max_width;
}
else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$src = ImageCreateFromJpeg($image);
$dst = ImageCreate($tn_width,$tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height);
header('Content-type: image/jpeg');
ImageJpeg($dst, null, -1);
ImageDestroy($src);
ImageDestroy($dst);
?>
I would REALLY appreciate any help with this. I've been working on it for a while, with no success.
Thank You!!
Brian