Hi,
I was wanting a bit of help please, I have the scripts below that will handle photos at 500kb and around 2800 pixs in size. The problem comes when people - even though I tell them not to - try to upload larger size photos, so could anyone suggest how to reduce the larger size photo down to first and then pass it on to the script below. Thanks as always,
Phil
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 600000);
require_once ('../mysql_connect.php'); // Connect to the database.
include('./header.inc');
// Function for escaping and trimming form data.
function escape_data ($data) {
global $dbc;
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string (trim ($data), $dbc);
} // End of escape_data() function.
// First name
if (!empty($_POST['first_name'])) {
$fn = escape_data($_POST['first_name']);
} else {
$fn = '';
}
// Check for a description (not required).
if (!empty($_POST['description'])) {
$d = escape_data($_POST['description']);
} else {
$d = '';
}
if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', '/home/myweb/public_html/family_pics/');
// replace any spaces in original filename with underscores
// at the same time, assign to a simpler variable
$file = str_replace(' ', '_', $_FILES['image']['name']);
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/4000, 1).'KB';
// create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
$file = "../family_pics/images/".$_FILES['image']['name'];
// Add the user.
$query = "INSERT INTO family (first_name, picture_url, description, registration_date) VALUES ('$fn','$file','$d', NOW() )";
$result = @mysql_query ($query); // Run the query.
// check that file is of an permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type']) {
$typeOK = true;
break;
}
}
mysql_close(); // Close the database connection.
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error']) {
case 0:
include('./create_thumb.inc.php');
break;
case 3:
$result = "Error uploading $file. Please try again.";
default:
$result = "System error uploading $file. Contact webmaster.";
}
}
elseif ($_FILES['image']['error'] == 4) {
$result = 'No file selected';
}
else {
$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
}
}
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File upload</title>
<style type="text/css">
<!--
body {
background-color: #FFFFCC;
}
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
-->
</style></head>
<body>
<?php
// if the form has been submitted, display result
if (isset($result)) {
echo "<p><strong>$result</strong></p>";
}
?>
This below is the included file from above.
// define constants
define('THUMBS_DIR', '/home/myweb/public_html/family_pics/images/');
define('MAX_WIDTH', 235);
define('MAX_HEIGHT',235);
// process the uploaded image
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
$original = $_FILES['image']['tmp_name'];
// begin by getting the details of the original
list($width, $height, $type) = getimagesize($original);
// calculate the scaling ratio
if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
$ratio = 1;
}
elseif ($width > $height) {
$ratio = MAX_WIDTH/$width;
}
else {
$ratio = MAX_HEIGHT/$height;
}
// strip the extension off the image filename
$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
$name = preg_replace($imagetypes, '', basename($_FILES['image']['name']));
// create an image resource for the original
switch($type) {
case 1:
$source = @ imagecreatefromgif($original);
if (!$source) {
$result = 'Cannot process GIF files. Please use JPEG or PNG.';
}
break;
case 2:
$source = imagecreatefromjpeg($original);
break;
case 3:
$source = imagecreatefrompng($original);
break;
default:
$source = NULL;
$result = 'Cannot identify file type.';
}
// make sure the image resource is OK
if (!$source) {
$result = 'Problem copying original';
}
else {
// calculate the dimensions of the thumbnail
$thumb_width = round($width * $ratio);
$thumb_height = round($height * $ratio);
// create an image resource for the thumbnail
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// create the resized copy
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
// save the resized copy
switch($type) {
case 1:
if (function_exists('imagegif')) {
$success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif');
$thumb_name = $name.'_thb.gif';
}
else {
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50);
$thumb_name = $name.'_thb.jpg';
}
break;
case 2:
$success = imagejpeg($thumb, THUMBS_DIR.$name.'', 100);
$thumb_name = $name.'';
break;
case 3:
$success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png');
$thumb_name = $name.'_thb.png';
}
if ($success) {
$result = "Your file $thumb_name was uploaded successfully,<p>
please view your pics in the gallery";
}
else {
$result = 'Problem creating thumbnail';
}
// remove the image resources from memory
imagedestroy($source);
imagedestroy($thumb);
}
}