<?php

ini_set('display_errors', 'On');

//echo '[]';
//echo '[' . empty($_GET['path']) . ']';
//echo '[' . !empty($_GET['path']) . ']';

//echo '[' . $_GET['path'] . ']';
if ( empty($_GET['path']) ) {
    echo 'error : no parameter';
    exit();
}
//echo 'yes!';
$image_path = '/var/www/images' . $_GET['path'];

$ip = $_SERVER['REMOTE_ADDR'];

//$image_path = "/var/www/image/notice001.jpg";
//$watermark = $_GET['text'];
$watermark  = 'www.maniadb.com : '. $ip;
if ( !file_exists($image_path) ) {
    echo 'error : file not found';
    exit();
//  $image_path = "/var/www/image/button.png";
//  $string = 'file not found';
}
//echo 'good!';

//;
// path validation check

$img_info = getimagesize($image_path);
$tmp = explode("/", $img_info['mime']);
$ext = $tmp[1];

//echo $ext;
//exit(1);

if($ext == "jpg" || $ext == "jpeg"){
    $image = @imagecreatefromjpeg($image_path);
}else if($ext == "png"){
    $image = @imagecreatefrompng($image_path);
}else if($ext == "bmp" || $ext == "wbmp"){
    $image = imagecreatefromwbmp($image_path);
}else if($ext == "gif"){
    $image = imagecreatefromgif($image_path);
}else {
    echo 'error : unknown file format';
    exit();
}

header("Content-type: image/png");

if ( imagesx($image) > 500 || imagesy($image) > 500 ) {
    $coloralpha   = imagecolorallocatealpha($image, 60, 87, 156, 100);
    //$coloralpha_c = imagecolorallocatealpha($image, 60, 87, 156, 110);
    //$coloralpha = imagecolorallocate($image, 60, 87, 156);
    //$px     = (imagesx($image) - 7.5 * strlen($watermark)) / 2;
    //echo imagesx($image)-(7.5 * strlen($watermark))-15 . '<br>';
    //echo imagesy($image)-20 . '<br>';
    

    imagestring($image, 4, 9, 9, $watermark, $coloralpha);
    //imagestring($image, 4, (imagesx($image) - 7.5 * strlen($watermark)) / 2, imagesy($image) / 2, $watermark, $coloralpha_c);
    imagestring($image, 4, (imagesx($image)-(8 * strlen($watermark))-15)/2, imagesy($image) / 2, $watermark, $coloralpha);
    imagestring($image, 4, imagesx($image)-(8 * strlen($watermark))-15, imagesy($image)-20, $watermark, $coloralpha);
}
imagepng($image);
imagedestroy($image);

?>
