Elxis CMS Forum

Support => Elxis 4.x/5.x DEV => Topic started by: CREATIVE Options on March 19, 2013, 11:00:05

Title: SiteMap for Images
Post by: CREATIVE Options on March 19, 2013, 11:00:05
An Image sitemap would help to crawled all the images.
It will be need a different XML sitemap, from that for the website link's.
Sample of image sitemap from Google Webmaster tools.

Code: [Select]
<?xml version="1.0" encoding="UTF-8"?>
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
 <url>
   <loc>http://example.com/sample.html</loc>
   <image:image>
     <image:loc>http://example.com/image.jpg</image:loc>
   </image:image>
   <image:image>
     <image:loc>http://example.com/photo.jpg</image:loc>
   </image:image>
 </url>
</urlset>

Compatible image sitemap with all the major search engines.

Can we add this feture into media browser ?
Title: Re: SiteMap for Images
Post by: datahell on March 23, 2013, 09:35:21
You can create a script outside Elxis that will read the images folder and generate the xml file you need for the sitemap.
Here is a generic code that read the contents of a folder, finds images and displays an XML sitemap like your sample. Name it "isitemap.php" and place it in your site root folder.

Code: [Select]
<?php 
$images 
= array();
$path = &#39;/absolute/path/to/images/folder/&#39;;
$handle opendir($path);
while (
$entry readdir($handle)) {
if (($entry != &#39;.&#39;) && ($entry != &#39;..&#39;)) {
if (!is_dir($path.$entry)) {
$ext strtolower(substr(strrchr($entry, &#39;.&#39;), 1));
if (in_array($ext, array(&#39;jpg&#39;, &#39;jpeg&#39;, &#39;png&#39;, &#39;gif&#39;))) { $images[] = $entry; }
}
}
}
closedir($handle);

//now display the XML sitemap
header(&#39;Expires: Mon, 26 Jul 1997 05:00:00 GMT&#39;);
header(&#39;Last-Modified: &#39;.gmdate(&#39;D, d M Y H:i:s&#39;).&#39;GMT&#39;);
header(&#39;Cache-Control: no-cache, must-revalidate&#39;);
header(&#39;Pragma: no-cache&#39;);
header(&#39;Content-type:text/xml; charset=utf-8&#39;);

echo &#39;<?xml version="1.0" encoding="UTF-8"?>
';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';
echo '<url>';
echo '<loc>http://www.example.com/isitemap.php</loc>';
if ($images) {
foreach ($images as $image) {
echo '<image:image><image:loc>http://www.example.com/relative/path/to/images/folder/'.$image.'</image:loc></image:image>';
}
}
echo '</url>';
echo '</urlset>';
?>
Title: Re: SiteMap for Images
Post by: CREATIVE Options on March 23, 2013, 10:27:06
Really thank you Datahell,
What about including it into Elxis 4.x & IOS Reservations ?
Title: Re: SiteMap for Images
Post by: nikos on May 08, 2013, 14:02:13
Code: [Select]

<?php 

$images 
= array();
$path = &#39;/absolute/path/to/images/folder/&#39;;
$handle opendir($path);
while (
$entry readdir($handle)) {
if (($entry != &#39;.&#39;) && ($entry != &#39;..&#39;)) {
if (!is_dir($path.$entry)) {
$ext strtolower(substr(strrchr($entry, &#39;.&#39;), 1));
if (in_array($ext, array(&#39;jpg&#39;, &#39;jpeg&#39;, &#39;png&#39;, &#39;gif&#39;))) { $images[] = $entry; }
}
}
}
closedir($handle);

?>



Using the above part of code i have created a gallery bot for Elxis 2009.x and the same as plugin i have started to do for Elxis 4.x also.

In order to sort images by their file names i have used the natsort (http://www.php.net/manual/en/function.natsort.php) function which does this i wanted, just reading all notes of php.net documentation about, there is a reference (http://www.php.net/manual/en/function.natsort.php#94812) that says "The natsort function will sort depending on the operating system, but not depending on either Linux or Windows-based systems. There's a difference when sorting an array which is generated from the filesystem ... so make sure you've named the files beginning with 01, then it works fine.".

So do you have a better suggestion about sorting or you think is fine to use the natsort function?
Title: Re: SiteMap for Images
Post by: datahell on May 08, 2013, 19:13:04
If you are inside Elxis you dont need to use a custom code as the one above. This is for scripts that runs outside of Elxis. An Elxis extension (module, plugin, etc) is executed inside Elxis so you have all the powerful Elxis libraries available (elxisFiles in this case).

In this case to get the images in a folder just do this:

Code: [Select]
$images = eFactory::getFiles()->listFiles('relative/path/to/images/folder/', '(.jpg)|(.jpeg)|(.png)|(.gif)$');
If you want to get the images in all sub-folders recursively:
Code: [Select]
$images = eFactory::getFiles()->listFiles('relative/path/to/images/folder/', '(.jpg)|(.jpeg)|(.png)|(.gif)$', true);but be aware that the above code willl return only the file names without the folder information.

To get the full paths recursively:
Code: [Select]
$images = eFactory::getFiles()->listFiles('relative/path/to/images/folder/', '(.jpg)|(.jpeg)|(.png)|(.gif)$', true, true);

elxisFiles documentation on Elxis docs (https://www.elxis.net/docs/developers/libraries/elxisfiles.html)
Title: Re: SiteMap for Images
Post by: nikos on May 08, 2013, 19:16:50
Thanks. And what about 2009.x ?
Title: Re: SiteMap for Images
Post by: datahell on May 08, 2013, 19:20:35
The same method exists in 2009.x but it is on the FileManager class and (attention) Elxis 2009.x requires the absolute path to the folder, not the relative one as Elxis 4.x.

Code: [Select]
global $mainframe, $fmanager;
$abs_path = $mainframe->getCfg('absolute_path').'/relative/path/to/images/folder/';
$images = $fmanager->listFiles($abs_path, '(.jpg)|(.jpeg)|(.png)|(.gif)$');
Title: Re: SiteMap for Images
Post by: nikos on May 08, 2013, 19:27:11
Reading the code of easygallery bot i saw that you used a similar one as this you wrote at the top of topic

Code: [Select]

$images = array();
$images['files'] = array();
$images['title'] = '';
$images['link'] = '';
$dir = $mainframe->getCfg('absolute_path').'/'.$gallery_element.'/';
if (file_exists($dir) && is_dir($dir)) {
if ($handler = opendir($dir)) {
$pat = "#((\.jpg)|(\.jpeg)|(\.png)|(\.gif))$#i";
$n = 0;
while (($f = readdir($handler)) !== false) {
if (preg_match($pat, $f)) {
if ($n >= $maximages) { break; }
$n++;
$images['files'][] = array(
'filename' => $f,
'url' => self::secureLive().'/'.$gallery_element.'/'.$f,
'title' => self::titlefromfile($f)
);
}
}
}
closedir($handler);
}
        return $images;
}


and telling me now that inside Elxis i do not need it, you confused me. Could you please make it a little bit more clear?
Title: Re: SiteMap for Images
Post by: datahell on May 08, 2013, 19:44:16
This is because I needed a different regex pattern than the one Elxis core provides (actually the insensitive "i" modifier).
See the pattern I used ($pat) and see the inputs of the listFiles methods to understand why I did it that way.
Title: Re: SiteMap for Images
Post by: nikos on May 08, 2013, 19:48:49
OK i will read all and i will check the code again. Bot as i did it works fine on elxis 2009.x site for a friend he asked about. Just i have still the question of natsort function if it is the right one for sorting images by file name.
Title: Re: SiteMap for Images
Post by: datahell on May 08, 2013, 19:52:23
I would use the simple sort function because it is simpler, faster and doesn't rely on the OS php runs. natsort provides human sorting but this is not always required. It depends on what you want to do. The difference between sort and natsort is well documented in php.net web site in the link you provided.
Title: Re: SiteMap for Images
Post by: nikos on May 08, 2013, 20:04:34
I had tried sort but i had different results of "logic" sorting in cases that you have image names as image_1.jpg, image_2.jpg, image_3.jpg....image_n.jpg and i also tried all sort_flags and finally i saw that the best "logic" sorting does it with natsort. But getting the 1st key of the array $images[0] which i use as the initial thumb for the gallery it gives different result on localhost than on live site. You can see it here (http://www.guzzista.gr/photos.html) how it works.

Thanks Datahell for your reply.

I will check the code again and i will publish on EDC the gallery bot which i am sure that people will like it.
Title: Re: SiteMap for Images
Post by: datahell on May 08, 2013, 21:20:20
You must be careful with natsort, see "potential gotchas" in the example2 (http://www.php.net/manual/en/function.natsort.php#example-4900) of php.net (and its not only that).
Personally I have never used either seen it somewhere in any script. If you need a custom ordering method use usort and uasort functions (they are VERY powerful).
For simple ordering by name use sort and if you dont want results like file1, file10, file2 use a naming method like: file01, file02, file10.

BTW, php.net is down :o (it happens even to the best families)
Title: Re: SiteMap for Images
Post by: nikos on May 08, 2013, 22:44:52
I just did it with simple sort function and works fine even at the 1st image as initial thumb. So i am satisfied and my friend became now more happy with the results.. :)

Thanks again