<?php
//
//	file: includes/class_thumbnail.php
//	author: ptirhiik
//	begin: 05/03/2006
//	version: 1.6.0 - 10/06/2006
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
	exit;
}

define('MAX_THUMB_WIDTH', 400);

class thumbnail
{
	var $handler;
	var $min_filesize;
	var $imagick_pgm;
	var $user_gd2;

	function thumbnail(&$handler, $min_filesize=0, $imagick_pgm='', $use_gd2=true)
	{
		$this->handler = &$handler;
		$this->min_filesize = intval($min_filesize);
		$this->imagick_pgm = trim($imagick_pgm);
		$this->use_gd2 = intval($use_gd2);
	}

	function create($src_file, $dst_file)
	{
		// get the file location
		if ( $this->handler->method != 'ftp' )
		{
			$src_file = phpbb_realpath($src_file);
		}

		// ensure the size of the original pic
		$min_filesize = $this->min_filesize;
		$img_filesize = intval(@filesize($src_file));
		if ( !$img_filesize || ($img_filesize <= $min_filesize) )
		{
			return false;
		}
		list($width, $height, $image_type, ) = @getimagesize($src_file);
		if ( !$width || !$height )
		{
			return false;
		}

		// get the size of the thumbnail
		list($new_width, $new_height) = $this->get_img_size_format($width, $height);

		// no thumbnail needed
		if ( ($width <= $new_width) && ($height <= $new_height) )
		{
			return false;
		}

		// get a name
		$tmp_file = $this->handler->get_temporary_filename($src_file, $dst_file);

		// create a thumbnail
		if ( !$this->imagick_create($src_file, $tmp_file, $new_width, $new_height) && !$this->gd_create($src_file, $image_type, $width, $height, $tmp_file, $new_width, $new_height) )
		{
			return false;
		}

		// move it in place
		$this->handler->open_channel();
		if ( $dst_file != $tmp_file )
		{
			$result = $this->handler->upload($tmp_file, 'image', $dst_file);
			$this->handler->unlink_temporary($tmp_file);
		}
		else
		{
			$result = true;
			$this->handler->chmod($dst_file);
		}
		$this->handler->close_channel();

		return $result;
	}

	function get_img_size_format($width, $height)
	{
		return $width > $height ? array(MAX_THUMB_WIDTH, round(($height * MAX_THUMB_WIDTH) / $width)) : array(round(($width * MAX_THUMB_WIDTH) / $height), MAX_THUMB_WIDTH);
	}

	function imagick_create($src_file, $dst_file, $new_width, $new_height)
	{
		if ( empty($this->imagick_pgm) )
		{
			return false;
		}
		passthru($this->imagick_pgm . ' -quality 85 -antialias -sample ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $src_file) . '" +profile "*" "' . str_replace('\\', '/', $dst_file) . '"');
		return @file_exists($dst_file);
	}

	function gd_create($src_file, $image_type, $width, $height, $dst_file, $new_width, $new_height)
	{
		$type = $this->gd_supported_image_types($image_type);
		if ( !$type['gd'] )
		{
			return false;
		}

		// read the source image
		// this will work as the image is always local, even with ftp activated : it stands in the tmp dir in this case
		switch ( $type['format'] )
		{
			case IMG_GIF:
				$image = imagecreatefromgif($src_file);
				break;
			case IMG_JPG:
				$image = imagecreatefromjpeg($src_file);
				break;
			case IMG_PNG:
				$image = imagecreatefrompng($src_file);
				break;
			case IMG_WBMP:
				$image = imagecreatefromwbmp($src_file);
				break;
		}

		// resize
		if ( ($type['version'] == 1) || !$this->use_gd2 )
		{
			$new_image = imagecreate($new_width, $new_height);
			imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		}
		else
		{
			$new_image = imagecreatetruecolor($new_width, $new_height);
			imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		}

		// save the new pic in the tmp file
		switch ( $type['format'] )
		{
			case IMG_GIF:
				imagegif($new_image, $dst_file);
				break;
			case IMG_JPG:
				imagejpeg($new_image, $dst_file, 90);
				break;
			case IMG_PNG:
				imagepng($new_image, $dst_file);
				break;
			case IMG_WBMP:
				imagewbmp($new_image, $dst_file);
				break;
		}

		// free memory and return
		imagedestroy($new_image);
		return @file_exists($dst_file);
	}

	function gd_supported_image_types($image_type)
	{
		if ( @extension_loaded('gd') && @extension_loaded('gd2') )
		{
			return array('gd' => false);
		}

		$format = function_exists('imagetypes') ? @imagetypes() : 0;
		switch ( $image_type )
		{
			case 2:
			case 9:
			case 10:
			case 11:
			case 12:
				$new_type = ($format & IMG_JPG) ? IMG_JPG : 0;
				break;
			case 6:
			case 15:
				$new_type = ($format & IMG_WBMP) ? IMG_WBMP : 0;
				break;
			case 1:
				$new_type = ($format & IMG_GIF) ? IMG_GIF : 0;
				break;
			case 3:
				$new_type = ($format & IMG_PNG) ? IMG_PNG : 0;
				break;
			default:
				$new_type = 0;
				break;
		}
		return array(
			'gd' => $new_type ? true : false,
			'format' => $new_type,
			'version' => function_exists('imagecreatetruecolor') ? 2 : 1,
		);
	}
}

?>