I'm having a ridiculous amount of trouble making this script work. Primarily I think because I don't really understand the php image functions, and am fairly inexperienced in PHP in general.
Basically, I want to take a user-uploaded image and resize it proportionally, then copy it to the server. I can upload no problem, but when I try to do all the createtruecolor copyresampled functions, I'm way over my head.
(there are some references in there to thumbnail images that don't yet do anything)
Here's the code so far:
PHP Code:
// UPLOAD IMAGE TO SERVER
$gallery = $_POST['gallery'];
$name = $_POST['name'];
$caption = $_POST['caption'];
$directory_full = '../../gallery/images/';
$directory_thumb = '../../gallery/images/';
$postfixFull = '_full';
$postfixThumb = '_thumb';
$ext = '.jpg';
$srcImage = $_FILES['image'];
$maxWidthFull = 500;
$maxWidthThumb = 100;
if(!empty($_FILES['image'])){
if($_FILES['image']['type']!="image/jpeg"){
die('Image must be a JPEG');
}
// rename image, create sub names
$imageName = str_replace($ext,"",str_replace(" ","",$_FILES['image']['name']));
$imageNameFull = $imageName . $postfixFull . $ext;
$imageNameThumb = $imageName . $postfixThumb . $ext;
// RESIZE IMAGE BY MAX WIDTH
$sizes = getimagesize($_FILES['image']['tmp_name']);
$aspect_ratio = $sizes[0] / $sizes[1]; // width / height
if($sizes[0] <= $maxWidthFull){
$newWidthFull = $sizes[0];
$newHeightFull = $sizes[1];
}
else{
$newWidthFull = $maxWidthFull;
$newHeightFull = abs($newWidthFull/$aspect_ratio);
}
// create full size image dimension block
$destImageFull = imagecreatetruecolor($newWidthFull, $newHeightFull);
//
$imageFinalFull = imagecopyresampled($destImageFull, $srcImage, 0, 0, 0, 0, $newWidthFull, $newHeightFull, $sizes[0], $sizes[1]);
// COPY AND RESIZE THUMBNAIL TO MAX WIDTH
// copy resized image to server
move_uploaded_file($_FILES['image']['tmp_name'], $directory_full . $imageFinalFull)
or
die ('Could not upload');
// copy thumbnail to server
// NOW ADD INFO TO DATABASE
mysql_query("INSERT INTO gallery VALUES (
'',
'$name',
'$gallery',
'$caption',
'$imageNameThumb',
'$imageNameFull'
)");
print('<br>Image uploaded');
}
else{
print 'file empty';
}
I've been reading the PHP manual on this stuff and I really just don't understand it. Hoping someone can explain what's going on here. Really appreciate it.
|