使用PHP重新调整目录中的所有图像大小?

时间:2015-04-17 01:36:44

标签: php glob

我正在进行一些Web开发我想使用PHP调整目录中的所有图像。该目录是公共属性,在该站点上工作的任何人都可以插入图像(用作随机背景)。有没有办法检查使用PHP从文件名数组中选择的图像大小?此外,如果参数太大,您可以将图像转换为特定尺寸吗?

<?php

$bg = glob("imgfiles/*.*"); $add image names to array
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set background equal to which random filename was chosen
?>

1 个答案:

答案 0 :(得分:1)

所以我建议像:

<?php
$maxw = 1024;
$maxh = 320;
$bg = glob("./imgfiles/*.*");  // add image names to array
$selectedBgPath = "./imgfiles/" . $bg[array_rand($bg)]; // set background path equal to which random filename was chosen
list($width, $height, $type, $attr) = getimagesize($selectedBgPath);
if($width > $maxw || $height > $maxh){
  // Resize as needed
  // $newSelectedBgPath
}
if(isset($newSelectedBgPath)){
  $fp = fopen($newSelectedBgPath, "rb");
} else {
  $fp = fopen($selectedBgPath, "rb");
}
header("Content-type: $type");
fpassthru($fp);
exit;
?>