我有一个alt标签的数组:
$array_alt=array("hii,
"Best",
"abc",
//"board certified",
"xyz",
"hello",
"new"
)
现在我想要的是将这些替代标签添加到 img src ,即 image 1 alt标签应该是" HII" ,其余的。
这就是通过文件夹动态调用图像的方式,所有图像出现在滑块中:
$images = scandir(images/all_images); // get path
sort($images,1); // 1 is to sort images numerically
foreach($images as $img)
{
if($img === '.' || $img === '..')
{
continue;
}
// check extensions as we need only images
if ((preg_match('/.jpg/',$img)) || (preg_match('/.gif/',$img)) || (preg_match('/.tiff/',$img)) || (preg_match('/.png/',$img)) )
{
list($width, $height, $type, $attr) = getimagesize($path.$img);
if(($width<$height) || ($height>500) )
{
?>
<div class="item img-landscape"><img class="lazyOwl" alt="" data-src="<?php echo $path.$img; ?>" ></div>
<?php
}
else{
?>
<div class="item"><img class="lazyOwl" alt="" data-src="<?php echo $path.$img; ?>" ></div>
<?php
}
}
else
{
continue;
}
}
如果有人可以帮助我。
我尝试使用它,但似乎没有用。
$images = scandir($img_path);
sort($images,1); // 1 is to sort images numerically
$i=0;
foreach($images as $img)
{
echo $array_alt[$i]."<br>";
if($img === '.' || $img === '..')
{
continue;
}
// check extensions as we need only images
if ((preg_match('/.jpg/',$img)) || (preg_match('/.gif/',$img)) || (preg_match('/.tiff/',$img)) || (preg_match('/.png/',$img)) )
{
list($width, $height, $type, $attr) = getimagesize($path.$img);
if(($width<$height) || ($height>500) )
{
?>
<div class="item img-landscape"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
<?php
}
else{
?>
<div class="item"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
<?php
}
}
else
{
continue;
}
$i++;
}
预先感谢
首先您的声明是错误的。
$array_alt=array("hii,
"Best",
"abc",
//"board certified",
"xyz",
"hello",
"new"
)
将是
$array_alt=array("hii",
"Best",
"abc",
//"board certified",
"xyz",
"hello",
"new"
)
对您的代码进行了一些修改(在Alts数组中添加"hii
之后丢失的"
,检查$array_alt
中是否存在键,简化扩展检查),并且似乎对我有用:
<?php
$array_alt = array(
"hii",
"Best",
"abc",
"board certified",
"xyz",
"hello",
"new"
);
$path = '/public_path/';
$images = scandir($img_path);
sort($images, SORT_NUMERIC); // SORT_NUMERIC == 1 is to sort images numerically
$allowedExtensions = array('jpg', 'gif', 'tiff', 'png');
$i = 0;
foreach ($images as $img) {
if (isset($array_alt[$i])) {
$alt = $array_alt[$i];
} else {
$alt = "default_alt_value";
}
echo $alt . "<br>";
if ($img === '.' || $img === '..') {
continue;
}
// check extensions as we need only images
if (!in_array(pathinfo($img, PATHINFO_EXTENSION), $allowedExtensions)) {
continue;
}
list($width, $height, $type, $attr) = getimagesize($path . $img);
if (($width < $height) || ($height > 500)) {
?>
<div class="item img-landscape"><img class="lazyOwl" alt="<?php echo $alt ?>"
data-src="<?php echo $path . $img; ?>"></div>
<?php
} else {
?>
<div class="item"><img class="lazyOwl" alt="<?php echo $alt ?>"
data-src="<?php echo $path . $img; ?>"></div>
<?php
}
$i++;
}
?>
$array_alt=array("hii",
"Best",
"abc",
//"board certified",
"xyz",
"hello",
"new"
);
$img_path="images/slider";
$path=JURI::BASE().$img_path."/"; // url/path of the images
$images = scandir($img_path);
sort($images,1); // 1 is to sort images numerically
$i=0;
foreach($images as $img)
{
if($img === '.' || $img === '..')
{
continue;
}
// check extensions as we need only images
if ((preg_match('/.jpg/',$img)) || (preg_match('/.gif/',$img)) || (preg_match('/.tiff/',$img)) || (preg_match('/.png/',$img)) )
{
list($width, $height, $type, $attr) = getimagesize($path.$img);
if(($width<$height) || ($height>500) )
{
?>
<div class="item img-landscape"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
<?php
}
else{
?>
<div class="item"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
<?php
}
}
else
{
continue;
}
$i++;
}