我是php的新手,我需要帮助。我正在尝试制作一个简单的图库。我已完成上传部分图库。现在,我希望从文件夹中获取这些图像,将其缩略图和将它们保存在数组中,以便稍后显示。 这就是我到目前为止所面临的困境。该数组最后仍为空。
$folder = 'images/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
$thumbArray = array();
for($i=0; $i<$count; $i++){
if(($img = @imagecreatefromstring($files[$i])) !== FALSE) {
$width = imagesx($img);
$height = imagesy($img);
$boxSize = min($width,$height);
$boxX = ($width / 2) - ($boxSize / 2);
$boxY = ($height / 2) - ($boxSize / 2);
$thumbArray[$i] = imagecreatetruecolor(100, 100);
imagecopyresampled($thumbArray[$i], $img, 0, 0, $boxX, $boxY, 100, 100, $boxSize, $boxSize);
}
}
提前致谢。
答案 0 :(得分:1)
代码中存在的问题是:
if (($img = @imagecreatefromstring($files[$i])) !== FALSE) { ... }
似乎声明从未被执行过。
要使用文件名从文件中获取图像,您应该使用imagecreatefromjpeg
功能。因此,循环中的代码应如下所示:
$img = imagecreatefromjpeg($files[$i]);
$width = imagesx($img);
$height = imagesy($img);
$boxSize = min($width,$height);
$boxX = ($width / 2) - ($boxSize / 2);
$boxY = ($height / 2) - ($boxSize / 2);
$thumbArray[$i] = imagecreatetruecolor(100, 100);
imagecopyresampled($thumbArray[$i], $img, 0, 0, $boxX, $boxY, 100, 100, $boxSize, $boxSize);
最后,您可以使用imagejpeg
功能在浏览器中查看结果,或直接将其保存到文件中。