2010-09-01 48 views
0

当我提出这个功能:PHP:制作缩略图,为什么我得到这些错误?

function makeThumbnail($type, $name, $size, $tmp_name, $thumbSize) { 

//make sure this directory is writable! 

$path_thumbs = "uploaded_files/";  

//the new width of the resized image, in pixels. 

$img_thumb_width = $thumbSize; // 

$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed) 

//List of allowed extensions if extlimit = yes 

$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");  

//the image -> variables 
$file_type = $type; 

$file_name = $name; 

$file_size = $size; 

$file_tmp = $tmp_name; 
//check if you have selected a file. 
echo $file_tmp."<br>"; 
echo $file_name."<br>"; 
echo $file_type."<br>"; 
echo $file_size."<br>"; 

if(!is_uploaded_file($file_tmp)){ 

echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>"; 

exit(); //exit the script and don't process the rest of it! 

} 

//check the file's extension 

$ext = strrchr($file_name,'.'); 

$ext = strtolower($ext); 

//uh-oh! the file extension is not allowed! 

if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { 

echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>"; 

exit(); 

} 

//so, whats the file's extension? 

$getExt = explode ('.', $file_name); 

$file_ext = $getExt[count($getExt)-1]; 

//create a random file name 

$rand_name = md5(time()); 

$rand_name= rand(0,999999999); 

//the new width variable 

$ThumbWidth = $img_thumb_width; 



///////////////////////////////// 

// CREATE THE THUMBNAIL // 

//////////////////////////////// 



//keep image type 

if($file_size){ 

if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ 

$new_img = imagecreatefromjpeg($file_tmp); 

}elseif($file_type == "image/x-png" || $file_type == "image/png"){ 

$new_img = imagecreatefrompng($file_tmp); 

}elseif($file_type == "image/gif"){ 

$new_img = imagecreatefromgif($file_tmp); 

} 

//list the width and height and keep the height ratio. 

list($width, $height) = getimagesize($file_tmp); 

//calculate the image ratio 

$imgratio=$width/$height; 

if ($imgratio>1){ 

$newwidth = $ThumbWidth; 

$newheight = $ThumbWidth/$imgratio; 

}else{ 

$newheight = $ThumbWidth; 

$newwidth = $ThumbWidth*$imgratio; 

} 

//function for resize image. 

if (function_exists(imagecreatetruecolor)){ 

$resized_img = imagecreatetruecolor($newwidth,$newheight); 

}else{ 

die("Error: Please make sure you have GD library ver 2+"); 

} 

//the resizing is going on here! 

imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

//finally, save the image 

ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext", 100); 

ImageDestroy ($resized_img); 

ImageDestroy ($new_img); 





} 



//ok copy the finished file to the thumbnail directory 

// move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext"); 

/* 

Don't want to copy it to a separate directory? 

Want to just display the image to the user? 

Follow the following steps: 



2. Uncomment this code: 

/* 

/* UNCOMMENT THIS IF YOU WANT */ 

echo "OK THUMB " . $thumbSize; 

exit(); 

//*/ 



//and you should be set! 



//success message, redirect to main page.  

$msg = urlencode("$title was uploaded! <a href=\"Resize.php\">Upload More?</a>"); 
} 

然后停止工作,而是一个功能外,它的工作好。

正如你所看到的,我添加了“echo $ file ....”,因为我想看看它们是否有价值,并且它们确实具有正确的值。

我刚刚得到错误错误:请选择一个文件上传。

该功能在正常上传图像脚本(全尺寸)后运行。

当我调用该函数我这样做:

makeThumbnail($_FILES[$fieldname]['type'], $_FILES[$fieldname]['name'], $_FILES[$fieldname]['size'], $_FILES[$fieldname]['tmp_name'], 100); 

在我的其他文件,其中它不是在一个函数,世界上没有区别仅在于变量是:

$file_type = $_FILES['file']['type']; 

$file_name = $_FILES['file']['name']; 

$file_size = $_FILES['file']['size']; 

$file_tmp = $_FILES['file']['tmp_name']; 

但它应该工作,我找不到任何错误,但它并没有,我不断收到错误。如果我删除了is_uploaded_file函数,我收到了一堆其他错误。

+0

请先调试一下。什么是'print_r($ _ FILES)'包含的内容? '$ tmp_name'指向一个现有的文件吗? – 2010-09-01 12:46:50

+0

嗨,用'is_uploaded_file'函数传递的文件名即使在Windows上也是区分大小写的!在Windows上,PHP倾向于使用像%TEMP%这样的环境变量来确定上传文件夹,并且这些环境变量有时与实际路径具有不同的大小写。 – klennepette 2010-09-01 12:48:34

回答

2

请确保您在调用函数之前不使用move_uploaded_file()

0

我使用timthumb将图像输出到缩略图时将其输出到屏幕上,而不是当它上传时。

这意味着你只有一个文件,而不是一个主尺寸和一个拇指尺寸。 TimThumb减小了服务器上文件的大小,因此它在浏览器上看起来不错并且流畅。看看它:TimThumb Link

+0

他没有问你用什么......他问为什么他的代码不起作用。 – Timothy 2010-09-01 12:47:38

+0

我实际上给了他一个替代方法来做一个插件同样的事情。这不是我知道的解决方案,但它是一个有用的替代方案 – 2010-09-01 12:49:29

+0

PS:我还检查了您的TimThumb链接。 777对于临时目录的权限不是很好,您可能需要考虑将它移植到想象中以获得更好的内存使用。 – Timothy 2010-09-01 12:50:10