Handling unicode and missing link card images in php2Bluesky

I have just committed a couple of changes to my php2Bluesky library. This release includes a simple change to enable the support of international characters in hashtags and a more complex (to explain) change to the options for link cards.

International support

This was a change to the regex used in the library to identify the start and end points of hashtags. The previous code used /#(w+)/ which matches only Alphanumeric characters: [A-Za-z0-9] and an underscore _. This meant that hastags such as #Gütersloh failed to be recognised correctly. Now the regex is /#([\p{L}\p{N}]{1,})/ this now supports:

  • Latin letters (a-z, A-Z)
  • Accented letters (é, ñ)
  • Letters from other scripts (e.g., Cyrillic, Arabic, Chinese, etc.)
  • Digits (0-9)
  • Numbers in other scripts (e.g., Arabic-Indic digits).

I can take zero credit for this as all the work has been done by @asjmcguire.bsky.social.

Image options in Link Cards

Link cards when attached to a post include a snippet of the article that you are linking to and an image from that page. The title, description and image are taken from the open graph tags which are embedded into a post. Here the the tags for this post:

The question is what happens when your link doesn’t have any image associated with it?

Setting the image for a link card

As of the last release you were able to specify your own image for a link card which would be used irrespective of the open graph image tag. For example you could do the following:

$response = post_to_bluesky($connection, $text, $image, $link, '');

Here $image could be either a local image held on the file system, a URL to a static image or to a service that serves up a random image of the right size, such as https://picsum.photos/1024/536.

Thanks go to @bearlydoug.com for the above.

Fallback on no image

The issue with this is that you have to know that you are going to want to change the link card image in advance and if you are automating posts that might not be ideal. I have now added a change that allows you to set the fallback position for links that don’t have an open graph image set.

At the top of functions.php there is a new constant:

// what happens when there is no link card image (RANDOM, BLANK or ERROR)
const linkCardFallback = 'BLANK';

This takes one of three options:

  • RANDOM – use a random image from https://picsum.photos/1024/536
  • BLANK – insert a blank image (see below) – this is the default
  • ERROR – end the process with an error message – nothing will be posted.

In the same folder as functions.php, there is now a file called blank.png which is, as the name suggests, a completely white image of the right dimensions. You could, of course, change this with an image of your own choosing, just make sure it is 1024×536 in size and is called blank.png.

Hopefully, that should give more options to those that want to use link cards.

As always you can get the latest code from here.

Leave a Reply

Your email address will not be published. Required fields are marked *