Kyberia v2.3 - 1st revision from SVN (Without patches of kyberia.sk team)
[mirrors/Kyberia-bloodline.git] / inc / eventz / configure_image.inc
index 92cfcb26f4f1636eeb217978943519b288e97500..a4fe18ce39ec27505aa84c9303ac69b18dc9372e 100644 (file)
@@ -1,65 +1,94 @@
 <?php
-/* This program is free software. It comes without any warranty, to
- * the extent permitted by applicable law. You can redistribute it
- * and/or modify it under the terms of the Do What The Fuck You Want
- * To Public License, Version 2, as published by Sam Hocevar. See
- * http://sam.zoy.org/wtfpl/COPYING for more details. */
+    // FINAL, RUNNING ... id# 2105641
+    // Comments, requests and bugs na moj mail ;) dakujem.
 
+    function __cin_get_img_type(&$_file_i) {
+        list($_mime_type, $_mime_subtype) = explode('/', $_file_i['type']);
+        if ($_mime_type != 'image') return (false);
 
-       function configure_image() {
+        $_img_i = getimagesize($_file_i['tmp_name']);
+        switch ($_mime_subtype) {
+            case 'gif':  return (($_img_i[2] == 1)?'gif':false); break;
+            case 'jpeg': return (($_img_i[2] == 2)?'jpg':false); break;
+            case 'png':  return (($_img_i[2] == 3)?'png':false); break;
 
-               global $db,$error,$node;
-               $user_id=$_SESSION['user_id'];
+            default: return (false); break;
+        }
+    }
 
-               if ($node['node_permission']=='owner') {
+    function __cin_create_tmp_image($_src_f, $_dest_f, $_img_t, $_img_w) {
 
-               if (!empty($_FILES['description_image']['tmp_name'])) {
-                       $image_tmp=$_FILES['description_image']['tmp_name'];
-                       $image_name=$_FILES['description_image']['name'];
+        switch ($_img_t) {
+            case 'gif': $_img_c_func = 'imagecreatefromgif'; break;
+            case 'jpg': $_img_c_func = 'imagecreatefromjpeg'; break;
+            case 'png': $_img_c_func = 'imagecreatefrompng'; break;
 
-                       if ($node['node_id']==$user_id) $width=50;
-                       else $width=NODE_IMAGE_WIDTH;
+            default: return (false); break;
+        }
 
-                        // mifo:
+        $_src_i  = $_img_c_func($_src_f);
+        $_src_w  = imagesx($_src_i);
+        $_src_h  = imagesy($_src_i);
 
-                        $output_image = "images/nodes/".substr($node['node_id'],0,1)."/".substr($node['node_id'],1,1)."/".$node['node_id'].".gif";
+        $_dest_w = floor($_src_w / ($_src_w / min($_src_w, $_img_w)));
+        $_dest_h = floor($_src_h / ($_src_w / min($_src_w, $_img_w)));
 
-                       if (stristr($image_name,".jpg") || stristr($image_name,".jpeg") || stristr($image_name,".gif")){
-                                // mifo:
-                                // $cmd=UTILZ_DIR."/jpegtopnm  $image |".UTILZ_DIR."/pnmscale -width=$width | ".UTILZ_DIR."ppmquant 256 |".UTILZ_DIR."ppmtogif >".SYSTEM_ROOT."images/nodes/".substr($node['node_id'],0,1)."/".substr($node['node_id'],1,1)."/".$node['node_id'].".gif";
-                                // workaround by mifo:
-                                if (stristr($image_name,".jpg") || stristr($image_name,".jpeg"))
-                                    $orig = imagecreatefromjpeg($image_tmp);
+        $_dest_i = imagecreatetruecolor($_dest_w, $_dest_h);
 
-                                if (stristr($image_name,".gif"))
-                                     $orig = imagecreatefromgif($image_tmp);
+        imagecopyresampled($_dest_i, $_src_i, 0, 0, 0, 0, $_dest_w, $_dest_h, $_src_w, $_src_h);
 
-                                if ($orig)
-                                {
-                                    $orig_x = imagesx($orig);
-                                    $orig_y = imagesy($orig);
-//                                  if ($orig_x > $width)
-                                       $image_x = $width;
+        imagegif($_dest_i, $_dest_f);
+        chmod($_dest_f, 0664);
 
-                                    $image_y = round(($orig_y * $image_x) / $orig_x);
+        imagedestroy($src_i);
+        imagedestroy($dest_i);
 
-                                    $image = imagecreatetruecolor($image_x, $image_y);
-                                    imagecopyresampled($image, $orig, 0, 0, 0, 0, $image_x, $image_y, $orig_x, $orig_y);
-//                                     echo $output_image;
-                                    imagegif($image, $output_image);
-                                }
+        return (true);
+    }
 
+    function configure_image() {
+    global $error, $node;
 
+        if (empty($_FILES['description_image'])) {
+            $error = '#00: Invalid file supplied.';
+            return (false);
+        }
 
-                       }
-                       //if ($cmd) {
-                       //      shell_exec($cmd);
-                       //}
+        // if is owner, find out type of the image
+        if ($node['node_permission'] == 'owner') {
 
-               }
+            if (!($_img_t = __cin_get_img_type($_FILES['description_image']))) {
+                $error = '#01: Invalid file format (JPEG, GIF & PNG are only allowed)';
+                return (false);
+            }
 
-               }
-               return true;
-       }
+            $_WEBROOT_PATH     = realpath(dirname(__FILE__).'/../..');
+            $_PROCESS_IMG_PATH = $_WEBROOT_PATH.'/process-img';
 
-?>
+            $_required_dims    = ($node['node_id'] == $_SESSION['user_id'])?50:NODE_IMAGE_WIDTH;
+            $_prepared_fname   = sprintf('%s/prepared/%03s_%s.gif.upload', $_PROCESS_IMG_PATH, $_required_dims, $node['node_id']);
+            $_nodeimg_fname    = sprintf('%s/images/nodes/%s/%s/%d.gif', $_WEBROOT_PATH, $node['node_id'][0], $node['node_id'][1], $node['node_id']);
+
+            if (!is_uploaded_file($_FILES['description_image']['tmp_name'])) {
+                $error = '#02: Invalid file upload';
+                return (false);
+            }
+
+            if (!__cin_create_tmp_image($_FILES['description_image']['tmp_name'], $_nodeimg_fname, $_img_t, $_required_dims)) {
+                $error = '#03: Unable to create temporary thumbnail image!';
+                return (false);
+            }
+
+            if (!move_uploaded_file($_FILES['description_image']['tmp_name'], $_prepared_fname)) {
+                $error = '#04: Error moving uploaded file!';
+                return (false);
+            }
+
+            chmod($_prepared_fname, 0664);
+            rename($_prepared_fname, substr($_prepared_fname, 0, -7));
+
+        }
+
+        return (true);
+    }
+?>
\ No newline at end of file
This page took 0.160264 seconds and 4 git commands to generate.