我想要一个脚本,当从充满较大图像的文件夹中自动生成缩略图时。我在实现它时遇到了一些问题。
我早期遇到了一些错误,因为我没有包含我需要的所有 phpthumb 文件,但现在错误来自 phpthumb 代码本身。将根据需要发布更多代码。
错误:
Warning: getimagesize(..) [function.getimagesize]: failed to open stream: Permission denied in C:xampphtdocsmysiteGdThumb.inc.php on line 1070
Fatal error: Uncaught exception 'Exception' with message 'File is not a valid image: ..' in C:xampphtdocsmysiteThumbBase.inc.php:193 Stack trace: #0 C:xampphtdocsmysiteGdThumb.inc.php(1081): ThumbBase->triggerError('File is not a v...') #1 C:xampphtdocsmysiteGdThumb.inc.php(98): GdThumb->determineFormat()
#2 C:xampphtdocsmysiteThumbLib.inc.php(127): GdThumb->__construct('..', Array, false) #3 C:xampphtdocsmysitesave_differentformat.php(47): PhpThumbFactory::create('..') #4 {main} thrown in C:xampphtdocsmysiteThumbBase.inc.php on line 193
脚本:
require_once '../mysite/ThumbLib.inc.php';
$dir = "../mysite/images" ;
$destination = "../mysite/thumbnails" ;
$images = scandir($dir);
foreach ($images as $image)
{
$ext = pathinfo($dir . DIRECTORY_SEPARATOR . $image,PATHINFO_EXTENSION);
$thumb = PhpThumbFactory::create($image . DIRECTORY_SEPARATOR. $image);
$thumb->adaptiveResize(100, 100);
$thumb->save($destination . DIRECTORY_SEPARATOR. $image, $ext);
$thumb->show();
}
?>
编辑
这是我现在收到的错误消息。它不知道$mode是什么,我可能在该文件夹中混合了图像类型。
Notice: Undefined variable: mode in C:xampphtdocsmysitesave_differentformat.php on line 51
Warning: getimagesize(..) [function.getimagesize]: failed to open stream: Permission denied in C:xampphtdocsmysiteGdThumb.inc.php on line 1070
Fatal error: Uncaught exception 'Exception' with message 'File is not a valid image: ..' in C:xampphtdocsmysiteThumbBase.inc.php:193 Stack trace: #0 C:xampphtdocsmysiteGdThumb.inc.php(1081): ThumbBase->triggerError('File is not a v...') #1 C:xampphtdocsmysiteGdThumb.inc.php(98): GdThumb->determineFormat()
#2 C:xampphtdocsmysiteThumbLib.inc.php(127): GdThumb->__construct('..', Array, false) #3 C:xampphtdocsmysitesave_differentformat.php(64): PhpThumbFactory::create('..') #4 {main} thrown in C:xampphtdocsmysiteThumbBase.inc.php on line 193
以下是 scandir(( 返回的内容:
array(19) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(36) "13912d3e8b4ae1890457fe935cc0c7fe.png" [3]=> string(36) "266c484ca488d053255c7767af212bee.png" [4]=> string(36) "8af941c37874dc3a7edee2772b60c323.png" [5]=> string(9) "Thumbs.db" [6]=> string(15) "butterflies.png" [7]=> string(22) "butterfliesinverse.png" [8]=> string(11) "favicon.png" [9]=> string(36) "fb9e25f57d12ec5019aa548665f34fa5.png" [10]=> string(20) "image1.png" [11]=> string(25) "image11 (1).png" [12]=> string(21) "image11.png" [13]=> string(25) "image12 (1).png" [14]=> string(21) "image12.png" [15]=> string(21) "image15.png" [16]=> string(20) "image2.png" [17]=> string(21) "image25.png" [18]=> string(21) "image28.png" }
OK 接受道歉
一个。在处理路径时,它并不是真的建议能够使用../
..这可能会导致很多问题,我的建议是使用真实路径
B.始终检查文件夹是否存在,并且可以在执行文件操作之前写入该文件夹
例
$dir = "mysite/images";
$destination = "mysite/thumbnails";
$mode = 0777;
if (! is_dir ( $dir )) {
die ( "Source Direcotry Does not exist : $dir" );
}
if (! is_readable ( $dir )) {
die ( "Source Direcotry Does not readable : $dir" );
}
if (! mkdir_recursive ( $destination, $mode )) {
die ( "Can't wite create $destination" );
}
$images = scandir ( $dir );
$allowedExtention = array (
"jpg",
"gif",
"png"
);
$errors = array ();
foreach ( $images as $image ) {
$imageFile = $dir . DIRECTORY_SEPARATOR . $image;
if (is_file ( $dir . DIRECTORY_SEPARATOR . $image )) {
if ($image == "." || $image == "..") {
continue;
}
$ext = pathinfo ( $dir . DIRECTORY_SEPARATOR . $image, PATHINFO_EXTENSION );
if (! in_array ( $ext, $allowedExtention )) {
continue;
} else {
$errors [] = $imageFile . " has invalid extention $ext";
}
try {
$thumb = PhpThumbFactory::create ( $dir . DIRECTORY_SEPARATOR . $image );
$thumb->adaptiveResize ( 100, 100 );
$thumb->save ( $destination . DIRECTORY_SEPARATOR . $image, $ext );
$thumb->show ();
} catch ( Exception $e ) {
$errors [] = "PhpThumbFactory : " . $e->getMessage ();
}
} else {
$errors [] = $imageFile . " not a valid File";
}
}
// Check for errors
if (count ( $errors )) {
foreach ( $errors as $error ) {
echo $error . " <br />";
}
}
function mkdir_recursive($pathname, $mode) {
is_dir ( dirname ( $pathname ) ) || mkdir_recursive ( dirname ( $pathname ), $mode );
return is_dir ( $pathname ) || mkdir ( $pathname, $mode );
}