I have a problem with a thumbnail creation script. First it uploads an image and creates a thumbnail. The imgae is uploaded fine along with the thumbnail, except for the fact that the thumbnail is black. The image uploaded is a jpeg and I am running GD version 2.0. I hope someone can help me with this.
Here is the Script
<?php
$site_name = $SERVER['HTTP_HOST'];
$url_dir = "http://".$SERVER['HTTP_HOST'].dirname($SERVER['PHP_SELF']);
$url_this = "http://".$SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$upload_dir = "pics/";
$upload_url = $url_dir."/pics/";
$message ="";
//create upload_files directory if not exist
if (!is_dir("pics")) {
die ("picture directory doesn't exist");
}
if ($_FILES['file']) {
$message = do_upload($upload_dir, $upload_url);
}
else {
$message = "";
}
print $message;
function do_upload($upload_dir, $upload_url) {
$temp_name = $_FILES['file']['tmp_name'];
$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$file_size = $_FILES['file']['size'];
$result = $_FILES['file']['error'];
$file_url = $upload_url.$file_name;
$file_path = $upload_dir.$file_name;
//File Name Check
if ( $file_name =="") {
$message = "Invalid File Name Specified";
return $message;
}
//File Size Check
else if ( $file_size > 500000) {
$message = "The file size is over 500K.";
return $message;
}
//File Type Check
else if ( $file_type == "text/plain" ) {
$message = "Sorry, You cannot upload any script file" ;
return $message;
}
$nw=200; //The Width Of The Thumbnails
$nh=125; //The Height Of The Thumbnails
$ipath = "pics"; //Path To Place Where Images Are Uploaded. No Trailing Slash
$tpath = "pics/thumbs";//Path To Place Where Thumbnails Are Uploaded. No Trailing Slash
$dimensions = GetImageSize($file);
$thname = "$tpath/$file_name";
$w=$dimensions[0];
$h=$dimensions[1];
$img2 = ImageCreateFromJPEG($file);
$thumb=ImageCreateTrueColor($nw,$nh);
ImageCopyResized($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h);
ImageJPEG($thumb,$thname,95);
$result = move_uploaded_file($temp_name, $file_path);
$message = ($result)?"Thank You." :
"Somthing is wrong with uploading a file.";
return $message;
}
?>