Drupal 7.使用 PHP 将图像添加到节点


Drupal 7. Adding images to a node with PHP

有没有办法以编程方式从特定文件路径添加图像数组?我想从我的drupal站点的一个文件夹中添加所有图像,并为它们添加花哨的框样式,可以这样做吗?我试过看 drupal.org 但没有成功。感谢您的帮助。

当然可以,只需启用 php 过滤器模块,并将 php 代码放在节点的主体中即可。确保将正文的格式设置为 PHP,而不是纯文本或 html。

因此,如果图像位于主题文件夹中,则可以使用以下方法抓取并显示显示它:

<img src="<?php print path_to_theme() . "/files/image.jpg"; ?>" title='some image'/>

您可以使用 php 的 scandir 函数获取目录中的所有文件。此函数将返回文件目录中所有文件的数组,之后您可以遍历此数组并将所有图像文件输出到屏幕上,如下所示:

$files = scandir("path/to/files/dir");
/*Unset the first 2 items in the array since they contain . and .. respectively */
unset($files[0]);
unset($files[1]);
foreach($files as $file)
{
    /* 
      Here we get the file extension 
      If the value of $file = "photo.jpeg"; this returns "jpeg"
    */
    $f_ext =  end(explode(‘.’, $file));   
    /*Checking if file is an image*/
    if($f_ext == 'jpg' || $f_ext == 'png' || $f_ext == 'gif'|| $f_ext == 'jpeg')
    {
         print "<img src='<?php print "path/to/files/$file"; ?>' title='some image'/>";
    }
}