我有一系列alt标签:
$array_alt=array("hii,
"Best",
"abc",
//"board certified",
"xyz",
"hello",
"new"
)
现在我想要的是将这些alt标签动态添加到 img src ,即图片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++;
}
提前致谢
答案 0 :(得分:2)
首先你的声明是错的..
$array_alt=array("hii,
"Best",
"abc",
//"board certified",
"xyz",
"hello",
"new"
)
将是
$array_alt=array("hii",
"Best",
"abc",
//"board certified",
"xyz",
"hello",
"new"
)
答案 1 :(得分:1)
修改了一下你的代码(在alts数组中"
之后添加了遗漏"hii
,检查$array_alt
中是否存在密钥,简化扩展检查),它似乎对我有用:< / p>
<?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++;
}
?>
答案 2 :(得分:0)
$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++;
}