]> dev.renevier.net Git - syp.git/blobdiff - inc/utils.php
create thumbnails of photos
[syp.git] / inc / utils.php
index cee9d9a2a2d7e26650f144e351a214366f7396fc..12c85c673dfd321a9b1d1a955103aa93f5895d2c 100644 (file)
@@ -4,7 +4,16 @@
 
 require_once ("inc/settings.php");
 
-function gethost() {
+function getthumbsdir () {
+    if (THUMBSDIR) {
+        return rtrim (THUMBSDIR, "/");
+    } else {
+        $uploaddir = rtrim (UPLOADDIR, "/");
+        return $uploaddir . "/" . "_thumbs";
+    }
+}
+
+function gethost () {
     $host = $_SERVER ["HTTP_HOST"];
     $colpos = strpos ($host, ':');
     // some web clients add port informations in Host header
@@ -14,6 +23,11 @@ function gethost() {
     return $host;
 }
 
+function ext_safe ($path) {
+    $basename = basename_safe ($path);
+    return end (explode (".", $basename)); 
+}
+
 function basename_safe ($path) {
     return end (explode ("/", $path)); 
 }
@@ -29,13 +43,18 @@ function unquote($gpc_str) {
    }
 }
 
-function full_url_from_imgpath ($filename) {
+function thumb_url_from_imgpath ($filename) {
+    if (defined ("THUMBSDIRURL") && (strlen (THUMBSDIRURL) != 0)) {
+        return rtrim (THUMBSDIRURL, '/') . "/_mini" . rawurlencode ($filename);
+    }
+    return full_url_from_path (getthumbsdir () . "/mini_" . rawurlencode ($filename));
+}
+
+function image_url_from_imgpath ($filename) {
     if (defined ("IMGSDIRURL") && (strlen (IMGSDIRURL) != 0)) {
         return rtrim (IMGSDIRURL, '/') . "/" . rawurlencode ($filename);
     }
 
-    $rel_path = UPLOADDIR . "/" . rawurlencode ($filename);
-
     return full_url_from_path (UPLOADDIR . "/" . rawurlencode ($filename));
 }
 
@@ -81,4 +100,101 @@ function full_url_from_path ($path) {
 
     return "$proto://$host$port$path";
 }
+
+function create_thumbnail ($filename, $destfile) {
+    $ext = strtolower (ext_safe ($filename));
+    if ($ext == "jpg" || $ext == "jpeg") {
+        $image = imagecreatefromjpeg ($filename);
+    } else if ($ext == "png") {
+        $image = imagecreatefrompng ($filename);
+    } else {
+        return false;
+    }
+
+    if ($image === false) {
+        return false;
+    }
+
+    if (defined (THUMBSMAXSIZE) && (THUMBSMAXSIZE > 0)) {
+        $thumbsmaxsize = THUMBSMAXSIZE;
+    } else {
+        $thumbsmaxsize = 400; // default value;
+    }
+
+    $width = imageSX ($image);
+    $height = imageSY ($image);
+    if (($width  <= $thumbsmaxsize) || ($height <= $thumbsmaxsize)) {
+        return false;
+    }
+
+    if ($width > $height) {
+        $thumb_width = $thumbsmaxsize;
+        $thumb_height = $height * ($thumbsmaxsize / $width);
+    } else if ($width  < $height) {
+        $thumb_width = $width * ($thumbsmaxsize / $height);
+        $thumb_height = $thumbsmaxsize;
+    } else if ($width  == $height) {
+        $thumb_width = $thumbsmaxsize;
+        $thumb_height = $thumbsmaxsize;
+    }
+
+    $thumb_image = ImageCreateTrueColor ($thumb_width, $thumb_height);
+    if ($thumb_image === false) {
+        return false;
+    }
+    if (!imagecopyresampled ($thumb_image, $image, 0, 0, 0, 0,
+                        $thumb_width, $thumb_height, $width, $height)) {
+        return false;
+    }
+
+    if ($ext == "jpg" || $ext == "jpeg") {
+        if (!imagejpeg ($thumb_image, $destfile, 100)) {
+            return false;
+        }
+    } else if ($ext == "png") {
+        if (!imagepng ($thumb_image, $destfile)) {
+            return false;
+        }
+    }
+
+    imagedestroy ($image); 
+    imagedestroy ($thumb_image); 
+
+    return true;
+}
+
+function safe_create_dir ($dirname) {
+    if (is_dir ($dirname)) {
+        return;
+    }
+    if (file_exists ($dirname)) {
+        par_error ($dirname . ": " . trans ('exist but is not a directory'));
+    }
+    if (!mkdir ($dirname)) {
+        par_error ($dirname . ": " . trans ('could not create directory'));
+    } else {
+        par_success ($dirname . ": " . trans ('directory created'));
+    }
+}
+
+function safe_create_writable_dir ($dirname) {
+    safe_create_dir ($dirname);
+    if (!is_writeable ($dirname) || !is_executable ($dirname)) {
+        par_error ($dirname . ": " . trans ('could not write in directory'));
+    }
+}
+
+function leave () {
+    exit ("\n</body></html>");
+}
+function par_success ($message) {
+    printf ("<p class=\"success center\">%s</p>", $message);
+}
+function par_error ($message) {
+    printf ("<p class=\"error center\">%s</p>", $message);
+    leave ();
+}
+function par_warn ($message) {
+    printf ("<p class=\"warn center\">%s</p>", $message);
+}
 ?>