Web Code to show Animated Image from within Indigo Touch

Posted on
Sun Oct 14, 2018 10:20 pm
GlennNZ offline
User avatar
Posts: 1555
Joined: Dec 07, 2014
Location: Central Coast, Australia

Web Code to show Animated Image from within Indigo Touch

Hi all.

Thought would post this as has solved a small problem for me; and thought may be of use to others.

I largely use Indigo Touch on mounted Ipads and was needing a solution for animated images.

Essentially what the below does is cycle through the Weather Radar images available on bom.gov.au and creates a refreshing URL on a webpage/website. It uses PHP to do this.

Prerequisites:
Website with php - either local (works well) and I have run xampp on a VM for some years - serving the odd page and bit and pieces.
It also works remotely on a webpage fine - and have it here for a test run:
http://www.frontviewplus.com/radar2.php
Click on it and see - nothing to exciting a simple static radar image.
BUT - refresh it - there we go a new image and so and so on.


It uses PHP to change the image returned at every request.
To Indigo Touch this looks like a simple image return and it works well from with IndigoTouch as a refreshing URL.

Basically every time the image is requested, the below code -cycles to the next image and sends that.

Simply use the above as a 'refreshing URL' from within Indigo and Indigo Touch. Everytime the refreshing URL is refreshed the image will change.

In fact it works so well - I'm now on the look-out to see what else I can animated from within Indigo Touch!
And for static images - eg. animated Gifs - should be very straightforward as no image downloading needed....


Glenn


Code: Select all
<?php
// Example php page to download image and then cycle through images
// at all times looking like a simple direct url image to requesting program
// compatible with indigoTouch
// GlennNZ
// 2018


// Can use session details below to save information between sessions
// Unfortunatly does not work with Indigo and within IndigoTouch so moved to
// saving locale file for persistent information
// session_start();
// if( empty($_SESSION['image']) ) {
     // $_SESSION['image'] = 1;
// }

// Checks to see whether index file exists - if exists reads it for image number_format
if (file_exists('imageupto.txt') )
{
   $imagenumber = file_get_contents('imageupto.txt');
} else {
   $imagenumber = 1;
}

// send the request appearing like a Ipad - over coming the 403 errors for bom.gov.au website

$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n" .  // check function.stream-context-create on php.net
              "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad
  )
);

// if png files don't exist or age of files more than 1/2hr redownload....

if ( !file_exists('1.png') || (time()-filemtime('1.png') > 0.5 * 3600))
{
   
   // create the background from the various transparent layers from bom.gov.au
   $context = stream_context_create($options);
   $backgroundurl = 'ftp://ftp.bom.gov.au/anon/gen/radar_transparencies/IDR714.background.png';
   $topourl = 'ftp://ftp.bom.gov.au/anon/gen/radar_transparencies/IDR714.topography.png';
   $locations = 'ftp://ftp.bom.gov.au/anon/gen/radar_transparencies/IDR714.locations.png';
   $url = 'ftp://ftp.bom.gov.au/anon/gen/radar/IDR714.gif';
   $ftpradar = 'ftp.bom.gov.au';
   $img = 'radar714.gif';

   file_put_contents($img, file_get_contents($url, false, $context));

   $image_1 = imagecreatefrompng($backgroundurl);
   $image_2 = imagecreatefrompng($topourl);
   $image_3 = imagecreatefrompng($locations);
   imagealphablending($image_1, true);
   imagesavealpha($image_1, true);
   imagecopy($image_1, $image_2, 0, 0, 0, 0, 512, 512);
   imagecopy($image_1, $image_3, 0, 0, 0, 0, 512, 512);
   imagepng($image_1, 'background.png');

   // Above: Background image created.
   // Below: Login to ftp server and download the directory
   $conn_id = ftp_connect($ftpradar);
   $login_result = ftp_login($conn_id, 'Anonymous', 'Anonymous');
   ftp_pasv($conn_id,true);
   $contents = ftp_nlist($conn_id, "/anon/gen/radar/");
   $IDR714 = array();

   $index = 0;
   foreach ($contents as $subarray)
   {
      if (stristr( $subarray,'IDR714.T.') !== FALSE) { // Yoshi version
         //create a new array with just Radar images for Radar IDR714
         $index++;
         array_push($IDR714,$subarray);
         $image_4 = imagecreatefrompng('ftp://ftp.bom.gov.au/'.$subarray);
         $background = imagecreatefrompng('background.png');
         imagealphablending($background, true);
         imagesavealpha($background, true);
         imagecopy($background, $image_4, 0, 0, 0, 0, 512, 512);
         // Create new png files of all the radar images renamed to 1-18.png
         // So overwritten when updated.
         imagepng($background, $index.'.png');
         //imagepng($background, str_replace('/anon/gen/radar/','',$subarray));
      }   
   }
}


// Abandoned Session usage as doesn't work with Indigo
//$imagetouse = $_SESSION['image'].'.png';
// $_SESSION['image'] = $_SESSION['image']+1;
// if ($_SESSION['image'] == 17){
   // $_SESSION['image'] = 1;
// }

if ($imagenumber >=19){
   $imagenumber = 1;
}
$file = $imagenumber.'.png';
$imagenumber++;

// Write new index number to file to use new image
file_put_contents("imageupto.txt",$imagenumber ) ;
$type = 'image/png';
header('Content-Type:'.$type);
readfile($file);


?>

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests