I have a form that uploads several images at once, now I need make thumbnails. I tried the following method but it has somme strage errors. Sometimes it will say the image isn't valid when it's a .jpeg/.jpg etc.
At the moment the upload.php just uses the litteral image extension, $FILES_[meta] or whatever the correct code is didn't seem to work.
Another thing is the thumbnail function always returns true, how can I make it return false if there is an error? I know very very little about php and images.
My host is running php 5 with gd 2 enabled if this makes any difference.
Upload.php
<?php
require_once ('../../uni_connect.php');
include ('../includes/access.php');
$eid = $_GET['eid'];
if($eid == ''){
$url = '/index.php';
@header("Location: $url");
}
$query = "SELECT COUNT(*) FROM event_guests WHERE uid_guest='$id' AND eid='$eid'";
$result = @mysql_query($query);
$row = @mysql_fetch_array($result, MYSQL_NUM);
$num = $row[0];
if ($num == 0) {
$page_title = 'Ooops';
include ('../includes/header.php');
echo '<h1>Ooops</h1><p>Seems there\'s a little problem with this event or your not on the guestlist. If you think you should be able to access this event and the problem persists please report it.</p>';
$menu = 'event';
include('../includes/submenu.php');
include('../includes/footer.php');
exit();
}
$page_title = 'Upload Pictures To Event';
include ('../includes/header.php');
if (isset($_POST['submitted'])) {
if ($_POST['terms'] == 'agree' && $_POST['privacy'] == 'agree') {
include ('../includes/thumbnail.php');
$dates = getdate();
$folder = $dates['year'].$dates['mon'].$dates['mday'];
foreach ($_FILES['pictures']['error'] as $key => $error) {
if(isset($_FILES['pictures']['name'][$key])){
if ($error == UPLOAD_ERR_OK){
$extension = explode ('.', $_FILES['pictures']['name'][$key]);
$ext = $extension[1];
$random_number = round(mt_rand(1, 1000000));
$new_filename = $folder.$random_number.'.'.$ext;
if(move_uploaded_file($_FILES['pictures']['tmp_name'][$key], $_SERVER['DOCUMENT_ROOT'].'/uploads/events/pictures/'.$new_filename)){
if($ext=='jpg' || $ext=='jpeg' || $ext=='pjpeg'){
thumbjpg($_SERVER['DOCUMENT_ROOT'].'/uploads/events/pictures/', $_SERVER['DOCUMENT_ROOT'].'/uploads/events/thumbs/', $new_filename, '150');
$picture = $new_filename;
$query = "INSERT INTO event_photos (eid, location, uid) VALUES ('$eid', '$picture', '$id')";
$result = @mysql_query ($query);
echo '<p>Your picture has been uploaded! Enjoy!</p>';
}elseif($ext=='gif'){
thumbgif($_SERVER['DOCUMENT_ROOT'].'/uploads/events/pictures/', $_SERVER['DOCUMENT_ROOT'].'/uploads/events/thumbs/', $new_filename, '150');
$picture = $new_filename;
$query = "INSERT INTO event_photos (eid, location, uid) VALUES ('$eid', '$picture', '$id')";
$result = @mysql_query ($query);
echo '<p>Your picture has been uploaded! Enjoy!</p>';
}else{
echo '<p>Wrong filetype, please submit a jp(e)g or gif</p>';
}
}
}//end error = ok
}//end of filename check
}//end foreach
}else{
echo '<p>You need to agree to the Terms and Conditions and Privacy Agreement.</p>';
}//end terms and conditions
}//end submit
?>
<h1>Upload Pictures To Event</h1>
<form action="/account/upload_event.php?eid=<?=$eid;?>" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Upload Pictures</legend>
<input type="file" name="pictures[]" />
<br/>
<input type="file" name="pictures[]" />
<br/>
<input type="file" name="pictures[]" />
<br/>
<input type="file" name="pictures[]" />
<br/>
<input type="file" name="pictures[]" />
<br/>
<label for="terms">Agree To <a href="legal/terms.php" target="_blank">T&C</a></label>
<input name="terms" type="checkbox" value="agree" checked/>
<br/>
<label for="privacy">Read Privacy?*</label>
<input name="privacy" type="checkbox" value="agree" checked/>
<br/><br/>
<div align="center">
<input type="submit" name="submit" value="Upload!" />
<input name="reset" type="reset" value="Clear" />
<input type="hidden" name="submitted" value="TRUE" />
</div>
</fieldset>
</form>
<?
$menu = 'event';
include('../includes/submenu.php');
include('../includes/footer.php');
exit();
?>
Thumbnail.php
<?php
function thumbjpg($image_path, $thumb_path, $image_name, $thumb_width)
{
$src_img = imagecreatefromjpeg("$image_path/$image_name");
$origw=imagesx($src_img);
$origh=imagesy($src_img);
$new_w = $thumb_width;
$diff=$origw/$new_w;
$new_h=$origh/$diff;
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
imagejpeg($dst_img, "$thumb_path/$image_name");
return true;
}
function thumbgif($image_path, $thumb_path, $image_name, $thumb_width)
{
$src_img = imagecreatefromgif("$image_path/$image_name");
$origw=imagesx($src_img);
$origh=imagesy($src_img);
$new_w = $thumb_width;
$diff=$origw/$new_w;
$new_h=$origh/$diff;
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
imagejpeg($dst_img, "$thumb_path/$image_name");
return true;
}
?>
Thanks for the help