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:
<?php
// default dimensions
//height : 524px
//width: 700px
$source_image = imagecreatefromjpeg("bg.jpg");
$source_imagex = imageSX($source_image);
$source_imagey = imagesy($source_image);
//destination image size calculation, should not exceed the target
//check if height and width are larger than required and then scale
if($source_imagey>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 90px
if($new_width>90 || $new_height>90) {
if($new_height>$new_width)
{
$thumb_height = (90/$new_width)*$new_height;
$thumb_width = 90;
$top = ($tHeight - 90)/2;
$left = 0;
}else{
$thumb_width = (90/$new_height)*$new_width;
$thumb_height = 90;
$left = ($thumb_width-90)/2;
$top = 0;
}
}
$image_t = imagecreatetruecolor($thumb_width,$thumb_height);
imagecopyresampled($image_t, $dest_image, 0, 0, 0, 0, $thumb_width,
$thumb_height, $new_width, $new_height);
$thumb_image = imagecreatetruecolor(90,90);
imagecopy($thumb_image, $image_t, 0, 0, $left, $top,
$thumb_width,$thumb_height);
imagejpeg($thumb_image, 'thumb.jpg', 80);
imagedestroy($thumb_image);
imagedestroy($dest_image);
imagedestroy($image_t);
Is this approach of creating a temporary image of smaller size and then copying thumbnail from it, correct ? Is there an alternative to this ? How can it be further optimized ?
UPDATE: The class that I have created, with the above code and some help and insight gained from the link at nettuts, can be found on github