annoyingmouse's avatar

makeSquare listing

2 10 809 (1 Today)
By annoyingmouse   |   
A client required that thumbnails be generated and placed equidistant from the edges of a square of a given size, depending on the aspect ratio of the image - that is to say that if the image was a landscape then it should be the same width of the square while being placed in the vertical middle. After checking any number of resources I couldn't find an appropriate script so I rolled my own. I'm pretty pleased with it and you're more than welcome to have it from here.

You can view the script as a PDF here.

If you make alterations, and more importantly improvements, please do let me know.

Take it easy.
Image size
1275x2891px 1.75 MB
anonymous's avatar
Join the community to add your comment. Already a deviant? Log In
summaro's avatar
summaroProfessional Artist
Nice code, but you should really be filtering those $_GET calls for malicious code.
annoyingmouse's avatar
Thanks muchly, though the context for which is was written (a quick and dirty thing to generate thumbnails without having to wade through GIMP for each one) meant that it was run once then deleted.

Should I use it in anger then you are of course correct.
gemstones's avatar
Consider creating a method which will resize any gif, jpg, or png image and utilizing that here - that will give your code more power, since it can handle multiple filetypes, and also make it more reusable.
annoyingmouse's avatar
Cheers =genstones. I concentrated on jpegs as I'm working through the tutorials in .net.
TheZarel's avatar
Well, overall, it's a pretty good script, but since you encouraged advanced critique, there are a few things you might want to change.

First, your script crashes if it encounters a non-JPEG file. I never understood why you used substr($file, -3, 3)=="php" instead of substr($file, -4)==".jpg".

Second, don't use scandir(), it's PHP 5 only. opendir, readdir, closedir works just as well.

Third, using $_GET isn't a really good idea unless the script is protected elsewhere, since someone could DoS your server by running that script repeatedly once they found it.

Fourth, you should sanitize your input; make sure size is positive, make sure color is a color, etc.

Fifth, why didn't you combine statements? if ($file == ".") continue; if ($file == "..") continue; can easily be shortened to if ($file=='.'||$file=='..') continue;, which in turn can be shortened to if (substr($file,0,1)=='.') continue, since any UNIX file that starts with . should not be touched (for instance, .HTACCESS).

Here's my rewritten version of it.


It's kind of sparse on comments, so if you don't understand something, feel free to ask.

The improvements are:

- faster, smaller
- transparent backgrounds
- function interface
- different naming scheme
- works for all images, not just JPEGs
- doesn't crash on non-PHP non-JPEG files
- doesn't crash on invalid image files
Kroc's avatar
Very, very nice work. Good to see things like hexadecimal right shifts. My only comment is that `mkthumbdir` does not appear to return false, if `mkthumb` returns false. Unless I'm reading something wrong, `mkthumbdir` always returns true, and thus `mkthumbdir() or die('Error.';);` will never error?
TheZarel's avatar
mkthumbdir() does its sanity check at the beginning.

function mkthumbdir($dir='.',$size=128,$color='')
if ($color!=='' && (strlen($color)!=6 || !ctype_xdigit($color)) ||
intval($size)<=0) /* $size or $color is not valid */
return FALSE;

So it can return FALSE, but not on default settings.

So, basically, the only reason mkthumb() will return FALSE when called from within mkthumbdir() is if it calls mkthumb() on non-image files, in which case it will go on to the next file, and skipping non-image files in the specified directory instead of crashing is intended behavior.

And if it crashes, it's this line in mkthumbdir():

$files = @opendir($dir) or die('Cannot open current directory.');

Basically, the following line should never actually die, which is why it just says "Error" instead of giving a detailed report (in that it shouldn't ever happen):

mkthumbdir() or die('Error.');
anonymous's avatar
Join the community to add your comment. Already a deviant? Log In