使用 PHP 重新调整目录中所有图像的大小?
Re-size all images in directory with PHP?
我正在做一些网络开发,我想使用 PHP 调整目录中所有图像的大小。目录是 public 属性,任何在该站点工作的人都可以插入图像(用作随机背景)。有没有办法检查使用 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
?>
所以我建议如下:
<?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;
?>
我正在做一些网络开发,我想使用 PHP 调整目录中所有图像的大小。目录是 public 属性,任何在该站点工作的人都可以插入图像(用作随机背景)。有没有办法检查使用 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
?>
所以我建议如下:
<?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;
?>