## III. Using NBBC

[ Previous: G. Supporting Wiki-Links | Next: I. Parsing/Validating HTML and Other Tricks ]

### H. Supporting Local Images

The BBCode `[img]` tag has been used since BBCode's inception to support insertion of images into a document, and NBBC supports this as well. Typically, these images are located on another server, though, and a full URL is used with the `[img]` tag: For example, it's not unusual to see something like this in BBCode:

Code:

    [img]http://img.photobucket.com/albums/v63/phantom-inker/other/forum-mod.png[/img]

Output:

Many of the pictures that get included, however, all come from the same web site, be it a remote image host like Photobucket, or the local host on which NBBC itself is to be running. Because of this, NBBC offers a special syntax that makes inclusion of images from the same location much easier. This can be used to simplify URLs, or it is more frequently used to offer a local group of images that your users can easily link to.

Let's look at this with an example. Let's say that you have a server named `http://www.example.com`, and you are using NBBC here: `http://www.example.com/foo/bar/nbbc.php`. You have also installed a simple image-gallery package, so that people can upload their images to your server, and it will store its images in `http://www.example.com/foo/images`. Now if a user wants to include an image named "`bill.jpg`" that he's just uploaded, normally, he'd have to type this:

Code:

    [img]http://www.example.com/foo/images/bill.jpg[/img]

That's pretty excessive, though, especially since most of the images that he and other users of this site will be located in that same `images/` directory. So NBBC provides its local images system to simplify exactly this common case. In your PHP code, you might first write this:

Code:

    $bbcode = new BBCode;
    $bbcode->SetLocalImgURL("http://www.example.com/foo/images/");
    $bbcode->SetLocalImgDir("/home/example.com/foo/images/");

(Note that the local image directory passed to `SetLocalImgDir()` must be the full server path to the local image directory; and the address given to `SetLocalImgURL()` should match that location, only as a global URL.)

Once you've set this up, that user can now much more easily include any file in that directory:

Code:

    [img]bill.jpg[/img]

So any path given to the `[img]` tag that does not include a protocol (such as `http:`, `ftp:`, or `https:`), and that does not start with a `\` or a `/` will be treated as a path to a local image, an image somewhere under the current local image directory. NBBC will sensibly only include images that are actual images, and for security reasons, it will also disallow the use of `..` in a filename.

[ Previous: G. Supporting Wiki-Links | Next: I. Parsing/Validating HTML and Other Tricks ]

Copyright © 2010, the Phantom Inker. All rights reserved.