summaryrefslogtreecommitdiff
path: root/utils/thumbnail.php
blob: f1f4b388b000691cb836fc8d1d4b9027c22b3fb8 (plain)
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
34
35
36
37
38
39
<?php
function thumbnail($album, $file, $maxwidth, $maxheight) {
  global $ALBUMS_DIR;

	if($file =="") return "No such image";

  // Config
  $quality = 70;
  $width = $maxwidth;
  $height = $maxheight;

  // Filenames
  $thumbnaildir = $ALBUMS_DIR . "/" . $album . "/thumbnails/";
	$thumbnail = $thumbnaildir . $maxwidth . "x" . $maxheight . "_" . $file;
  $original = $ALBUMS_DIR . "/" . $album . "/" . $file;

  if(!file_exists($thumbnaildir)) {
    // The thumbnaildir doesn't exist, create it.
    mkdir($thumbnaildir, 0755);
  }

  // Create thumbnail
  if(!file_exists($thumbnail)) {
    list($width_orig, $height_orig) = getimagesize($original);
    if ($width && ($width_orig < $height_orig)) {
      $width = ($height / $height_orig) * $width_orig;
    } else {
      $height = ($width / $width_orig) * $height_orig;
    }
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($original);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    imagejpeg($image_p, $thumbnail, $quality);
  }
  
  // Return thumbnail filename
  return $thumbnail;
}
?>