SCRIPTING:

Andreea Blanke
THE PHP 5 MASS THUMBNAILS CLASS: RESIZE, ENLARGE, SHRINK, LEAVE UNMODIFIED LARGE AMOUNTS OF IMAGES
This is much more than just thumbnails. This class gets a folder or an array of images as its input, scans them also accordingly to specified extensions if required, and resizes the images.
You can specify either a mere number or a number that represents a percentage: by that number either the width or height of the image gets resized, and the other size gets resized accordingly.
You can decide whether to resize all the images or only the ones that are wider than the specified number/percentage (shrinking), or only the ones which are smaller (enlarging), or just them all regardless of other considerations. You then output to specified folders.
October 2004
{ @ }

The model above is Andreea Blanke
Visit Miss Andreea Blanke's Website
LOADS OF ANDREEA BLANKE ON THE NET


PURPOSE OF THE SCRIPT
What the script does and why it is better than a few others

There is too much literature in the world: if we should really trudge through it all, we would all overflow and burst into madness with the meanings and the beauty that overwhelm us, like Saul Bellow's Herzog, and we would start writing letters to herr Nietzsche.
So beautiful we just can't cope.

The reason I devised this script is better explained by the following example:once a friend who wanted a website asked for my help, and it was going to be mostly a website of pics of his marriage. He provided me with two cds full of photos he took at his honeymoon.
The problem with these photos was that they were all incredibly wide, so resizing them was unescapable a necessity; and resizing them all one by one, would have likely taken over two hours - this in the best case.

Lacking an adequate software I looked for, I decided then to write myself my own PHP utility that could resize en masse images from a driver, and that could discriminate whether it had to resize after a percentage or after a fixed specified length (if that measure was meant to be applied onto the image width or onto the image height had to be specified too); the script had to be "smart" enough to accept further data, namely whether all the images had to be resized or only those that met certain characteristics, like most significantly those that were either bigger or smaller (as far as their width or height was concerned) that the specified width or height.

Then the output should have been, ideally, able to create three folders, one for the unmodified copies, one for the shrunk images, one for the enlarged ones - or alternatively one single folder for them all.
A further option would have been, of course, to create a set of <IMG> tags ready to be printed.
This latter thing was anyway the less important - which sets a significant difference with other scripts for whereas many scripts are concerned with making cute thumbnails (which often are not even such but mere IMG tags with width and height readapted, whereas what I needed was the modification of the physical files), here we do something more dramatic within whose scope thumbnails production falls inertially as a byproduct: that is, here we use Php as a software that manipulates and relocates images en masse .

And yes, I had an argument online also for this: wondering on a group whether anybody was aware of any built in PHP routine that could resize and copy a file with one routine only (which does not exist, by the way: but asking is legitimate) they found the question very "amusing" because "php was primarily conceived to write web pages not to manipulate images".
But what Php was "primarily" is not what we use Php for any longer, but it was precisely the reason why we did use it not: in fact "primarily" PHP was also a non Object Oriented Language; but now it even allows for Object Oriented Features like the methods __set and __get and __call and __construct that even traditional Object Oriented Languages did not allow for!
So the guy could spend his time in a more profitable way at "amusing" his wife, at which I am positive he excels.
For, alright: here we manipulate images. With Php, with good peace of all the white collars of programming that are so unimaginative when at coding that they worship whatever tradition has been bequeathed to them simply because they can't imagine a tradition of their own.
J.D. Salinger and the little Holden Caufield were absolutely right:
«Never tell anybody anything».
With this script shrinking consumes less time than enlarging, but on my Pentium resizing as many as 400 images took less than 30 seconds. If you have thus 5000 images you can make in about 3 minutes the job that manually would have took hours.

Of course, the script required also simplicity when invoking it: I wanted to be able to do that all with a couple of initialization statements, and passing a few arguments to specify exactly which options I wanted to implement for the current session.

This script requires PHP 5 and above.
This script does not affect the original images: it yields copies.


THE CODES
Your codes

First your class code, later on the description of how to initialize it and of its public methods and the arguments they take.
The name of the class is thumbs though as said it is not limited to producing thumbnails, actually they weren't even my problem when I devised this script, and yet I decided for this name simply because it is popular enough and still describes one of the possible features the script can implement.

Donald Rumsfeld in a Richard Avedon photo
Donald Rumsfeld in a Richard Avedon photo, I find this image a fascinating mix of the Spoon River Anthology and of White Collars by Richard Wright Mills: it seems to express sort of an underlying unbalance between what is and what could be: is the scope inadequate for the greatness of the man, or is the man inadequate for the greatness of the scope? What is that doesn't fit?
That seems the message, and it yields no answer, and that's the beauty with it.

lines (with comments and blank lines):
Ezra Pound  in a Richard Avedon photo
Ezra Pound in a beautiful Richard Avedon photo


METHODS OVERVIEW
How you can use this script, how to initalize it, its methods and arguments

The initialization and usage of an instance of the class is rather simple: it can be achieved with 2 statements, whereas the only thing you must pay attention to is to pass arguments to the constructor upon initialization. Though the script has default values, yet the knowledge of the argumenrts is for this script more critical than ever, and the simplicity of the two statements you have to use is a bit balanced by the necessity to pass the arguments in a wise way.

I'll document here only the public methods, though the class strongly relies on private methods, many of them which, actually, could be used also outside the class: in fact, for instance, a couple of them are the methods named scanDirectory and findFilesr that I developed in the file Unfold Matrix and Folders, Find Files with Php, which was a Php dramatic evolution from their analogous Javascript Unfold Irregular Matrixes scripts.
As usual I'll use as variable name for a class instance the usual $foo, which could be replaced with whatever name of your choice.

Method:
thumbs

This is the constructor, that is:
$foo = new thumbs();
Here are its arguments which it is very important to know and pass to it:
  1. $newSize: defaults to 100.
    This is the number that represents the new dimension. Whether this will be applied to the width or height of the image, you'll specify with the next parameters.
    Whether it is a percentage and not a new measurement in pixels, you will flag with the next arguments too: as a percentage, it could be meant to enlarge or shrink and thus also this too can be specified with the next arguments: in fact a measure in pixels is not relative: if I say 100 pixels and the image is 200, it can be only shrunk, or if it is 80 it can be meant only to be enlarged. But if it is meant to be a percentage, then a further specification must be passed: for what do you mean by say 20%: 20% more or 20% less than the current size?
  2. $isWidth: defaults to 1.
    If passed as 1 it means that the new dimension must be compared against the width of the image. If passed as zero, against its height.
    True, it is possible to imagine a situation where you want to shrink or enlarge only after having assessed certain measures on both width and height. I do not exclude I will add a method for this in the future, but this class is already over 400 lines of code as it is.
  3. $isPercent: defaults to 0.
    It accepts thee possible values:
    • zero: flags that the passed new dimension (first argument) is not to be meant as a percentage.
    • Number 1: flags it is a percentage and means: enlarge.
    • Number -1 (note the minus sign): flags it is a percentage and means: shrink.
  4. $resamplingMode: defaults to 0.
    Accepts these values:
    • 0: resample all images anyway.
    • 1: resample images only if the compared width or height (depending on the second argument) is bigger: that is, shrink but do not enlarge.
    • 2: resample images only if the compared width or height (depending on the second argument) is smaller: that is, enlarge but do not shrink.
  5. $storingMode: defaults to 0.
    Accepts these values:
    • 0: save all the newly generated file in one single folder.
    • 1: save all the newly generated file in one single folder, but within that folder make three subfolders (and store new images accordingly) named:
      • unmodified
      • shrunk
      • enlarged
      If these folders do not exist, they get created.
  6. $destinationFolder: defaults to 'C:/temp/'.
    This is obviously the folder where you want the new images being dispatched. Arrange accordingly. If the folder does not exist, it gets created.
  7. $systemSeparator: defaults to "/".
    Please if you're on Windows, pass it as forward slash: that's better. Change accordingly if your operative system does not accept slashes as folder path separators.
Thus a nearly complete instance of an initialization may look like:
$foo=new thumbs(50,1,-1, 0,0);
Which means: resize to 50 checking against the widths. 50 is a percentage, and meant to reduce (-1).
Resample all images and store them in one single folder (without passing the folder path, the default C:/temp/ applies).

a Keith Carter photo
a Keith Carter photo


Method:
run

After initialized, you want to run your script. But of course you have to specify for each run on which files: so the arguments of this method are as follows:
  1. $filesOrLocation: defaults to empty string.
    It can be either an array whose each entry is the String of the path to the image file (but not a path to a whole folder), or it can be a single String in which case it is the String path of an entire folder where, arguably, your to-be-resized images reside.
    If your idea is to recursively scan more folders, nest the method in a loop yourself.
  2. $extensionRegExp: defaults to "/.jpg|.jpeg/".
    If passed as zero, the method will attempt to grab all the passed files and resize them. If passed as a regular expression, it must list the file extensions you want to check for, inclusive of the leading dot, separated by a pipe char namely |, and with no withe spaces interposed: the method will then attempt to grab only files with those extensions in their file names.
  3. $scanLimit: defaults to 1.
    I suggest to leave this unmodified. If you pass it as zero, not only the passed folder (if any) will be scanned but also all the subfolders will be all scanned and manipulated.
So, for instance :
$foo=new thumbs(50,1,-1, 0,0);

$foo->run('c:/myPath/myImages/folder1/', '.gif');


a Henri Cartier Bresson photo
a Henri Cartier Bresson photo


Method:
tags

This is an optional method. It can do two things: store in the class property named tags (a class method name and a class property are discriminated by PHP also if they carry the same name) an array whose each entry is a fully tailored IMG tag with the values of the resized files as the method run has stored them in some other (private) class properties. Or it can store and print the tags too.
It gest these arguments:
  1. $filesOrLocation: defaults to 'C:/temp/'
    This must be the folder where the actual images are.
  2. $extensionRegExp: defaults to '/.jpg|.jpeg/'.
    The same already said for the argument with this very same name in the method run, applies here too.
  3. $justPrint_isJoiner: defaults to empty string.
    If you pass it as something else, the method will rejoin all the produced image tags (which span through an array) by this joiner.
  4. $prependAsPath: defaults to an empty string.
    If passed, to the image tag SRC property will be prepended what is passed here. Optional.
  5. $preImage: defaults to an empty string. Is prepended before each image.
  6. $postImage: defaults to an empty string. Is appended after each image.
  7. $appendToTag: defaults to 'class="myImages"' and is a string you can append inside each IMG tag.
  8. $scanLimit defaults to 1 and the same as already said for the argument which carries the same name in the method run applies.
    This happens because the method tags scans again the folder passed as first argument because the assumption is: once resized your images, you might have moved them to another folder before choosing to print them.
  9. Instance:
    $foo=new thumbs(50,1,-1, 0,0);

    $foo->run( 'C:/path/images', 0 );

    $foo->tags('C:/temp/', 0, "\n<br>\n", 'C:/temp/', 'pre image: ', ' post image');


    That prints something like this (the images have not been really resized, this is an example for illustration purposes):



    Or:
    pre image: post image
    pre image: post image
    pre image: post image
    pre image: post image
    pre image: post image
    pre image: post image
    pre image: post image
    pre image: post image
    pre image: post image
    That's all.

a Henri Cartier Bresson photo
a Henri Cartier Bresson photo