That looks all right.
I have a couple of thoughts though.
First, your if statements, they are all checking the same variables, so it would be far more efficient to use "if..else if..else" rather than three separate if statements.
For example:
-
// ----
-
// Scenario one, three if statements
-
if($x > $y) { // Even if this is true.. }
-
if($x < $y) { // this one will also be checked }
-
if($x == $y) { // as will this }
-
-
// ----
-
// Scenario two, if.. else if
-
if($x > $y) { // If this is true...}
-
else if($x < $y) { // this will be skipped! }
-
else { // As will this! }
-
-
/* ----
-
So using scenario two; two out of three boolean expressions will not be executed, most of the time, giving a slight performance boost
-
(not notable in this example but... still)
-
*/
-
Second. You use the function
imagecopyresized() which, granted, will do exactly what you want.
But, if you replace it with the
imagecopyresampled function, your image will not only be resized, it will also be re sampled, increasing its quality.
Finally. You import both the new width and the new height, scale the image down and then center it on a canvas that may not be close to the original image aspect ratio.
Have you considered creating a thumb using the original aspect ratio and then use HTML or CSS to position it when you output it?
That way there will be no white background in the thumb itself, which will allow you much more freedom when you display it.
Imagine if you decide, 100.000 images from now, that you want to change the background color of your thumbs. Your way, it would not be possible without re-creating every single thumb. This way it would be as simple as changing a single CSS value.