Is it ok to use the temp dir. that the file is uploaded to, to get the image for ImageCreateFromJPEG or does it have to be saved on the server.
Here is my code for the upload and thumbnail part of the script. Just ignore the upload part of it.
I hope someone has some advice to get this working properly.
Code: Select all
<?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;
}
//NOW CREATE THUMBNAIL
$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($_FILES['file']['size']);
$thname = "$tpath/$file_name";
$w=$dimensions[0];
$h=$dimensions[1];
$img2 = ImageCreateFromJPEG($_FILES['file']['tmp_name']);
$thumb=ImageCreateTrueColor($nw,$nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w > $h){
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
ImageJPEG($thumb,$thname,95);
}elseif(($w < $h) || ($w == $h)){
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
ImageJPEG($thumb,$thname,95);
}else{
ImageCopyResampled($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;
}
?>