Jump to content

"Take down" Image after Time Expires


louisstephens
 Share

Recommended Posts

So I have an odd feature to tackle. I want to include an image in the template (aka a header), but want to implement a function to set a time in the future. Once the time was met, the image will automatically come down (if possible switch to another image, but that is not something Ill worry about now). 

Now, I set up an image field "timed_image", and I set up the date/time field "select_timed_expire". I also know how to echo the information on the page, but where I am finding issue is actually how to implement this. I could use javascript and pass the date/time to it, but from there I am quite lost on how to implement this.

Has anyone done something like this before?

Link to comment
Share on other sites

Something like this?

$now = date("Y-m-d h:i:s");
$expires = $page->select_timed_expire // assuming you set output format for this field to the same as $now.

if ($now < $expires){
      echo "<img src='{$page->timed_image->url}'/>";

} else {
     // do something else or nothing.
}
  • Like 2
Link to comment
Share on other sites

Same solution as Reno, but I'd compare the timestamps and not date strings :)

$now = time();
$expires = strtotime($page->select_timed_expire); // If you have a date string as output format

  • Like 2
Link to comment
Share on other sites

If you want to cycle to the next image after the expiration, you can use a multi-image field and show the first image until the expiration date, and then move on to the second image.

$now = date("Y-m-d h:i:s");
$expires = $page->select_timed_expire // assuming you set output format for this field to the same as $now.
$url = $page->timed_image->first()->url; // default to first image

// if it's past the expiration, get the 2nd image. 0 based index, so 1 is 2nd image.
if ($now > $expires) $url = $page->timed_image->eq(1)->url; 

echo "<img src='$url'/>"; 
Link to comment
Share on other sites

I had just found out about using first();. but had a quick question. Most of the pages will have the default single image as the header, but once in awhile I will need to run a different image in its' place (set to expire). Will the above example cause an issue on the pages that only have the one image and nothing before it in the array?

Link to comment
Share on other sites

You would just need to check if there is an expiration set, if there isn't, then just stick with the first image. If there is an expiration set, then check the dates and respond accordingly.

untested, written in browser.

$url = $page->timed_image->first()->url; // default to first image, make sure your image field is set to multiple.
$expires = $page->select_timed_expire // assuming you set output format for this field to the same as $now.

if ($expires){
    if (date("Y-m-d h:i:s") > $expires){
        $url = $page->timed_image->eq(1)->url; // if it's past the expiration, get the 2nd image.
    }
}

echo "<img src='$url'/>"; 
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By spercy16
      These issues should be fairly easy for any intermediate to advanced ProcessWire developer to answer. I'm new to PHP and relatively new to ProcessWire and just need a bit of help. What I'm trying to do is bring in a couple of cards from my Projects page to display on my home page. I finally got the code right to bring in the cards but right now they're using my original images instead of my resized "variations". So firstly, I would like to know how to reference the variations of my images instead of using the original. Secondly, I need to grab only four of the cards from the Project page and not import in all ten. It should be just two small changes to my code to do these things (I would imagine). Here is the code I currently have for that section:
      <?php // https://processwire.com/api/arrays/ // check if the array of images has items if (count($pages->get("/projects/")->images)) : // get array of images from the field $images = $pages->get("/projects/")->images; $count = 0; // iterate over each one foreach ($images as $image) : $count++; $sectionText = $pages->get("/projects/")->get("paragraph_$count"); $img = $image; $buttonCode = $pages->get("/projects/")->get("url_$count"); ?> <span id="card<?php echo $count?>" class="card"> <img class="cardThumb" src="<?php echo $img->url; ?>" alt="<?php echo $image->description; ?> Thumbnail" /> <div class="cardBody"> <div class="cardText"> <h2><?php echo $img->description; ?></h2> <?php echo $sectionText; ?> </div> <div class="primaryBtn"> <a href="https://www.paypal.com/donate?hosted_button_id= <?php echo $buttonCode; ?> &source=url"> <button> <i class="fas fa-donate"></i> Donate </button> </a> </div> </div> </span> <?php endforeach; endif; ?> Thanks in advance for any help!
    • By picarica
      so hello there i have fiel field type i have lots of stuff there, and also an image i can easily get image from that field using
                                                        
      $options = array('quality' => 85, 'upscaling' => true, 'cropping' => 'north', 'sharpening' => 'medium'); $word = ".png"; // Test if string contains the word foreach($childgames->subor_hry as $file) { if(strpos($file, $word) !== false){ /* $imger = $file->size(473, 266, $options); */ echo $file; /* echo $file->url; */ } } so i get the fiel i tried invoking size on it like the commented out part and it doesnt work i get error Oh snizzle… Error:     Exception: Method Pagefile::size does not exist or is not callable in this context (in
      what am i doing wrong? can you guys help me by all logic this should work
    • By Max Esnée
      Hi,
      When I upload a large(ish) SVG (≈120ko in this case) into an image field, it tends to get stuck at 100% and block the whole website. If I force refresh the admin page, the svg has not been uploaded. The file is correctly copied into the assets folder though. Other svg tends to be uploaded fine albeit I've noticed that pages containing svg images in the admin are very slow to load generally. Other pages using the same template but with jpeg or png images are much faster to load.
      I get the following error in the JS console:

      I develop locally using MAMP on macOS and I'm using Firefox. I'm using the last ProcessWire version (3.0.165).
      I also tested in Chrome and got the same error.
    • By rjgamer
      Hi guys,
      the field "redirect_last" of type DateTime got not updated. The update on the field "redirect_counter" works and got saved.
      Does anybody know what I did wrong in my code?
      if ($input->urlSegment(1) === 'redirect') { $page->of(false); $page->redirect_last = time(); $page->redirect_counter += 1; if ($page->save('redirect_counter')) { $session->redirect($page->website_url, 302); } } Thanks.
    • By Marvin
      Hello, my name Marvin, i want to ask something. I'm new at processwire, and still learn it, i try yo showing an image, at a table, the image was show, but i can't resize the image
      please HELP
      Here i attach, my code belor
      <?php $num = 1; foreach($pages->get("/files/")->children as $child) { $current = $child === $page ? " class='current'" : ''; $result = $child->images; // $result->width(900); // $result->height(100); foreach($result as $items){ foreach($pages->get($child->name)->files as $file) { // $file = $child->files; // echo $file->name; echo "<tr><td>".$num++.".</td><td>".$child->title."</td><td>".$child->text_1."</td><td>".$child->text_2."</td><td>".$child->text_3."</td><td><a href='".$file->httpUrl."'>".$file->name."</a></td><td><img src='".$items->url."'></td></tr>"; } } } ?>  
×
×
  • Create New...