HiveBrain v1.2.0
Get Started
← Back to all entries
snippetphpMinor

PHP code to scale images and generate thumbnails

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thumbnailsphpandgeneratecodeimagesscale

Problem

I have written a piece of code by taking help from this link. For generating thumbnail, I create a temporary scaled image and then copy from that image. I am assuming that it is resource intensive.

Settings: the maximum values for target dimensions of scale images are predefined. Also, the thumbnail is always square and its side's value is also predefined.

The logic for my code is:
Maximum Target dimensions - 700X524
Thumbnail - 90X90

  • Get source image dimensions



  • Check if either of height or width is larger than 524 & 700 respectively


If yes,

  • Check if height> width.If yes,set height=524 and scaled width;else set width=700 and scale height.



  • create true color image from imagecreatettruecolor.



  • Generate scaled image with new dimensions from the original image



For thumbnail:

  • Check if either of scaled height and width is greater than 90



  • If yes, scale the shorter side to 90 and longer side accordingly.



  • Create a true color canvas of the further scaled dimensions.



  • Create a short image with the shorter side being 90 and larger one scaled accordingly



  • Create a true color canvas of dimensions 90X90



  • Copy thumbnail image to this canvas from the recently created shorter scaled image.



The code:

```
524 || $source_imagex > 700) {
if($source_imagey>$source_imagex){
$new_height = 524;
$new_width = 524/$source_imagey*$source_imagex;
}else{
$new_width = 700;
$new_height = 700/$source_imagex*$source_imagey;
}
}

$dest_image = imagecreatetruecolor($new_width, $new_height);

//poor quality but fast
imagecopyresized($dest_image, $source_image, 0, 0, 0, 0, $new_width,
$new_height, $source_imagex, $source_imagey);
imagejpeg($dest_image,"final.jpg", 80);

//better quality but slow
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $new_width,
$new_height, $source_imagex, $source_imagey);
imagejpeg($dest_image, 'final2.jpg', 80);

//create square thumbnail
// resize image to scale shorter side to 9

Solution

Please don't count this as an answer. I've never done anything with any of the image libraries before, so I can't help you with the specifics. I would have put this into a comment, but its obviously too big. However, here are a few things to ponder.

First, in regards to the comments Keiran Lovett made, a class may make this a little simpler to work with, but it may also add an unnecessary level of overhead or abstraction. This seems like a relatively simple application, thus the overhead and abstraction that a class would introduce may not be worth it. It depends on a number of things, but the main ones I would contemplate are if I were going to be doing a lot of different things with these images, or how much simpler or faster/slower it would make it. Either way, I definitely would start using functions, and then if you decide to use a class it will make the transition easier.

Your maximum width and height should be made into constants, or at the very least variables. This avoids the "magic numbers" and makes it easier to use and update these values should you ever decide to change them.

define( 'MAX_Y', 524 );
define( 'MAX_X', 700 );


How do you treat images that have been rotated? An image flipped to a different aspect ratio would result in a smaller image on one axis and a skewed thumbnail. Or at least, so it would seem to me. I'm not sure how to do this, but it is something you may want to look in to.

And finally, in regards to how memory intensive this is going to be, I don't know. The best way to find out would be to run some tests and profile it. Create 100 (or more) oversize images and run the necessary code in a loop to convert each one. This is where having functions would be handy.

$start = microtime( TRUE );
for( $i = 0; $i < 100; $i++ ) {
    //functions to scale and convert
}
$total = microtime( TRUE ) - $start;

Code Snippets

define( 'MAX_Y', 524 );
define( 'MAX_X', 700 );
$start = microtime( TRUE );
for( $i = 0; $i < 100; $i++ ) {
    //functions to scale and convert
}
$total = microtime( TRUE ) - $start;

Context

StackExchange Code Review Q#15470, answer score: 3

Revisions (0)

No revisions yet.