Scraping non-absolute URL - php

I am trying to scrape www.weather.bm. I want all 10 radar images, but I can only get one (the image updates regularly) and it's not a absolute image url. I was hoping I could use the image as a image slideshow like the link but dont know how. Also, how can I remove images/Radarlegend.png? I just need the radar images.
Here is my code:
include('simple_html_dom.php');
$html = file_get_html('http://www.weather.bm/radarMobile.asp');
foreach($html->find('img') as $element)
echo $element->src . '<br>'
My output is:
<div id="main">
images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1556.jpg<br>images/Radarlegend.png<br></div>
</div>

There is a list of all ten images is in the JavaScript (formatting changes mine):
radarFileNames = new Array(
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1446.jpg',
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1456.jpg',
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1506.jpg',
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1516.jpg',
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1526.jpg',
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1536.jpg',
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1546.jpg',
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1556.jpg',
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1606.jpg');
A simple search through for the line containing radarFileNames = new Array will return this line. Then you can extract the URLs with two preg_split()s: first on \(|\), then split the second token on ', '. Finally, prefix http://www.weather.bm/ to the URLs.

$localOffset = "+2 Hours"; //change this to your local offset from the image times you need. for me (CST) it's 2 hours.....
$start= strtotime("-90 Minutes " . $localOffset); // go back 90 minutes (The limit of the available images)
$start=($start-($start % (600))+(60*6)); //go to the next earlier 6 minute mark (all images appear to be at the 6 minute mark))appear to be at the 6 minute mark))
for($x=1;$x<=9;$x++){
$DateTime = date("Y-m-d-Hi", $start); // set the image time we want.
$fName = ("http://www.weather.bm/images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-$DateTime.jpg"); // set the filename
echo" $min <img src='$fName' alt='$fName'/><br/>"; // echo (or add to a stack or variable...) your image node
$start+=(600);//add 10 minutes
}

Related

System hit counter SaaS - Via TXT - Imagecopymerge

I am doing an hit counter (view) system, and now I need to find a way to merge all number images into one to be possible to embed in another pages, could someone help me out?
Here is the code I have:
$number = trim(file_get_contents('visitas.txt'));
$file = 'visitas.txt';
$views = file_get_contents ($file);
$fdata = intval($views)+1;
file_put_contents($file, $fdata);
$array = str_split($number);
if(!empty($array)){
foreach($array as $single){
echo '<img style="height:20px" src="'.$single.'.png">';
}
}else{
echo '<img src="0.png">';
}
$image = imagecreatefromstring(file_get_contents('sample.jpg'));
At the moment everything is working, each refresh is counting and sum the number writed by image, but I need to embed this counter in another pages, something like:
<img src='https://countersite.com.br/img-counter-385503.jpg'>
Here is a image of the counter working:
UPDATED
I need help to merge the image already created with the numbers (0.png, 1.png...) into one image (counter1.png).

How to show/hide banner, based on it's from_date and end_date in PHP?

I'm trying to work on an index page, and to implement minor modifications to the said page. Currently I do have a banner management section within the admin panel where I'm able to upload an image along with a title. Thus, my index page will show all entered banners as a slide show.
However, I've been trying to make it abit more organized. For that I edited the database and added fields such as banner_id, banner name, Fromdate, EndDate and bannerimage. However instead of showing the entire banner content, I want to show banner image based on the date and time which I added into the database.
is there any way to sort this filtering method? I'm trying to learn and improvise on my own, but then again due to the fact that I'm still a beginner and I do know the logical application I'm failing to implement it code wise.
----This is the current code that I have to handle the slide sow---
$image_dir = "$_SERVER[DOCUMENT_ROOT]/images/img"; // directory on server
$image_relative_path = 'images/img'; // path to images relative to script
$file_types = array('jpg', 'jpeg', 'gif', 'png');
$image_time = '10000'; // seconds each image will display (4000 = 4 seconds)
$image_rotation ='';
if ($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$ext_bits = explode(".", $file); // finds file extensions
foreach ($ext_bits as $key => $value) {
if (in_array($value, $file_types)) {
$image_rotation .= '<div data-p="170.00"><img data-u="image" src="'.$image_relative_path.'/' . $file . '"/></div>';
}
}
}
}
closedir($handle);
}
?>
<div class="container">
<div id="jssor_1" style="position:relative;margin:0 auto;top:0px;left:0px;width:1300px;height:500px;overflow:hidden;visibility:hidden;">
<!-- Loading Screen -->
<div data-u="loading" class="jssorl-009-spin" style="position:absolute;top:0px;left:0px;width:100%;height:100%;text-align:center;background-color:rgba(0,0,0,0.7);">
<img style="margin-top:-19px;position:relative;top:50%;width:38px;height:38px;" src="images/spin.svg" />
</div>
<div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:1300px;height:500px;overflow:hidden;">
<?php echo $image_rotation; ?>
</div>
----The above code works fine, but it doesn't give me the privilege to show/hide banner based on the dates entered into the database.---
So, Currently I am uploading each banner as
banner name, banner image, Date & time to start showing banner, end date and end time for banner. (However, I'm using a datepicker that accepts the date and time within a single package.
So, let's say I upload 10 banner, I don't want it to be shown on the index page right away, I want it to be displayed within the dates as per the database
As this is tagged with PHP I'll provide a PHP answer.
With PHP you can compare DateTime by using a variety of comparison operators.
For your case, it's best suited to use <= (less than or equal to) and >= (greater than or equal to).
Your logic will essentially be:
if date is greater than or equal to DateA and is less than or equal to DateB - show banner.
<?php
$today = date('Y-m-d H:i:s');
$dateA = date('Y-m-d H:i:s', strtotime('13 Nov 2018'));
$dateB = date('Y-m-d H:i:s', strtotime('21 Dec 2018'));
echo $today. '<br />';
echo $dateA. '<br />';
echo $dateB. '<br />';
if ($today >= $dateA && $today <= $dateB) {
echo 'show banner';
}
Here we make use of PHPs in-built functions date and strtotime to declare today, DateA (the start of the range) and DateB (the end of the range). I used a range I know would display show banner just for examples sake.
Note: This isn't a copy-paste solution, this just shows you an answer to your problem for you to edit/tweak to suit your code :)

I have to manually add array values after split on img tag

I got articles that I split on the first image tag, so I can put the image in a container-fluid (full width of the page instead of a standard bootstrap format). This works fine, except when I add more images, the array is further split, and so just having $article[0] (introtext) $article[1] (full width image) and $article[2] (rest of the text) is not enough. Since the next image and text are in value 3 and 4.
How can I make sure all the following data after the first split stays inside value 2? Or maybe automatically check how long the array is and add those values dynamically?
My code now:
// Product \\ Content
$content = "SELECT * FROM `lb_content` WHERE alias = '".$conn->real_escape_string($_GET['alias'])."' ";
$contentcon = $conn->query($content);
$contenti = array();
while ($contenti[] = $contentcon->fetch_array());
$introtext = $contenti[0]['introtext'];
preg_match_all('/(<img[^>]+\>)/i', $introtext, $artikelimages);
$artikelimages = $artikelimages[0];
$splitartikel = preg_split('/(<img[^>]+\>)/i', $introtext, -1, PREG_SPLIT_DELIM_CAPTURE);
echo $splitartikel[0];
echo $splitartikel[1]; (inside container fluid but ill just add the relevant code)
echo $splitartikel[2];
Now when there is another image, I have to add:
echo $splitartikel[3];
echo $splitartikel[4];
manually. For the new image and text after that image.
After echoing the first two pieces, join the rest of the array (if any) and echo the result:
echo implode(array_slice($splitartikel,2));

How to display frames that are separated from an animated GIF?

I'm trying to use GifFrameExractor. Full code for it is at the link if needed, but I dont think you will for the purposes of this question.
My code on my site is the following, using an animated gif of Homer Simpson...
$gifFilePath = 'images/homer.gif';
if (GifFrameExtractor::isAnimatedGif($gifFilePath)) { // check this is an animated GIF
$gfe = new GifFrameExtractor();
$gfe->extract($gifFilePath);
// Do something with extracted frames ...
foreach ($gfe->getFrames() as $frame) {
// The frame resource image var
$img = $frame['image'];
echo $img;
// The frame duration
$duration = $frame['duration'];
}
}
But my addition of "echo $img;" just outputs a bunch of resource IDs like "Resource id #8Resource id #10Resource id #12" and so on.
I googled and it says resource ID means you're trying to output a resource but you first need to extract info from the resource.
So how do I extract the actual img info so that I can display each frame from the animated gif?
I basically just want to display each of the frames of the animated gif down the page.
Any ideas?

PHP pagination of 400+ pictures

a have a website with more than 400 pictures in a directory. I'd like to list them by 12 on every page. How can i do that? Here is my actual code:
<!doctype html>
<html lang="hu">
<head>
<title>Amatőr</title>
<meta charset="utf-8"/>
</head>
<body>
<h2>Vannak jó képeid? Töltsd fel őket és kikerülhetnek az oldalra!</h2>
<article>
<header>
Amatőr Lányok
</header>
<div id="kepek">
<?php
$imgdir = '../img/blog/img/amator/'; //Pick your folder
$allowed_types = array('png','jpg','jpeg','gif'); //Allowed types of files
$dimg = opendir($imgdir);//Open directory
while($imgfile = readdir($dimg))
{
if( in_array(strtolower(substr($imgfile,-3)),$allowed_types) OR
in_array(strtolower(substr($imgfile,-4)),$allowed_types) )
/*If the file is an image add it to the array*/
{$a_img[] = $imgfile;}
}
$totimg = count($a_img); //The total count of all the images
//Echo out the images and their paths incased in an li.
for($x=0; $x < $totimg; $x++){ echo "<a onclick='Lightbox.start(this, false, false, false, false); return false;' rel='lightbox[amator]' href='" . $imgdir . $a_img[$x] . "'><img class='kep_listaz' width='200px' height='160px' src='" . $imgdir . $a_img[$x] . "' /></a>";}
?>
</div>
</article>
</body>
</html>
Thanks!
So this is really more of a maths questions.
You will need to get the total count of the images, then divide that by the max number of images per page (make sure to ceil() it).
You have now a max number of pages needed to view all of these images. What you need to do now is figure whether you want page=1/page=2 etc or as a start and end. Both are relatively easy, however, with the page you'd need to do
$page = (int)$_GET['page'];
$start = $page * $max_items_per_page;
$end = $start + $max_items_per_page;
This way it's probably saver. Also add extra code to make sure you're not out of bounds with the page requested.
Now it's a matter of getting an array of the files (I suggest you use glob()) and use array_slice() that from start to end.
Finally just have a previous/next page, or list all (or some) of the pages. Getting next and previous is as simple as adding/removing 1 to $page, i.e. $next = $page + 1; and $prev = $page-1;. Again, for both of these, double check that you're not out of bounds. Probably best not to show next/previous if they are out of bounds.
There are two ways to paginate this kind of data: on the front-end where you send it all and JavaSctipt only shows the user a portion at a time, or on the back-end where the page is only rendered with a portion at a time and you reload the whole page to get more.
The fastest way to get it working is using the back-end method:
Before your for loop add $page = isset($_GET['page']) ? $_GET['page']-1 : 0;
Change the loop's argument to $x=$page*12; $x < $totimg && $x < ($page+1)*12; $x++
Then you can manipulate pages by adding ?page=3 to the url
You'll also want to add some error handling incase an invalid page number is submitted.

Categories