/    Sign up×
Community /Pin to ProfileBookmark

Resize and save image via upload form

Ok i know i have seen hundreds of posts on resizing images but they all seem to be about manipulating an image on your server and displaying it at a different size

I want to use the upload image idea that is sticky at the top of the php forum and incorporate the following

When a user uploads the image it will first be checked for width and height and which ever is the shorter will be resized to 200px and the other dimention will be resized proportionally

so that no matter if the image is in portrait or landscape it will always be resized to the same size just the width and height will be opposite way round depending on which orientation the image is if you get what i mean

Then once the image has been set to its new size i want it saved to the uploaded folder on my site

I firstly need this for making sure all images fit a certain size while retaining their aspect ratio but secondly if someone uploads a huge 3000px by 4000px image i dont want to just display it at smaller dimentions and leave such a huge file on my server taking up unneeded disk space

Can anyone help with modifying the script, any suggestions

Below is the exact code im currently using havent finished it all fully yet as it also ads some info to a database regarding the user details and some info about the image location but i just need the image to be resized before saving it to the upload folder on the server

[CODE]<?php
$uploadsDirectory = $_SERVER[‘DOCUMENT_ROOT’].’/WWPL/members/property-photos/’;
$uploadSuccess = ‘http://’ . $_SERVER[‘HTTP_HOST’].’/members/submit-listing.php?sent=1′;
$uploadForm = ‘http://’ . $_SERVER[‘HTTP_HOST’].’/members/submit-listing.php’;
$fieldname = ‘photo’;

$errors = array(1 => ‘php.ini max file size exceeded’,
2 => ‘html form max file size exceeded’,
3 => ‘file upload was only partial’,
4 => ‘no file was attached’);

isset($_POST[‘submit’])
or error(‘the upload form is neaded’, $uploadForm);

($_FILES[$fieldname][‘error’] == 0)
or error($errors[$_FILES[$fieldname][‘error’]], $uploadForm);

@is_uploaded_file($_FILES[$fieldname][‘tmp_name’])
or error(‘not an HTTP upload’, $uploadForm);

$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.’-‘.$_FILES[$fieldname][‘name’]))
{
$now++;
}

@move_uploaded_file($_FILES[$fieldname][‘tmp_name’], $uploadFilename)
or error(‘receiving directory insuffiecient permission’, $uploadForm);

$propertyname = $_POST[‘propertyname’];
$address = $_POST[‘address’];
$city = $_POST[‘city’];
$state = $_POST[‘state’];
$zip = $_POST[‘zip’];
$country = $_POST[‘country’];
$email = $_POST[’email’];
$phone = $_POST[‘phone’];
$website = $_POST[‘website’];
$description = $_POST[‘description’];
$photo = $uploadFilename;
$userid = $check[“id”];
$date = date();
$icon = $_POST[‘icon’];

$qry = “INSERT into listings
(userid, propertyname, address, city, state, zip, country, lat, lng, icon, phone, email, website, description, photo, date)
VALUES
(“$userid”, “$propertyname”, “$address”, “$city”, “$state”, “$zip”, “$country”, “$lat”, “$lng”, “$icon”, “$phone”, “$email”, “$website”, “$description”, “$photo”, “$date”)”;
$result = mysql_query($qry);

header(‘Location: ‘ . $uploadSuccess);

function error($error, $location, $seconds = 5)
{
header(“Refresh: $seconds; URL=”$location””);
echo ‘<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”‘.”n”.
‘”http://www.w3.org/TR/html4/strict.dtd”>’.”nn”.
‘<html lang=”en”>’.”n”.
‘ <head>’.”n”.
‘ <meta http-equiv=”content-type” content=”text/html; charset=iso-8859-1″>’.”nn”.
‘ <link rel=”stylesheet” type=”text/css” href=”stylesheet.css”>’.”nn”.
‘ <title>Upload error</title>’.”nn”.
‘ </head>’.”nn”.
‘ <body>’.”nn”.
‘ <div id=”Upload”>’.”nn”.
‘ <h1>Upload failure</h1>’.”nn”.
‘ <p>An error has occured: ‘.”nn”.
‘ <span class=”red”>’ . $error . ‘…</span>’.”nn”.
‘ The upload form is reloading</p>’.”nn”.
‘ </div>’.”nn”.
‘</html>’;
exit;
}[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@bokehApr 28.2007 — [CODE]@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);[/CODE]
[/QUOTE]
Try removing that and replacing it with the following (not tested):[code=php]$source = @imagecreatefromstring(
@file_get_contents($_FILES[$fieldname]['tmp_name']))
or die('Not a valid image format.');
$x = imagesx($source);
$y = imagesy($source);
if($w && ($x < $y)) $w = round(($h / $y) * $x);
else $h = round(($w / $x) * $y);
$slate = @imagecreatetruecolor($w, $h)
or error('Invalid image dimmensions', $uploadForm);
imagecopyresampled($slate, $source, 0, 0, 0, 0, $w, $h, $x, $y);
@imagejpeg($slate, $uploadFilename, 85)
or error('receiving directory insufficient permission', $uploadForm);
imagedestroy($slate);
imagedestroy($source);[/code]
Copy linkTweet thisAlerts:
@BWWebDesignsauthorApr 29.2007 — thanks, i actually found a bit of code that seems to be working, similar to the code you posted above but it also added a thumbnail image aswell of which i decided would be a good idea so i went with that

Thanks anyway
×

Success!

Help @BWWebDesigns spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...