Browse DevX
Sign up for e-mail newsletters from DevX


Build an Object-oriented File System in PHP : Page 4

Using a simple file storage convention and some PHP code, you can create a hierarchical file system that mimics many object-oriented concepts, facilitates reuse, and simplifies your Web development efforts.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Dealing with Images
Another PHP function lets you link to images as easily as pages, without having to care much about the exact file location. This is particularly helpful with templates, so you can easily inherit the look and feel of a parent directory, or override logos and graphics without changing the actual page layout. And as an added bonus, you get automatic width and height attributes, and even an automatic alt attribute if you forget to provide one.

So, rather than measuring the image size manually and writing a long HTML tag such as:

<img src="/mammals/bears/gfx/logo.jpg" width="400" height="100" alt="Logo" border="0" />

You can write this shorter version:

<? image("logo.jpg"); ?>

And in many cases, you won't need to write any code at all. Simply drop a file into the gfx/ subdirectory, and the image should show up on your page automatically. For example, if your site uses /gfx/logo.jpg for its logo, you could apply a different logo to the bears section by creating /bears/gfx/logo.jpg. Doing that would override the regular logo, and thus show up automatically without writing any special code.

It's also very common to use "thumbnail" images—small images that link to larger or full-sized images. Normally, using thumbnails is a fairly tedious task; you have to load an image in a photo-editing program, resize it, save it, put both copies onto your site, and write HTML code such as the following:

<a href="/path/to/big.jpg"> <img src="/path/to/small.jpg" width="200" height="150" alt="click for bigger image" border="0" /> </a>

A small PHP function similar to image() makes this process much easier. Simply put the original image on your site, and call a function:

<? thumbnail("big.jpg"); ?>

Doing this has the same effect as the longer method above, except that the Web server will create the thumbnail image automatically the first time it's requested. You can optionally pass numeric values to specify a maximum width or height for the thumbnail image.

The files inherit.inc and thumbnail.inc in the sample code implement the image() and thumbnail() functions, respectively. To use thumbnail(), make sure to give your Web server permission to write to the gfx/ directory; the server won't create thumbnail images without write permission. The thumbnail() function also requires the free "convert" utility from ImageMagick.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date