Flickr api title - php

Hello everyone I am trying to make a gallery on my website and I am pulling the images/sets from flickr. I am able to load all the sets with this bit of code:
$flickr = simplexml_load_file('http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key='.$api.'&user_id='.$user_id.'');
foreach($flickr->photosets->photoset as $ps) {
echo '<img src="http://farm'.$ps['farm'].'.staticflickr.com/'.$ps['server'].'/'.$ps['primary'].'_'.$ps['secret'].'_q.jpg"><br />';
}
With this it will return a list of all the set's main images. However I also would like to add the title above it but the XML output of the title is in $flickr->photosets->photoset->title making it hard for me to get the title above every picture. Is there a easy way to get the title inside the foreach loop for the images but that the title also aligns correctly with the image?
the xml flickr outputs looks like:
<photosets page="1" pages="1" perpage="30" total="2" cancreate="1">
<photoset id="72157626216528324" primary="5504567858" secret="017804c585" server="5174" farm="6" photos="22" videos="0" count_views="137" count_comments="0" can_comment="1" date_create="1299514498" date_update="1300335009">
<title>Avis Blanche</title>
<description>My Grandma's Recipe File.</description>
</photoset>
</photosets>

If it's with the given XML, you can obtain it inside of the foreach loop with $ps->title.

Related

How to use light box on posted images that are extracted from database

I am trying to create a forums where my php file extracts the replies to the thread from my database and displays them in a foreach loop.
for example:
<?php
if(sizeof($arr_posts) > 0){
foreach ($arr_posts as $posts){
echo $posts;
}
}
?>
So $arr_posts holds the posts to this thread. So element of the array holds a html block that displays the post on the page.
However, the issue I have spent days on (not kiddig). When a user has submitted a reply with an image. I.e.
'Look at this dog <img src="dog.png">'
I want a lightbox theme to expand the image to make it bigger to zoom in. However, I have struggled to somehow get the lightbox by lokesh around the image tag in order for this to work. I needed to get this:
'Look at this dog '
is there some other way I can do this? Any suggestions are welcoming. :)
you can use
preg_match_all('/<img[^>]+>/i',$html, $result);
and replace anythings you want with $result in loops

Is there a way to get the album artwork img src from a discogs.com listing?

I am trying to use preg_match or preg_match_all to get the cover art from https://www.discogs.com/Lady-Gaga-Chromatica/release/15386439
So far I've only been able to get the Discogs logo and the favicon.
Here is the code I am using, which yields two images, the logo of Discogs.com and the favicon:
<?php
$text = file_get_contents("https://www.discogs.com/Lady-Gaga-Chromatica/release/15386439");
preg_match_all('/<img.*>/',$text,$out);
print_r($out);
?>
Edit 1:
I was asked to copy the data I'm trying to collect the image from, this is a lot of data so I am not able to post it here. It would be best if you could look at https://www.discogs.com/Lady-Gaga-Chromatica/release/15386439 and isnpect source to see the data.
Edit 2:
I looked at the console and see a 400 Bad Request code. But if I print out $text from the previous code, I can get the whole page to load on my webpage so I don't get why I can't fetch just the images.

xml string to php page

a photographer here with next to none programming experience. I am working on my website and got far enough (yes, I did a lot of searches) but cannot find the answer. Anybody can point me to the right direction?
I want to generate a code for my galleries. I have several different galleries/pages, and I would like to store all src, alt in one xml file, then bring it to a specific page.
SO far I have this
<?xml version="1.0"?>
<catalog>
<img id="pf01">
<src>foodphotography/01</src>
<alt>food1</alt>
</img>
<img id="pf02">
<src>foodphotography/01</src>
<alt>food2</alt>
</img>
<pf id="pf03">
<src>foodphotography/05</src>
<alt>food2</alt>
</pf>
</catalog>
and the php code
<?php
// Loading the XML file
$xml = simplexml_load_file("img.xml");
foreach($xml->children() as $img)
{
echo "<div class='swiper-slide'>\n";
echo "<img src='/images/".$img->src."' alt='".$img->alt."'>\n";
echo "</div>\n";
}
?>
I am trying to display on one page only <img> and on the other <pf>. Right now it displays all 3 images. How do I pick from the xml file only specific images? It looks like I cannot have more than one catalog.
Any better way to do this?
Thank you
There's lots of way you could filter on this, but since you're interested in having them on different pages, then you might just use xpath to filter the children. So to just show the img use the xpath /document/img
<?php
// Loading the XML file
$xml = simplexml_load_file("img.xml");
// swap children for xpath
foreach($xml->xpath('/catalog/img') as $img)
{
echo "<div class='swiper-slide'>\n";
echo "<img src='/images/".$img->src."' alt='".$img->alt."'>\n";
echo "</div>\n";
}
And on your other page, use the xpath /catalog/pf

Get image from RSS Feeds with no image URL

I would just like to to know how other developers manage to properly get/extract the first image in the blog main content of a site from URL in the RSS feed. This is the way I think of since the RSS feeds don't have image URL of the post/blog item in it. Though I keep on seeing
<img src="http://feeds.feedburner.com/~r/CookingLight/EatingSmart/~4/sIG3nePOu-c" />
but it's only 1px image. Does this one has relevant value to the feed item or can I convert this to maybe the actual image? Here's the RSS http://feeds.cookinglight.com/CookingLight/EatingSmart?format=xml
Anyway, here's my attempt to extract the image using the url in the feeds:
function extact_first_image( $url ) {
$content = file_get_contents($url);
// Narrow the html to get the main div with the blog content only.
// source: http://stackoverflow.com/questions/15643710/php-get-a-div-from-page-x
$PreMain = explode('<div id="main-content"', $content);
$main = explode("</div>" , $PreMain[1] );
// Regex that finds matches with img tags.
$output = preg_match_all('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $main[12], $matches);
// Return the img in html format.
return $matches[0][0];
}
$url = 'http://www.cookinglight.com/eating-smart/nutrition-101/foods-that-fight-fat'; //Sample URL from the feed.
echo extact_first_image($url);
Obvious downside of this function:
It properly explodes if <div id="main-content" is found in the html. When there's another xml to parse with another structure, there will be another explode for that as well. It's very much static.
I guess its worth mentioning also is regarding the load time. When I perform loop through out the items in the feed, its even more longer.
I hope I made clear of the points. Feel free to drop in any ideas that could help optimize the solution perhaps.
The image urls are in the rss file, so you can get them just by parsing the xml. Each <item> element contains a <media:group> element that contains a <media:content> element. The url to the image for that item is in the "url" attribute of the <media:content> element. Here is some basic code (php) for extracting the image urls into an array:
$xml = simplexml_load_file("http://feeds.cookinglight.com/CookingLight/EatingSmart?format=xml");
$imageUrls = array();
foreach($xml->channel->item as $item)
{
array_push($imageUrls, (string)$item->children('media', true)->group->content->attributes()->url);
}
Keep in mind, though, that the media doesn't necessarily have to be an image. It can be a video or an audio recording. There might even be more than one <media:group>. You can check the "type" attribute of the <media:content> element to see what it is.

Fetch URL from XML with jQuery and PHP - Jquery Slideshow

Im using the jQuery Nivo Slider, its pretty simple to setup:
<link rel="stylesheet" href="nivo-slider.css" type="text/css" media="screen" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="jquery.nivo.slider.pack.js" type="text/javascript"></script>
<!-- Then somewhere in the <body> section -->
<div id="slider">
<img src="images/slide1.jpg" alt="" />
<img src="images/slide2.jpg" alt="" title="#htmlcaption" />
<img src="images/slide3.jpg" alt="" title="This is an example of a caption" />
<img src="images/slide4.jpg" alt="" />
</div>
<div id="htmlcaption" class="nivo-html-caption">
<strong>This</strong> is an example of a <em>HTML</em> caption with a link.
</div>
However I want to be able to define the Images and captions in a seperate file, perhaps an XML file or text file. I'd like to store the images in a fairly readable fashion so that virtually anyone could update the file if need be.
So question is how can I generate HTML in the same structure as used above by generated automatically via PHP from a file?
I need to generate a list of images with some options:
I supposse the options are:
IMG src
href
alt
title (The title relates to the caption)
caption (The actual caption text)
Option in the PHP to order the images based on an ID
Only the IMG src, alt and title would be compulsary with the remaining fields optional.
I imagine XML is going to be the easiest way to define these options, but I have no idea how to go about creating my own XML. I've already written down the following but I need to know how to correctly define this as XML. E.g. Is there a namespace or something I need to add to the top of the file to create valid XML?
<images>
<image>
<id>1</id>
<source>images/slide1.jpg</source>
<alt>This is an image</alt>
<title>caption1</title>
<href>http://url.com</href>
<image>
<image>
<id>3</id>
<source>images/slide3.jpg</source>
<alt>This is an image</alt>
<title>caption3</title>
<href>http://url.com</href>
<caption>This is an example of a HTML caption.</caption>
<image>
<image>
<id>2</id>
<source>images/slide2.jpg</source>
<alt>This is an image</alt>
<title>caption2</title>
<href>http://url.com</href>
<image>
</images>
So thats potentially the XML defined to store the slider details.
Now I need to read the XML to generate the HTML:
All the images have to be stored between the <div id="slider"> images </div> and the captions have to be defined outside of this .
So I need to fetch the images first in the corret order based on the ID <id>. Something like (Not real code).
foreach image as images {
$id = <id>;
$src = <src>;
$title = <title>;
$alt = <alt>;
$href = <href>;
orderby $id;
<!--HERE I NEED TO DO SOME KIND OF if HERE SO THAT ONLY THOSE ELEMENTS
THAT ARE POPULATED IN THE XML ARE USED
-->
echo "<a href='$href'><img src='$src' alt='$alt' title='$title' /></a>";
}
The above would loop 3 times based on the above XML, and output the full HTML ready for a caption.
So to generate the captions I need to create this:
foreach images as image {
if $title = * {
echo "<div id='$title' class='nivo-html-caption'>
This is an example of a HTML caption.
</div>"
}
}
I beleive the Title give to the <img> is the relative to the ID given to the nivo-html-caption ID.
I realise I've written a lot here but just done have the understanding of XML to put the file together in a valid way.
How can I fetch the file using PHP?
How can I loop through the XML to generate the Images in HTML and put them in the correct order?
How can I generate the HTML captions seperatly?
Any help greatly appreciated!
Personally, I'd use JSON to store this information as its very simplified. Using php or perl, you just loop through the array, and this information can be pushed to the users browser if need be.
All of this can be found on David Powers's book OOP php, these examples are his in the book fyi:
How can I fetch the file using PHP?
$xml = simplexml_load_file($location);
How can I loop through the XML to generate the Images in HTML and put them in the correct order?
example of xml structure:
<book isbn13="978-1-43020-991-1">
<title>PHP Object Oriented Solutions</title>
<author>David Powers</author>
<publisher>friends of ED</publisher>
<description>Introduces the key concepts . . . </description>
</book>
ex:
$xml = simplexml_load_file('inventory.xml');
echo $xml->book[0]->title;
And it should show you, PHP Object Oriented Solutions
ex:
$xml = simplexml_load_file('inventory.xml');
foreach ($xml->book as $book) {
echo $book->title . '<br />';
}
This loops through each books if you have more than one .
How can I generate the HTML captions seperatly?
I assume you can have a separate foreach loop that is used for grabbing the captions from the xml.
Yeah, I think this is enough for you to google tutorial on simplexml functions and stuff. Good luck.
Oh, it can make your xml file simpler, less level of nested tags, by embedding html into the xml:
<url> <![CDATA[ URL ]]> </url>
So with that perhaps it'll make parsing your xml file a lot easier.

Categories