【问题标题】:How display photo under the text?如何在文字下显示照片?
【发布时间】:2014-02-11 04:41:17
【问题描述】:

我必须使用数组和 rand 函数显示随机照片。

现在我会在每张照片下添加文字(例如,长辈的话文字与图像)

我的代码:

<?php


$images=array(
    array('file'=>'pic1','alt'=>"Your Description About pic1"),
    array('file'=>'pic2','alt'=>"Your Description About pic2"),
    array('file'=>'pic3','alt'=>"Your Description About pic3"),
    array('file'=>'pic4','alt'=>"Your Description About pic4"),
    array('file'=>'pic5','alt'=>"Your Description About pic5"),
    array('file'=>'pic6','alt'=>"Your Description About pic6"),
    )
    $i=rand(0,count($images)-1);
    $selectimage="newfolder/{$images[$i]['file']}.jpg";
    $alt=$images[$i]['alt'];

 if (file_exists($selectimage) && is_readable($selectimage))

    $imagesize= getimagesize($selectimage);
?>

在htm中查看

<img src="<?php echo $selectimage;?> "  alt="<?php echo $alt ; ?>" <?php echo $imagesize[3] ; ?> /> 

我该怎么办?

【问题讨论】:

  • 那么究竟是什么问题?
  • 我无法在照片下显示文字 //////// 带有文字的随机图片
  • 您实际上也没有显示图像的代码

标签: php html css image


【解决方案1】:

您的代码中有语法错误。您需要在数组中使用, 而不是;,并在每个语句的末尾使用;

$images=array(
    array('file'=>'pic1','alt'=>"Your Description About pic1"),
    array('file'=>'pic2','alt'=>"Your Description About pic2"),
    array('file'=>'pic3','alt'=>"Your Description About pic3"),
    array('file'=>'pic4','alt'=>"Your Description About pic4"),
    array('file'=>'pic5','alt'=>"Your Description About pic5"),
    array('file'=>'pic6','alt'=>"Your Description About pic6")
    );

【讨论】:

    【解决方案2】:

    我知道的最简单的方法是包装图像和文本,然后相应地定位。

    <?php
    
       $images = array(
         array('file'=>'pic1','alt'=>"Your Description About pic1", 'text'=>"Pic1 Subtext"),
         array('file'=>'pic2','alt'=>"Your Description About pic2", 'text'=>"Pic1 Subtext")
         );
    
       $i=rand(0,count($images)-1);
       $selectimage="newfolder/{$images[$i]['file']}.jpg";
       $alt=$images[$i]['alt'];
       $subtitle = $images[$i]['text'];
    
    
       if (file_exists($selectimage) && is_readable($selectimage)) {
         $imagesize= getimagesize($selectimage);    
       }
    
    ?>
    
    <div class="image_wrapper">
       <img src="<?= $selectimage; ?>" alt="<?= $alt; ?>"/>
       <p class="img_subtitle"><?= $subtitle; ?></p>
    </div>
    

    【讨论】:

    • 有个不错的idea.but id没有display alt photo.//我有display 比如长辈文字的words with photo
    • 在样式标签中使用 getimagesize() 的第三个参数将不起作用,因为输出用于 img 标签内部,如下所示:height="yyy" width="xxx"
    • @Mortzea 对不起,我在没有吃午餐的时候读得更好。 :) 但你可以在你想要的任何文字。如果您想将照片与文字组合在一起,我会将其包装在 div 中。也许在数组中添加另一个字段,其中包含每张照片所需的文本。
    • 你能举个例子吗?
    • 我编辑了上面的例子。你说的是这个吗?
    【解决方案3】:

    我找到了。

    对于显示文本应将文本添加到数组中。

    <!doctype html>
    <html>
     <head>
     <?php 
    
      $images= array(
    array('file' => 'pic1' ,'txt'=>'sample1', 'alt' => "Your Discription About pic1"),
    array('file' => 'pic2','txt'=>'sample2' , 'alt '=> "Your Discription About pic2"),
    array('file'=> 'pic3' ,'txt'=>'sample3', 'alt'  => "Your Discription About pic3"),
    array('file' => 'pic4','txt'=>'sample4' , 'alt' => "Your Discription About pic4"),
    array('file'=>'pic5','txt'=>'sample5' , 'alt'  => "Your Discription About pic5")
    );
    
    $i=rand(0,count($images)-1);
    $selectimage="newfolder/{$images[$i]['file']}.jpg";
    $alt=$images[$i]['alt'];
    if (file_exists($selectimage) && is_readable($selectimage)) {
    $imagesize= getimagesize($selectimage);
    }
    
    ?>
    </head>
    <body>
    <img src="<?php echo $selectimage;?> " />
    <?php echo $images[$i]['txt'];?>
    </body>
    
    </html>
    

    【讨论】:

      猜你喜欢
      • 2019-11-06
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多