-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbnails.php
33 lines (28 loc) · 1.11 KB
/
thumbnails.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
#--THUMBNAIL CREATION CODE--runs if thumbnail does not exist (consider cronjob)
#TODO test type of image
function generateThumbnail($photo, $photosDir, $thumbnailsDir, $thumbnailWidth, $thumbnailHeight, $thumbnailRatio) {
if (!file_exists($thumbnailsDir."/".$photo)) {
list($width,$height) = getimagesize($photosDir."/".$photo);
$aspectRatio = $width/$height;
$thumb = imagecreatetruecolor($thumbnailWidth,$thumbnailHeight);
$src_x = 0;
$src_y = 0;
$src_w = $width;
$src_h = $height;
if ($aspectRatio < $thumbnailRatio) {
//Take all width but only some of height
$src_h = intval($width/$thumbnailRatio);
$src_y = intval(($height-$src_h)/2);
} elseif ($aspectRatio > $thumbnailRatio) {
//Take all height but only some of width
$src_w = intval($height*$thumbnailRatio);
$src_x = intval(($width-$src_w)/2);
}
$image = imagecreatefromjpeg($photosDir."/".$photo);
imagecopyresampled($thumb, $image, 0, 0, $src_x, $src_y, $thumbnailWidth, $thumbnailHeight, $src_w, $src_h);
imagejpeg($thumb, $thumbnailsDir."/".$photo);
imagedestroy($thumb);
}
}
?>