0

I want to change some php code to select a random image from a specified directory rather than the same default image it currently selects. This is the piece of code that currently selects a default fallback image - if there is no image available:

 <?php
 $postCover = '';

 if ($post->hasImage()) {
  $postCover = $post->getImage($photoSize);
 }

 if (!$post->hasImage() && $params->get('photo_legacy', true)) {
  $postCover = $post->getContentImage();
 }

 if (!$postCover && $params->get('show_photo_placeholder', false)) {
  $postCover = $post->getImage($photoSize, true);
 }
 ?>

 <?php if ($postCover) { ?>
 <div class="eb-mod-thumb<?php if ($photoAlignment) { echo " is-" . $photoAlignment; } ?> <?php if (isset($photoLayout->full) && $photoLayout->full) { echo "is-full"; } ?>">
  <?php if (isset($photoLayout->crop) && $photoLayout->crop) { ?>
            <a href="<?php echo $post->getPermalink();?>" class="eb-mod-image-cover"
                style="
                    background-image: url('<?php echo $postCover;?>');
                    <?php if (isset($photoLayout->full) && $photoLayout->full) { ?>
                    width: 100%;
                    <?php } else { ?>
                    width: <?php echo $photoLayout->width;?>px;
                    <?php } ?>
                    height: <?php echo $photoLayout->height;?>px;"
            ></a>
  <?php } else { ?>
            <a href="<?php echo $post->getPermalink();?>" class="eb-mod-image"
                style="
                    <?php if (isset($photoLayout->full) && $photoLayout->full) { ?>
                    width: 100%;
                    <?php } else { ?>
                    width: <?php echo (isset($photoLayout->width)) ? $photoLayout->width : '260';?>px;
                    <?php } ?>"
            >
                <img src="<?php echo $postCover;?>" alt="<?php echo $post->title;?>" />
            </a>
  <?php } ?>
 </div>
<?php } ?>

I think it is this line I need to change:

<img src="<?php echo $postCover;?>" alt="<?php echo $post->title;?>" />

I have found this solution on here: PHP pull random image from folder

<?php

$dir = "images/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
?> <img src="images/<?php echo $images[$i]; ?>" alt="" />

Which seems to be able to do what I want, but I am unsure how to incorporate it (or where) in the code I have supplied (not having php experience, but trying to learn).

Can anyone help me?

Kind regards

Mel

Community
  • 1
  • 1
Malc
  • 17
  • 6
  • Hi FareedMN, I am trying to apply this (thanks) but I cannot get it to work. I have created a folder called newimage and placed some random images in it, then in my code I added the folowing: $path='/images/rsceb/260x150/modules/mod_responsive_scroller_for_easyblog/assets/images/fallback.jpg"; if(stristr($postCover,$path) !==false){$dir = "images/newimage/"; $images = scandir($dir); $i = rand(2, sizeof($images)-1); $postCover="images/".$images[$i]; } But it just shows code above a blank image – Malc Feb 09 '16 at 17:09

2 Answers2

0

Let's say the link to your default image is : /path/to/default_imge.jpg

So a little solution for someone who doesn't like to create a big mess for this is :

.....

if (!$postCover && $params->get('show_photo_placeholder', false)) {
                $postCover = $post->getImage($photoSize, true);
            }
            ?>
// New Code Starts

$path='/path/to/default_imge.jpg';
if(stristr($postCover,$path) !==false){
$dir = "images/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
$postCover="images/".$images[$i];
}

// New Code Ends

            <?php if ($postCover) { ?>
.......

In this case you can go back to the normal behavior just be removing the new code.

FareedMN
  • 152
  • 7
  • Hi FareedMN, I am trying to apply this (thanks) but I cannot get it to work. I have created a folder called newimage and placed some random images in it, then in my code I added the folowing: – Malc Feb 09 '16 at 17:02
  • please double check your directory and the path to your default image .... it can be tricky to get the right path Malc – FareedMN Feb 09 '16 at 17:32
0

This will not answer your question, but this will help you while learning PHP/HTML You mix up html and PHP in a way that makes the code unreadable.

In place of doing this:

if ($var) { ?> <div>Some HTML</div> <?php }
else { ?> <div>Some other HTML</div> <?php } ?>

Do this:

if($var){
    echo "<div>Some HTML</div>";
}
else{
    echo "<div>Some other HTML</div>";
}

It's a common beginner mistake, that makes it more difficult to learn coding because it "obfuscate" your code. This will make your code more understandable for you and for us :)

Math
  • 666
  • 8
  • 26