<?php
//================================================================================
// PLEASE DO NOT REMOVE THIS HEADER!!!
//
// COPYRIGHT NOTICE
// This script is licensed under the GPL
// 
// Copyright 2007-2008 Alias 454 Studios and Brandon Keep (c) All rights reserved.
// Created 11/18/2007   
// Brandon Keep,    http://www.openautoclassifieds.com
//                  http://www.alias454studios.com/scripts/
//
// Last Modified 04-24-2008 by
// Brandon Keep,    http://alias454studios.com
//================================================================================
// This software IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//================================================================================

// Get extension and return it
$file = $_GET["pic"];
//$width = $_GET["width"];
$ext_split = split("\.",$file);
$ext = $ext_split[1];
header("Content-type: image/$ext");

function openImage($file)
{
	// Get extension and return it
	$ext_split = split("\.",$file);
	$ext = $ext_split[1];
	switch($ext) {
		case 'jpg':
		case 'jpeg':
			$im = @imagecreatefromjpeg($file);
			break;
		case 'gif':
			$im = @imagecreatefromgif($file);
			break;
		case 'png':
			$im = @imagecreatefrompng($file);
			break;
		default:
			$im = false;
			break;
	}
	return $im;
}

	$im    = openImage($file);
    $old_x = imageSX($im);
    $old_y = imageSY($im);

    $new_w = (int)($width);

	if (($new_w <= 0) or ($new_w>$old_x)) {
		$new_w=$old_x;
    }

    $new_h = ($old_x*($new_w/$old_x));

    if ($old_x > $old_y) {
        $thumb_w = $new_w;
        $thumb_h = $old_y*($new_h/$old_x);
    }
    if ($old_x < $old_y) {
        $thumb_w = $old_x*($new_w/$old_y);
        $thumb_h = $new_h;
    }
    if ($old_x == $old_y) {
		$thumb_w = $new_w;
		$thumb_h = $new_h;
    }

	$thumb = ImageCreateTrueColor($thumb_w,$thumb_h);
	
	$watermark_img = openImage('images/watermark.png');
	$watermark_width = imageSX($watermark_img);
	$watermark_height = imageSY($watermark_img);
	
	$watermark = ImageCreateTrueColor($watermark_width,$watermark_height);
	imagecopyresampled($watermark,$watermark_img,0,0,0,0,$thumb_w,$thumb_h,$watermark_width,$watermark_height);

	$x = $thumb_w/2;  
	$y = $thumb_h/3; 

	imagecopymerge($im, $watermark, 0, 0, 0, 0, $thumb_w, $thumb_h, 30); 
	imagecopyresampled($thumb,$im,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

	//choose which image program to use
	if ($ext == 'jpeg' || 'jpg') {
		imagejpeg($thumb);
	} elseif ($ext == 'png') {
		imagepng($thumb);
	} elseif ($ext == 'gif') {
		imagegif($thumb);
	}
    imagedestroy($thumb);
?>