Tagged: php

A UTF-8 Aware substr_replace (for use in App.net)

So, I stayed up bashing my head against a brick wall all last night! PHP's string functions aren't (yet) UTF-8 aware.

This is a replacement for subtr_replace which should work on UTF-8 Strings:

  1. function utf8_substr_replace($original, $replacement, $position, $length)
  2. {
  3.  $startString = mb_substr($original, 0, $position, "UTF-8");
  4.  $endString = mb_substr($original, $position + $length, mb_strlen($original), "UTF-8");
  5.  
  6.  $out = $startString . $replacement . $endString;
  7.  
  8.  return $out;
  9. }

Take this typical string from App.net

» Hello @bob how are you?

According to App.net's entities, @bob occurs at position 9 and has length of 3.

Normally, we would just use substr_replace.

However, PHP will count any unicode character like "»" as two characters. So it thinks that the position of @bob is 10.

Arse.

So, given we have the position of the substring, and its length, we can use PHP's multibyte functions to split the string in two.

First,

  1. $startString = mb_substr($originalString, 0, $position, "UTF-8");

Gives us:

» Hello @

Secondly,

  1. $endString = mb_substr($originalString, $position + $length, mb_strlen($originalString), "UTF-8");

Gives us

 how are you?

Finally, we stitch them back together

  1. $newString = $startString . $replacement . $endString;

Introducing a NEW QR Generator

When people ask me which QR generator to use, I usually suggest Google Charts. However, recently I've become dissatisfied with its limitations, so I've decided to write and release my own QR encoder.

I'm still looking for a catchy name for it (suggestions welcomed) - so for now it's called "QR Generator PHP".

It's available on GitHub or you can use it directly.

So, how does it compare to Google Charts?

Feature New QR Encoder Google Charts
Image Formats PNG, JPG, GIF PNG
Maximum Image Size 1480*1480px 547*547px
Unicode Support Yes No
Downloadable Images Yes- to a specific filename No
Open Source Yes No
Run on your own webserver? Yes No

Usage

Usage is really simple.

To generate a QR code which says "hello":

qr.php?d=hello

(Click to see the QR code)

You can set the size with, oddly enough, the size parameter:

qr.php?d=hello&size=1000

Size can be up to 1480 pixels.

You can set the image format to JPG or GIF. By default it outputs PNG.

qr.php?d=hello&t=J

The Error Correction can be set to L, M, Q, or H.

qr.php?d=hello&e=H

You can also tell the web browser to download the image - rather than just display it. The "download" parameter sets the filename for the image.

qr.php?d=hello&t=g&download=testing

The file will be called testing.gif (filetype is determined automatically)

Installation and configuration

Installing the software on your web server is easy. You need PHP4.1 or higher and gd 1.6 or higher. Those are fairly old versions, so any competent web host will have those.

There are three folders.

 |_php
 |_data
 |_image

DO NOT TOUCH THE CONTENTS OF THE data AND image FOLDERS.

Inside the php folder, you'll find the "qr.php" file.

There are only two things you need to configure - the location of the data and image folders

  1. $path = "./../data"; // You must set path to data files.
  2. $image_path = "./../image"; // You must set path to QRcode frame images.

By default, they're set up to be accessed without any modification. But, if you desperately want to move them to a different location, make sure you update qr.php.

Copyright

I've based my QR generator on that of Swetake. The original was licensed as "revised BSD" - I have kept the original licence.

Feedback

If you've got any feedback - either leave it in the comments here, or over at GitHub.

Calibre PHP Patches

Two quick patches which should be in the next version of Calibre PHP.

Adding File Size

This shows the sizes of the eBook files.

Screenshot shows a demonstration using the free "Hacking The BBC" eBook.
Continue reading

Installing Calibre PHP

(These are mostly notes to myself!)

I love Calibre, it's the perfect eBook management tool. It comes with a built in WWW server so you can easily access your library on the go. The only problem is that this really only works if you have a single machine dedicated to Calibre. For various reasons, I don't have a single machine.

I have a desktop, laptop, and server. The Calibre Library is just a database with a set of files and folders - so all three machines sync via DropBox. As long as I don't have the Calibre program open on my desktop and laptop at the same time, everything is hunky-dory.

However, having Calibre running on the server buggers everything up. So, I'm using a separate program - Calibre-PHP Content Server.

Installation was relatively simple, assuming you've already got apache and PHP installed, you'll also need GD, SQLite3, and Smarty.

This was how I installed them on Ubuntu

sudo apt-get install smarty php5-gd php5-sqlite php5-sqlite3sqlite3 

Configuration is slightly confusing. Copy the config_default.php to config_local.php and edit it.

$config['library_dir'] = '/data/Dropbox/eBooks/Calibre Library';
$config['smarty_dir'] = '/data/smarty-cache';
$config['smarty'] = '/usr/share/php/smarty';

The library directory must be readable to the webserver (chmod a+r) and all the directories above it have to be searchable (chmod a+x).

The "smarty_dir" is where the cache is stored. It needs two sub-folders, smarty_cache and smarty_templates_c. Both of these directories must be writable by the webserver.

That should be it. There's help available at the official help thread.

Displaying Twitter Photos via Entities

Twitter has announced that it will soon open up a native photo sharing service.

Rather than using an external service like Embed.ly to retrieve thumbnails, all the data is embedded within Twitter Entities.

So, if you request a status using "include_entities=true", you will be able to grab the image and display the thumbnail using the following code.

  1. function twitter_get_media($status) {
  2.    if($status->entities->media) {
  3.  
  4.       $url = $status->entities->media[0]->media_url_https;
  5.  
  6.       $width = $status->entities->media[0]->sizes->thumb->w;
  7.       $height = $status->entities->media[0]->sizes->thumb->h;
  8.  
  9.       $media_html = "<a href=\"" . $url . "\" target='_blank'>";
  10.       $media_html .=  "<img src=\"" . $url . ":thumb\" width=\"" . $width .
  11.          "\" height=\"" . $height . "\" />";
  12.       $media_html .= "</a><br />";
  13.      
  14.       return $media_html;
  15.    }        
  16. }

So, a tweet like this:


Will render like this (in Dabr):
Twitter Dabr Images

Notes

This is very rough and ready proof of concept code. Beware of the following:

  • This will only take the first image from the tweet.
  • Only images are supported - I'm not sure how their proposed video sharing will work.
  • There's no error checking.
  • In the above code, the https URL is used - if you want a non-SSL link, you'll need to remove the "_https"

Enjoy!