hey peeps
ive made an upload function and then a function to create a thumbnail from the uploaded file.
the file uploads correctly but the thumbnail doesn't seem to get created.
here is the upload function:
function uploadFile ($oldFile, $userFile, $imgDir) {
//extensions array
$validExt = array ('.jpg', '.jpeg', '.png', '.gif');
//get the file ext
$ext = strrchr($userFile, ".");
$ext = strtolower($ext);
//check the file ext against the array
if(in_array($ext, $validExt)) {
//needle is in haystack
//process upload
//rename the file
$newName = md5(rand() * time()) . $ext;
//new file dest
$destFile = $imgDir . $newName;
//move the file to tmpDir
$result = move_uploaded_file($oldFile, $destFile);
if($result == false) {
//there was an error uploading
return false;
}else{
//file was uploaded
return $destFile;
}
}else{
//needle is not in haystack. exit
return false;
}
}
and heres the thumbnail creation function:
function createThumb($oldFile, $userFile, $imgDir, $picWidth) {
$result = uploadFile($oldFile, $userFile, $imgDir);
if ($result == false) {
//file could not be uploaded
return false;
}else{
//file was uploaded. create thumbnail
//pathinfo
$file = pathinfo($result);
//find the .ext
$ext = $file['extension'];
//strip .ext to lower
$ext = strtolower($ext);
//get image sizes
$sizes = getimagesize($result);
//ratio
$ratio = $sizes[0]/$sizes[1];
//create new height
$newHeight = round($picWidth/$aspect);
//create true colour image
$srcImg = imagecreatetruecolor($picWidth, $newHeight);
if($ext == 'jpg' || $ext == 'jpeg') {
$tmpImg = imagecreatefromjpeg($result);
}elseif($ext == 'png') {
$tmpImg = imagecreatefrompng($result);
}elseif($ext == 'gif') {
$tmpImg = imagecreatefromgif($result);
}
imagecopyresampled($srcImg, $tmpImg, 0, 0, 0, 0, $picWidth, $newHeight, $sizes[0], $sizes[1]);
//save image
if($ext == 'jpg' || $ext == 'jpeg') {
imagejpeg($srcImg, $result, 100);
}elseif($ext == 'png') {
imagepng($scrImg, $result, 100);
}elseif($ext == 'gif') {
imagegif($srcImg, $result, 100);
}
imagedestroy($srcImg);
imagedestroy($tmpImg);
}
}
and heres how i call the function:
$tmpFile = $_FILES['image']['tmp_name'];
$userFile = $_FILES['image']['name'];
$thumbWidth = 150;
$albumImg = '../images/album/';
$result = createThumb ($tmpFile, $userFile, $albumImg, $thumbWidth);
anyone have any idea why the script wont create the thumbnail?