1 (edited by Daf 2014-10-28 15:42:35)

Topic: Facebook/Share - current image shows

Is there a simple way to share/facebook the current image being viewed?

Specifically using the Facebook/Share icons ?


Was possible in Simpleviwer but only with some coding/tweaks:
- Like buttons in seperate floating/absolute DIV
- Use the API alter source of FAcebook like iFrame so that the Facebook like link would update for each image viewed
- Use Direct linking to show the specific image when a use clicked on the facebook link
- Use server side code to alter the og:image when FAcebook would call for one.

I imagine this would be possible with Juicebox too.


However I would like it to be cleaner, using the inbuilt share button - Is there an API/method where I can alter the "shareURL" when each image is viewed ?

Re: Facebook/Share - current image shows

Unfortunately, there is no easy way to do this. Facebook no longer allows a specific image URL to be passed via their share URL.
The image displayed in the Facebook share window is defined only by the value of the og:image meta tag.
(There is also no way to change the data passed in the share URL. This is hard-coded into the 'juicebox.js' file which is obfuscated and cannot be modified.)
The link being shared will point towards the current image in the gallery but the thumbnail displayed in the Facebook share window will be the og:image.

You may be able to achieve your goal using the technique you outlined above but it might be quite complex and is not guaranteed to work.

3 (edited by Daf 2014-10-29 00:30:11)

Re: Facebook/Share - current image shows

There is also no way to change the data passed in the share URL. This is hard-coded into the 'juicebox.js' file which is obfuscated and cannot be modified.

Ah shame / Thanks.

The method I mentioned works with Simpleviewer so guess it would with Juicebox too.
(Not purchased Juicebox yet so can't try)

For those that might read this  - the majority is as per simpleviewer facebook forum examples:
http://www.simpleviewer.net/forum/viewt … p?id=15213
http://www.simpleviewer.net/forum/viewt … p?id=13243

- Set up API call to call local Javascript (svImageChange) think on Juicebox this would be "onImageChange"
- This function:
1. Sets index after the hash
2. Adds an ID parameter to the temporary url plus hash (since server side code can't read hash value)
3. Sets the source of the facebook iFrame using this temp url

        function svImageChange(index) {

            //set  hash for direct link on current url
            document.location = "#" + (index + 1);

            //get iFrame
            var original = document.getElementById("fb").src;
            
            //set ID + hash for like url
            var newurl = document.location.toString().substr(0, (document.location.toString()).lastIndexOf("/")+1);
            newurl = newurl + "?ID=" + (index+1) + "#"+(index+1);
            
            var modified = original.replace(/\?href=(.*?)&/, "?href=" + encodeURIComponent(newurl) +"&");
            document.getElementById("fb").src = modified;
        };

- So facebook like/share iframe source is something like http://domain.com/GalleryPage?ID=12#12
- This way Simpleviewer/client-side can read the index after the hash - and set the correct image
- Server side can read the index in"ID=" parameter, then
- Parse the XML - since my filenames are not the index to find the filename
- Set the og:image in server side code before rendering thepage, so when FAcebook reads the url - it gets the correct thumbnail

There is of course a slight cost to this since the Facebook iFrame refreshes each time you view a different image.
There was another issue which I can't recall now.

4 (edited by Daf 2014-10-29 00:17:59)

Re: Facebook/Share - current image shows

Ah - extra issue would be if someone copied the URL to post in Facebook.
The URL in browser only has hash
e.g.
http://domain.com/GalleryPage#12

Not the verion with ID for setting og:image
http://domain.com/GalleryPage?ID=12#12

On another version I did do some Javascript to check for the presence of this ID, if not there would redirect to include it. Not the nicest of solutions.

window.onload = function()
{
  var hashval= window.location.hash;
  
  if(hashval.length>0){
  
    if(!isNaN(hashval.substring(1, hashval.length)))
    {
        window.location.replace(window.location.protocol + "//" + window.location.host + window.location.pathname + "?ID=" + hashval.substring(1, hashval.length));
     }
  }
}

Re: Facebook/Share - current image shows

Just bought juicebox and coming back to this.
Sorry if there are newer threads - but this came when I searched.

So is there any way of altering og:image with Facebook share link ?
Thought worth asking in case Juicebox has an API now.

e.g. method described above:
- Instead of a share link : index.html#34
it does a link something like:
- index.html?Image=34
or
-index.html?Image=filename

This would mean back-end code could read the image info and alter the og:image tag appropriately.

Re: Facebook/Share - current image shows

I guess it may be possible to do in a similar way to Simpleviewer method - i.e. create an independent set of share icons - using the API to update these links ?

But altering the inbuilt share icons would be good/nicer.

Re: Facebook/Share - current image shows

So is there any way of altering og:image with Facebook share link ?

As far as I am aware, it is not possible to set or modify the thumbnail used in the share window (or specifically the og:image) via the Facebook share link and changing the og:image dynamically via JavaScript (for example, when onImageChange() is fired) will not work. It is possible to set multiple og:image meta tags in a web page but I see no way to inform Facebook which one to use at any specific time.
As far as I can tell, the only way to have the thumbnail image correspond to the actual image being shared might be to have the share links point towards individual web pages (one per gallery image), each with a unique og:image. However, Juicebox does not work this way (each gallery image does not have its own web page) and the 'juicebox.js' file cannot be edited.
Unfortunately, I do not see a way that this can be done (at least not easily within the current parameters of Juicebox-Pro and Facebook).
Maybe other users will have some ideas or suggestions...

8 (edited by Daf 2015-08-20 10:20:39)

Re: Facebook/Share - current image shows

Ok thanks.

I'm guessing there may be alternative methods, two I have in mind:

Seperate/custom share links
- Not show the Juicebox share buttons.
- create my own share link in a floating div, which points to a page with parameter that the code behind can read and adjust og:image appropraietly - these are updated via Juicebox API : onImageChange(e) . I did something similar to this in simpleviewer


Replace Juicebox click function
- A Call from onImageChange(e) calls jQuery $('.jg-bb-button.jb-bb-brn-facebook').unbind('click');
- Add my own click event - linking to page with parameter

Not sure if this will work - but will give it a go.

Re: Facebook/Share - current image shows

Your two suggestions seem to be based around dynamically creating a new page for each image with the required og:image tag. This might work to ensure that the corresponding thumbnail is displayed each time an image in the gallery is shared but then the page that is shared will no longer be the page containing the Juicebox gallery. It will be your custom page with the required og:image. I'm not sure if this is a drawback or something that is intended. (Maybe you could redirect back to the gallery page from the page containing the og:image.) Just thinking out loud...

Re: Facebook/Share - current image shows

@Daf you are on the right track, with a little bit of back end coding you can certainly do it - here is the proof of concept

example1 http://www.marcosven.com/galleries/left_thumbs example2 http://www.marcosven.com/galleries/strip_view

I just created these galleries and sometimes it takes a while for facebook to index the images, so it might not work on the first or second try.

11 (edited by Daf 2015-08-24 00:10:08)

Re: Facebook/Share - current image shows

Managed it - editing the juicebox social links rather than having a seperate social div.

Some code - c#/ASP.net

Page code-behind

        protected string ogimage = "";
        protected string ogurl = "";
        protected string imageurl = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            //remove query
            Uri path = new Uri(Request.Url.GetLeftPart(UriPartial.Path));
            //remove file
            string pathfolder = path.AbsoluteUri.Remove(path.AbsoluteUri.Length - path.Segments.Last().Length);
            
            if (pathfolder.Substring(pathfolder.Length - 1, 1) == "?")
            { pathfolder = pathfolder.Substring(0, pathfolder.Length - 1); }

            ogurl = "<meta property=\"og:url\" content=\"" + Request.Url + "\" />";
    
            if(Request.QueryString["Img"] != null)
            {
                ogimage = "<meta property=\"og:image\" content=\"" + pathfolder + "images/" + Request.QueryString["Img"] + ".jpg\" />";
                imageurl = pathfolder + "images/" + Request.QueryString["Img"] + ".jpg";
            }
            else
            {
                ogimage = "<meta property=\"og:image\" content=\"" + pathfolder + "images/_DAF1470.jpg\" />";
                imageurl = pathfolder + "images/_DAF1470.jpg";
              }

        }

Open-graphic section of header


<meta property="og:title" content="Nest 2015" />
<%=ogurl %>
<%=ogimage %>
<meta property="og:image:type" content="image/jpeg" />

Config XML

I have added the filename into the caption of the images.
I don't show captions so this was doable.
I do this so that I can easily specify what image to display.
If not doing this I'd need to reply on the ID - and then need to parse the XML to find the filename.



JQuery/Javascript to alter the social icon click events


   

  <script type="text/javascript">

            //create a gallery instance
            var jb = new juicebox({
                containerid: 'juicebox-container'
            });

            

            var FBhandler = function (e,url) {
                var fbShareWindow = 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(url);
                window.open(fbShareWindow,"_blank", 'height=450, width=550, top=' + ($(window).height() / 2 - 275) + ', left=' + ($(window).width() / 2 - 225) + ', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0');
                return false;
            };

            var Twitterhandler = function (e, url) {
                var twitShareWindow = 'https://twitter.com/intent/tweet?text=' + encodeURIComponent(document.title)
                                       + "&url=" +  encodeURIComponent(url);
                window.open(twitShareWindow, "_blank", 'height=450, width=550, top=' + ($(window).height() / 2 - 275) + ', left=' + ($(window).width() / 2 - 225) + ', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0');
                return false;
            };

            var Gplushandler = function (e, url) {
                var gpShareWindow = 'https://plus.google.com/share?url=' + encodeURIComponent(url);
                window.open(gpShareWindow, "_blank", 'height=450, width=550, top=' + ($(window).height() / 2 - 275) + ', left=' + ($(window).width() / 2 - 225) + ', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0');
                return false;
            };

            var Tumblrhandler = function (e, url) {
                var tumblrShareWindow = 'https://www.tumblr.com/widgets/share/tool/preview?shareSource=legacy&canonicalUrl=&url=' + encodeURIComponent(url)
                                       + "&posttype=photo"
                                        + "&content=<%=imageurl%>"
                                        + "&caption=" + encodeURIComponent(document.title)
                                        + "&clickthroughUrl=";
                window.open(tumblrShareWindow, "_blank", 'height=450, width=550, top=' + ($(window).height() / 2 - 275) + ', left=' + ($(window).width() / 2 - 225) + ', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0');
                return false;
            };

            //then set up an event listener
            jb.onImageChange = function (e) {
                //unbind
                $(".jb-bb-btn-facebook").unbind("click");
                $(".jb-bb-btn-twitter").unbind("click");
                $(".jb-bb-btn-gplus").unbind("click");
                $(".jb-bb-btn-tumblr").unbind("click");

                //bind
                var url = window.location.href.split('?')[0].split('#')[0] + "?Img=" + e.caption + "#" + e.id
                $(".jb-bb-btn-facebook").bind("click", function () { FBhandler(e, url); });

                var url = window.location.href.split('?')[0].split('#')[0] + "?Img=" + e.caption + "#" + e.id
                $(".jb-bb-btn-twitter").bind("click", function () { Twitterhandler(e, url); });

                var url = window.location.href.split('?')[0].split('#')[0] + "?Img=" + e.caption + "#" + e.id
                $(".jb-bb-btn-gplus").bind("click", function () { Gplushandler(e, url); });

                var url = window.location.href.split('?')[0].split('#')[0] + "?Img=" + e.caption + "#" + e.id
                $(".jb-bb-btn-tumblr").bind("click", function () { Tumblrhandler(e, url); });
            };



    </script>

12

Re: Facebook/Share - current image shows

Will likely need to tweak it to incorporate URL rewrites/FriendlyURL

13

Re: Facebook/Share - current image shows

Facebook share behaviour is inconsistent i.e. the selected photo doesn't always get reaped by FB.
I think this is more a Facebook issue than something with my code.
Not sure if it's something that I can influence or not.

e.g. could be difference between URLS:
/Default.aspx?Img=_DAF0389
or
/?Img=_DAF0389
and og:url value

Or possibly a FB cache issue.
G+ is consistent.