Jeg har lavet følgende rettelser:
<?php
// if an image is uploaded
if (isset ($_POST['submit']))
{
// the thumbnail height
$size = 150;
// the directory where the original uploaded image is saved
$filedir = 'pics/';
// the directory where the thumbnail image is saved
$thumbdir = 'pics/';
// the prefix to be added to the original name to name the thumbnail
$prefix = 'small_';
// the file settings for the uploaded image
$mode = '0666';
$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];
$userfile_description = isset($_POST['description']) ? $_POST['description'] : "";
if (isset ($userfile_name))
{
$prod_img = $filedir . $userfile_name;
$prod_img_thumb = $thumbdir . $prefix . $userfile_name;
move_uploaded_file($userfile_tmp, $prod_img);
chmod($prod_img, octdec($mode));
$sizes = getimagesize($prod_img);
$aspect_ratio = $sizes[1] / $sizes[0];
if ($sizes[1] <= $size)
{
$new_w = $sizes[0];
$new_h = $sizes[1];
}
else
{
$new_h = $size;
$new_w = abs($new_h / $aspect_ratio);
}
$dest = imagecreatetruecolor($new_w, $new_h) or die('Problem In Creating image');
$src = imagecreatefromjpeg($prod_img) or die('Problem In opening Source Image');
if (function_exists('imagecopyresampled'))
{
imagecopyresampled($dest, $src, 0, 0, 0, 0, $new_w, $new_h, imagesx($src), imagesy($src)) or die('Problem In resizing');
}
else
{
Imagecopyresized($dest, $src, 0, 0, 0, 0, $new_w, $new_h, imagesx($src), imagesy($src)) or die('Problem In resizing');
}
imagejpeg($dest, $prod_img_thumb, 90) or die('Problem In saving');
imagedestroy($dest);
// Connect to the database:
mysql_connect('localhost', 'db_username', 'db_pass')
or die('Problems connection to database server');
mysql_select_db('db_name')
or die('Couldnt use selected Database. ' . mysql_error());
mysql_query("INSERT INTO filenames(filename, description) VALUES('$userfile_name', '$userfile_description')")
or die('Error in SQL: ' . mysql_error());
mysql_close();
}
echo '
<a href="' . $prod_img . '">
<img src="' . $prod_img_thumb . '" width="' . $new_w . '" heigt="' . $new_h . '" alt="" />
</a>';
// show an upload form to upload and resize an image
}
else
{
echo '
<form method="post" action="' . $_SERVER['PHP_SELF'] . '" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="text" name="description" />
<input type="submit" name="submit" value="upload and resize image" />
</form>';
}
?>
Du skal lige ændre username og password så de passer med din database.. Derudover forudsætter koden at du har et table i din database:
table navn: filenames
felter: id, filename, description