Jump to content

Auto Generate XML from image directory


Zoomrix

Recommended Posts

Hello,

 

I'm a complete PHP newb. I'm curious whether PHP is even the best solution for my problem.

I'm trying to automatically generate an xml file with the following structure:

 

<?xml version="1.0" encoding="UTF-8"?>
<portfolio>

   <categories>
      <category id="film">Films</category>
      <category id="photo">Photography</category>
      <category id="graphic">Graphic Design</category>
      <category id="web">Web Design</category>
      
   </categories>

   <items>
      <item>
         <thumbnail>img/photo/003_small.jpg</thumbnail>
         <preview>img/photo/003.jpg</preview>
         <category>photo,film</category>
         <description>Description goes here.</description>
      </item>
      <item>
         <thumbnail>img/photo/003_small.jpg</thumbnail>
         <preview>img/photo/003.jpg</preview>
         <category>film</category>
         <description>Description goes here.</description>
      </item>
      <item>
         <thumbnail>img/photo/003_small.jpg</thumbnail>
         <preview>img/photo/003.jpg</preview>
         <category>graphic,photo</category>
         <description>Description goes here.</description>
      </item>
   </items>
</portfolio>

 

(You can ignore the description as I can put that in manually after the fact)

There are lots of images and I just know that theres a way to fascilitate a script for this repetitive task to read a directory of a folder named "img" - see that there are a few folders named "photo" "film" and "graphic" etc, pull the images out of them in their corresponding categories etc.

I just need the initial generated script with thumbnails and all the attributes, I can do the final tweeks afterward.

 

This is for http://www.zoomrix.com/#portfolio

 

This is the code that I'm working from right now (gathered from a few sources)

 

<?php
$path_to_image_dir = 'images2'; // relative path to your image directory

$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<items> 
</items>
XML;

$xml_generator = new SimpleXMLElement($xml_string);

if ( $handle = opendir( $path_to_image_dir ) ) 
{
    while (false !== ($file = readdir($handle))) 
    {
        if ( is_file($path_to_image_dir.'/'.$file) ) 
        {
           $item = $xml_generator->addChild('item');  
           $item->addChild('thumbnail', $path_to_image_dir.'/thumbs/'.$file);    
           $item->addChild('preview', $path_to_image_dir.'/'.$file);   
           $item->addChild('category', 'photo'); 
           $item->addChild('description', ' ');     
        }
    }
    closedir($handle);
}

header("Content-Type: text/xml");

echo $xml_generator->asXML();
?>

(The above code could be ran here: http://www.zoomrix.com/index5.php )

 

I just can't figure out how to include the <portfolio> and <category> tags, or how to reference a file structure to input for the category child (depending on what folder an image is in).

All the help would be greatly appreciated, even if you point me to the right tutorial.

 

Thanks,

 

GeorgeKotelnikov

www.zoomrix.com

Link to comment
https://forums.phpfreaks.com/topic/231117-auto-generate-xml-from-image-directory/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.