【问题标题】:Add PHP script in img src tag with current data-parameters使用当前数据参数在 img src 标签中添加 PHP 脚本
【发布时间】:2016-06-09 00:20:26
【问题描述】:

我有一个 php 脚本,它显示文件夹目录中的所有照片,我需要包含一个应用给定数据参数的 html 标记。 php代码是:

?php

$directory = "images/*/";


$images = glob("" . $directory . "*.png" );

$imgs = '';

foreach($images as $image){ $imgs[] = "$image"; }

shuffle($imgs);


$imgs = array_slice($imgs, 0, 20);


foreach ($imgs as $img) {
   echo "<img src='$img' /> ";
  }

?>

我需要把它作为img src放在html标签中:

<img src="" title="Ring" data-parameters='{"zChangeable": true, "x": 215, "y": 200, "colors": "#000000", "removable": true, "draggable": true, "rotatable": true, "resizable": true, "price": 10, "boundingBox": "Base", "autoCenter": true}' />

我尝试了而不是 echo " 创建具有价值 $img 的新 var 并使用,但这只给了我一张图片

【问题讨论】:

  • 你确定 glob 返回值吗?你测试过吗?
  • 是的,它可以工作,但我不知道如何将它包含在带有数据参数的 html 标记中。如果有人有更好的解决方案,那么我的 php 代码很高兴使用它,因为我是 php 的新手。提前谢谢
  • 确定有效吗? 你测试了吗?print_r( $images ); die();glob行之后)因为你输出img的代码是正确的,所以我敢打赌问题出在glob()命令...
  • 我对其进行了多次测试,如果我将该代码放在 exemple.php 中并将其包含在 index.php 中 它工作得很好,但没有数据参数='{“zChangeable”:真,“x”:215,“y”:200,“颜色”:“#000000”,“可移动”:真,“可拖动”:真,“可旋转”:真,“可调整大小": true, "price": 10, "boundingBox": "Base", "autoCenter": true}' />
  • 为了语义!这不会解决问题,但请尝试声明 $imgs = array(); 而不是将其声明为字符串。如果它是一个数组,那么它的声明应该显示出来。 :)

标签: php html image


【解决方案1】:

我找到了解决方案:)

foreach ($imgs as $img) {

    $name = basename($img, ".png"); 
    echo "<img title='$name' src='$img' data-parameters='{\"zChangeable\": true, \"x\": 215, \"y\": 200, \"colors\": \"#000000\", \"removable\": true, \"draggable\": true, \"rotatable\": true, \"resizable\": true, \"price\": 10, \"boundingBox\": \"Base\", \"autoCenter\": true}'/>"  ;
}

【讨论】:

    【解决方案2】:

    试试这个,如果有帮助会很高兴

    foreach (glob("images/*.png") as $image)
    {
        $imgs[] = $image;
    }
    
    shuffle($imgs);
    
    
    $imgs = array_slice($imgs, 0, 20);
    
    
    foreach ($imgs as $img) {
       echo '<img src="'.$img.'">';
      }
    
    ?>
    

    【讨论】:

    • 它与第一个代码的工作方式相同,thanx 但是我可以在 html 标记中使用该脚本的结果??我需要列出的图像具有参数:数据-参数='{“zChangeable”:真,“x”:215,“y”:200,“颜色”:“#000000”,“可移动”:真,“可拖动”:真,“可旋转”:真,“ resizable": true, "price": 10, "boundingBox": "Base", "autoCenter": true}' />
    • 它仍然显示单个图像?
    • 它显示所有图像,但我怎样才能把那个回声放在 标签中应用属性,图像可以是可拖动的,可调整大小的......
    • 从您的问题来看,我认为您在显示图像时遇到了困难,现在我明白了这个问题。在那种情况下,这不是一个正确的答案
    猜你喜欢
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多