<?php

        function file_type($file){
                $path_chunks = explode("/", $file);
                $thefile     = $path_chunks[count($path_chunks) - 1];
                $dotpos      = strrpos($thefile, ".");
                return strtolower(substr($thefile, $dotpos + 1));
        }

                $path         = "/var/www/Modules/".$HTTP_GET_VARS["pth"]."/";

                if (file_exists($path.$HTTP_GET_VARS["fname"]."_Small.jpg")) {
                  $filename     = $HTTP_GET_VARS["fname"]."_Small.jpg";
                } else {
                  $filename = "nopicture2.jpg";
                  $path     = "/var/www/Web/PIC/";
                }
        //      $thumbnail    = thumbnail_name($filename);
                $extension    = file_type($filename);
                $remove_thumb = false;
                $img_size     = array();
                $width          = "150";
                $height         = "150";

  if (!file_exists("/tmp/".$filename)) {

                if(in_array($extension, array('png', 'gif', 'jpg', 'jpeg'))){
                        if(!$img_size = getimagesize($path.$filename)){
                                $remove_thumb = true;
                        }
                        if($extension == 'gif'){
                                if(!$image = imagecreatefromgif($path.$filename)){
                                        $remove_thumb = true;
                                }
                        }elseif($extension == 'png'){
                                if(!$image = imagecreatefrompng($path.$filename)){
                                        $remove_thumb = true;
                                }
                        }elseif($extension == 'jpg' || $extension == 'jpeg'){
                                if(!$image = imagecreatefromjpeg($path.$filename)){
                                        $remove_thumb = true;
                                }
                        }
                        $img_height = $img_size['1'];
                        $img_width  = $img_size['0'];
                        if($img_width > $img_height){
                                $thumb_width  = $width;
                                $thumb_height = ($img_height)*($height/$img_width);
                        }elseif($img_width < $img_height) {
                                $thumb_width  = ($img_width)*($width/$img_height);
                                $thumb_height = $height;
                        }elseif($img_height == $img_width) {
                                $thumb_width  = $width;
                                $thumb_height = $height;
                        }
                        if($remove_thumb == false){
                                $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
                                imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $img_size[0], $img_size[1] );
                                $black = ImageColorAllocate($thumb, 0, 0, 0);
                                ImageString ($thumb, 3, $thumb_width-120, $thumb_height-10, "www.mlab.cz", $black);
                                imagejpeg($thumb, "/tmp/".$filename);

                        }       
                }else{
                        $remove_thumb = true;
                }

  }


header("Content-type: image/jpeg");
readfile("/tmp/".$filename);
?>