NEED HELP PLEASE!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tecknot
Forum Newbie
Posts: 1
Joined: Fri May 14, 2004 2:44 am

NEED HELP PLEASE!!

Post by tecknot »

I have an image upload script with a thumbnail generator that will upload the picture fine but only displays a black thumbnail.

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;
}

?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Well try copying the image to your own dir before you create the thumbnail as you may not have write access in the tmp dir.
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

I'd be quite surprised, as a temp directory is supposed to be world-writeable, isn't it? Otherwise, where do apps which can't write to it store their garbage?
Post Reply