<? 
#####################################################
#####################################################
##                                                 ##
##        Image script by Laurens A. Laman.        ##
##      This script loads a picture and puts a     ##
##    Watermark onto it. It also resizes it if     ##
##    you like. It displays the picture on your    ##
##  screen so you can put it in a <img src=> tag   ##
##                                                 ##
##           Script tested on PHP 4.4.1            ##
##                                                 ##
#####################################################
#####################################################

##################################################################
##################################################################
##                                                              ##
##                        How to use it?                        ##
##                                                              ##
##           This is very simple: just upload this script       ##
##  Then access it like <img src="Image.php?image=./test.jpg">  ##
##                  Or if you like to resize:                   ##
##      <img src="Image.php?image=./test.jpg&resize=Yes">       ##
##                                                              ##
##################################################################
##################################################################


###################################################
###################################################
##                                               ##
##                 Let's START:                  ##
##                                               ##
###################################################
###################################################


###################################################
###################################################
##                                               ##
##               Modify Variables:               ##
##                                               ##
###################################################
###################################################
$nieuwWidth = "150"; //--- The width of the picture to be resized. The height will be calculated automatically
$errpict    = "../jeugd/img/notfound.gif"; //--- The path of the picture if something goes wrong
$errtext    = "Picture not found"; //--- The error message if $_GET['image']; is empty
$watermark  = "../jeugd/img/watermerk2.gif"; //--- The location of the default watermark (horizontal)
$Watervert  = "../jeugd/img/watermerkvert.gif";//--- The location of the vertical watermark
$trans      = "30"; //-- Transparent in percent of the watermark (0-100)
###########################################################
###########################################################
##                                                       ##
## DO NOT MODIFY BENEATH UNLESS YOU KNOW WHAT YOUR DOING ##
##                                                       ##
###########################################################
###########################################################
//--- check if var 'resize' is empty else resize the image
If (!$_GET['resize']){
//--- check if var 'image' has been passed in the URL 
if ($_GET['image']) 
{ 
    //--- Content type
    header('content-type: image/jpeg'); 

    //--- set the path 
    $pict = $_GET['image'];
    
    

    //--- watermark 
    $path_watermark = $watermark; 
    $watermark = imagecreatefromgif($path_watermark); 
    $watermark_width = imagesx($watermark); 
    $watermark_height = imagesy($watermark); 

    //--- check that file exists
    if(!file_exists($pict)) {
    $pict=$errpict;
    }
    

    //--- image 
            $ext=explode('.',$pict);
            $ext_check=count($ext);
            $ext=$ext[count($ext)-1];
            $ext = strtolower($ext);
    //--- use the proper imagecreate
    if     ($ext=="jpg"){$image = imagecreatefromjpeg($pict)or die('Cannot Create image');}
    elseif ($ext=="gif"){$image = imagecreatefromgif($pict)or die('Cannot Create image');}
    elseif ($ext=="png"){$image = imagecreatefrompng($pict)or die('Cannot Create image');}
    elseif ($ext=="bmp"){$image = imagecreatefrombmp($pict)or die('Cannot Create image');}
    else                {$image = imagecreatefromgif($errpict)or die('Cannot Create image');}//--- if file is not a picture then show error
    
    
    
    list($width, $height) = getimagesize($pict);
    $left = $width - $watermark_width; 
    $top = $height - $watermark_height;    

    //--- merge watermark + image 
    if ($width > $watermark_width){
    imagecopymerge($image, $watermark, $left, $top, 0, 0, $watermark_width, $watermark_height, $trans); 
   }else{
    //--- Vertical watermark if the width of the image is too small
   $watermark = imagecreatefromgif($Watervert); 
    $watermark_width = imagesx($watermark); 
    $watermark_height = imagesy($watermark); 
    $left = $width - $watermark_width; 
    $top = "0" ;
    if ($height > $watermark_height){//--- and if the image is too small for both watermarks, then show nothing
    imagecopymerge($image, $watermark, $left, $top, 0, 0, $watermark_width, $watermark_height, $trans); 
    }
   }

    //--- print image 
    imagejpeg($image); 

    //--- destroy image + watermark 
    imagedestroy($image); 
    imagedestroy($watermark); 
} 
else {//--- Show error if var 'image' is empty

$text =  $errtext;
   $width = strlen($text) * 8.3;
    $height = 16;
    $font = 4;
$txtimg = imagecreate($width, $height);
imagecolorallocate($txtimg, 255, 0, 0);
$txtcolor = imagecolorallocate($txtimg, 255,255,255);
imagestring($txtimg, $font, 0, 0, $text, $txtcolor)or die ("Cannot Create image");
    
    //--- Content type
    header("Content-Type: image/jpeg");  
    //-- show img
    imagejpeg($txtimg);  
    //-- destroy img
    imagedestroy($txtimg);  

}
}else{
if($_GET['image'])
{ 
//--- File and new size
$filename = $_GET['image'];

//--- check that file exists 
    if(!file_exists($filename)) {$filename=$errpict;}




//--- Content type
header('Content-type: image/jpeg');

//--- Get new sizes
list($width, $height) = getimagesize($filename);
$percent = $height / $width;
$new_width = "150";
$new_height = "150" * $percent;
if ($newheight > "150"){
$percent = $width / $height;
$new_height = "150";
$new_width = "150" * $percent;
}

//--- Load
$thumb = imagecreatetruecolor($new_width, $new_height);


            $ext=explode('.',$filename);
            $ext_check=count($ext);
            $ext=$ext[count($ext)-1];
            $ext = strtolower($ext);
    //--- use the proper imagecreate
    if     ($ext=="jpg"){$source = imagecreatefromjpeg($filename)or die('Cannot Create image'); }
    elseif ($ext=="gif"){$source = imagecreatefromgif($filename)or die('Cannot Create image'); }
    elseif ($ext=="png"){$source = imagecreatefrompng($filename)or die('Cannot Create image'); }
    elseif ($ext=="bmp"){$source = imagecreatefrombmp($filename)or die('Cannot Create image'); }
    else{                $source = imagecreatefromgif($errpict)or die('Cannot Create image');}//--- if file is not a picture then show error


//--- Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);


    //--- add watermark
    $path_watermark = $watermark;
    $watermark = imagecreatefromgif($path_watermark); 
    $watermark_width = imagesx($watermark); 
    $watermark_height = imagesy($watermark); 
    $left = $new_width - $watermark_width; 
    $top = $new_height - $watermark_height;
   if ($new_width > $watermark_width){
    imagecopymerge($thumb, $watermark, $left, $top, 0, 0, $watermark_width, $watermark_height, $trans); 
   }else{
    //--- Vertical watermark if the width of the image is too small
    $watermark = imagecreatefromgif($Watervert); 
    $watermark_width = imagesx($watermark); 
    $watermark_height = imagesy($watermark); 
    $left = ($new_width - $watermark_width); 
    $top = "0" ;
    if ($new_height > $watermark_height){//--- and if the image is too small for both watermarks, then show nothing
    imagecopymerge($thumb, $watermark, $left, $top, 0, 0, $watermark_width, $watermark_height, $trans); 
    }
   }
//--- Output
imagejpeg($thumb);

}
else
{//--- Show error if var 'image' is empty
$text =  $errtext;
   $width = strlen($text) * 8.3;
    $height = 16;
    $font = 4;
$txtimg = imagecreate($width, $height);
imagecolorallocate($txtimg, 255, 0, 0);
$txtcolor = imagecolorallocate($txtimg, 255,255,255);
imagestring($txtimg, $font, 0, 0, $text, $txtcolor)or die ("Cannot Create image");
    
    //--- Content type
    header("Content-Type: image/jpeg");  
    //-- show img
    imagejpeg($txtimg);  
    //-- destroy img
    imagedestroy($txtimg);  
}

}//--- end resize
?> 