[ABD] Resize sent avatars

Any abandoned MODs will be moved to this forum.

WARNING: MODs in this forum are not currently being supported or maintained by the original MOD author. Proceed at your own risk.
Forum rules
IMPORTANT: MOD Development Forum rules

WARNING: MODs in this forum are not currently being supported nor updated by the original MOD author. Proceed at your own risk.
Hołek
Registered User
Posts: 6
Joined: Wed Oct 14, 2009 5:52 pm

[ABD] Resize sent avatars

Post by Hołek » Fri Oct 16, 2009 11:29 pm

Modification name: Resize sent avatars
Author: Hołek
Modification description: Aren't you tired of phpBB giving you dimension errors every time you upload new avatar? Why can't it resize the avatar on its own? Now it can! As long as the filesize is not exceeded, phpBB will upload and resize the file successfully.
Modification version: 1.1.1

Requirements: ImageMagick (for transparency/animations), GD extension (as a fallback)
phpBB version: 3.0.5

There's only one file to edit, but this one edit improves usability of avatar changing greatly!

Version history:
  • 1.1.1 - 2009-10-17
    • fixed minor bug, causing wrong resizing method to be applied to pallette-based images (thanks to autinhyeu)
  • 1.1.0 - 2009-10-17
    • support for animated GIF files
    • support for transparent PNG/GIF files
  • 1.0.0 - 2009-10-16
    • initial version
Installation script:

Code: Select all

##############################################################
## MOD Title: Resize sent avatars
## MOD Author: Hołek < holek@toolserver.org > (Michał Połtyn) http://toolserver.org/~holek/
## MOD Description: Aren't you tired of phpBB giving you dimension errors every time
##                  you upload new avatar? Why can't it resize the avatar on its own?
##                  Now it can! As long as the filesize is not exceeded,
##                  phpBB will upload and resize the file successfully.
## MOD Version: 1.1.1
## 
## Installation Level: Easy
## Installation Time: 1 minute
## Files To Edit: includes/functions_upload.php
## Included Files: 
## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##############################################################
## MOD History:
## 
## 2009-10-18 - Version 1.1.1
##     - fixed minor bug, causing wrong resizing method
##       to be applied to pallette-based images
##       (thanks to autinhyeu)
## 2009-10-18 - Version 1.1.0
##     - support for animated GIFs
##     - using Imagemagick if available
## 2009-10-17 - Version 1.0.0
## 
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
##############################################################

#
#----[ OPEN ]-----------------------------------------------------------
#

includes/function_upload.php

#
#----[ FIND ]-----------------------------------------------------------
#

		if (!$this->upload->valid_dimensions($this))
		{
			$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_SIZE'], $this->upload->min_width, $this->upload->min_height, $this->upload->max_width, $this->upload->max_height, $this->width, $this->height);
			return false;
		}

		return true;
	}

#
#----[ REPLACE WITH ]---------------------------------------------------
#

		if (!$this->upload->valid_dimensions($this))
		{
			$valid = $this->create_thumb();
			
			if (!$valid)
			{
				$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_SIZE'], $this->upload->min_width, $this->upload->min_height, $this->upload->max_width, $this->upload->max_height, $this->width, $this->height);

				return false;
			}
		}

		return true;
	}

	/**
	* Create a thumb if uploaded image is too big.
	* This function was based mainly on MediaWiki's thumbnail creating process
	* and create_thumbnail function in functions_posting.php
	* @source MediaWiki
	*/
	function create_thumb()
	{
		global $config;

		if ($this->width > $this->height) 
		{
			$thumb_width = $this->upload->max_width;
			$thumb_height = $this->height*($this->upload->max_height/$this->width);
		}
		else if ($this->width < $this->height) 
		{
			$thumb_width = $this->width*($this->upload->max_width/$this->height);
			$thumb_height = $this->upload->max_height;
		}
		else /* $this->width == $this->height */
		{
			$thumb_width = $this->upload->max_width;
			$thumb_height = $this->upload->max_height;
		}

		// Only use imagemagick if defined and the passthru function not disabled
		if ($config['img_imagick'] && function_exists('passthru'))
		{
			$quality = '';
			$sharpen = '';
			$frame = '';
			$animation = '';
			if ( $this->mimetype == 'image/jpeg' )
			{
				$quality = '-quality 80'; // 80%
				/** Reduction in linear dimensions below which sharpening will be enabled */
				if ( ( $thumb_width + $thumb_height ) / ( $this->width + $this->height ) < 0.85 )
				{
					$sharpen = '-sharpen 0x0.4';
				}
			}
			elseif ($this->mimetype == 'image/png')
			{
				$quality = '-quality 95'; // zlib 9, adaptive filtering
			}
			elseif ($this->mimetype == 'image/gif')
			{
				/**
				 * Force thumbnailing of animated GIFs above this size to a single
				 * frame instead of an animated thumbnail. ImageMagick seems to
				 * get real unhappy and doesn't play well with resource limits. :P
				 * Defaulting to 1 megapixel (1000x1000)
				 */
				if($this->width * $this->height > 1.0e6)
				{
					// Extract initial frame only
					$frame = '[0]';
				}
				else
				{
					// Coalesce is needed to scale animated GIFs properly (MediaWiki bug 1017).
					$animation = ' -coalesce ';
				}
			}
			# Specify white background color, will be used for transparent images
			# in Internet Explorer/Windows instead of default black.

			# Note, we specify "-size {$this->width}" and NOT "-size {$this->width}x{$this->height}".
			# It seems that ImageMagick has a bug wherein it produces thumbnails of
			# the wrong size in the second case.

			if (substr($config['img_imagick'], -1) !== '/')
			{
				$config['img_imagick'] .= '/';
			}
			$cmd  = 
				escapeshellcmd($config['img_imagick']) . 'convert' . ((defined('PHP_OS') && preg_match('#^win#i', PHP_OS)) ? '.exe' : '') .
				" {$quality} -background white -size {$this->width} ".
				escapeshellarg($this->destination_file . $frame) .
				$animation .
				// For the -resize option a "!" is needed to force exact size,
				// or ImageMagick may decide your ratio is wrong and slice off
				// a pixel.
				' -thumbnail ' . escapeshellarg( "{$thumb_width}x{$thumb_height}!" ) .
				" -depth 8 $sharpen " .
				escapeshellarg($this->destination_file) . ' 2>&1';

			@passthru($cmd);

			// after converting let's check the file dimensions again
			if (($this->image_info = @getimagesize($this->destination_file)) !== false)
			{
				$this->width = $this->image_info[0]; // the _real_ width
				$this->height = $this->image_info[1]; // the _real_ height
				if ($this->upload->valid_dimensions($this)) 
				{
					return true;
				}
			}
		}
		if (extension_loaded('gd'))
		{
			/* This code is greatly based on MediaWiki's thumbnail generation process */
			$typemap = array(
				'image/gif'          => array( 'imagecreatefromgif',  'palette',   'imagegif'  ),
				'image/jpeg'         => array( 'imagecreatefromjpeg', 'truecolor', array( __CLASS__, 'imagejpegwrapper' ) ),
				'image/png'          => array( 'imagecreatefrompng',  'bits',      'imagepng'  ),
				'image/vnd.wap.wbmp' => array( 'imagecreatefromwbmp', 'palette',   'imagewbmp'  ),
				'image/xbm'          => array( 'imagecreatefromxbm',  'palette',   'imagexbm'  ),
			);
			if (!isset( $typemap[$this->mimetype] ))
			{
				return false;
			}

			list($loader, $color_style, $save_type) = $typemap[$this->mimetype];
		
			if (!function_exists($loader))
			{
				return false;
			}
			$src_image = call_user_func( $loader, $this->destination_file );
			$thumb = imagecreatetruecolor($thumb_width, $thumb_height);

			// Initialise the destination image to transparent instead of
			// the default solid black, to support PNG and GIF transparency nicely
			$background = imagecolorallocate( $thumb, 0, 0, 0 );
			imagecolortransparent( $thumb, $background );
			imagealphablending( $thumb, false );
		
			if( $color_style == 'palette' ) {
				// Don't resample for paletted GIF images.
				// It may just uglify them, and completely breaks transparency.
				imagecopyresized( $thumb, $src_image,
					0,0,0,0,
					$thumb_width, $thumb_height, $this->width, $this->height );
			} else {
				imagecopyresampled( $thumb, $src_image,
					0,0,0,0,
					$thumb_width, $thumb_height, $this->width, $this->height );
			}

			imagesavealpha( $thumb, true );

			call_user_func( $save_type, $thumb, $this->destination_file );
			imagedestroy($thumb);
			imagedestroy($src_image);
			$this->width = $thumb_width;
			$this->height = $thumb_height;
			return true;
		}
		return false;
	}

	static function imagejpegwrapper( $dst_image, $thumb_path ) {
		imageinterlace( $dst_image );
		imagejpeg( $dst_image, $thumb_path, 95 );
	}

#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM
That's all, folks!
Last edited by Hołek on Sat Oct 17, 2009 9:20 pm, edited 5 times in total.

DJ Skitzo
Registered User
Posts: 596
Joined: Sun Aug 12, 2007 7:25 pm

Re: [RC] Resize sent avatars

Post by DJ Skitzo » Sat Oct 17, 2009 3:19 am

Hello,

I haven't installed this MOD yet, but I have a question.

Does this MOD resize animated gif's & if so does it keep the animation?

I have tried other MOD's just like this but I haven't yet found one which correctly deals with animated gif's

User avatar
kber
Registered User
Posts: 986
Joined: Sun May 07, 2006 9:22 am
Location: Egypt
Contact:

Re: [RC] Resize sent avatars

Post by kber » Sat Oct 17, 2009 9:23 am

very cool :)
Hello All

Hołek
Registered User
Posts: 6
Joined: Wed Oct 14, 2009 5:52 pm

Re: [RC] Resize sent avatars

Post by Hołek » Sat Oct 17, 2009 10:04 am

DJ Skitzo wrote:Does this MOD resize animated gif's & if so does it keep the animation?
Not yet, but I believe I'll code it sometime today.

Hołek
Registered User
Posts: 6
Joined: Wed Oct 14, 2009 5:52 pm

Re: [RC] Resize sent avatars

Post by Hołek » Sat Oct 17, 2009 12:07 pm

And there you go. Now supporting transparent and animated files. Update script from 1.0.0 to 1.1.1:

Code: Select all

##############################################################
## MOD Title: Resize sent avatars (1.0.0 > 1.1.1 UPDATE script)
## MOD Author: Hołek < holek@toolserver.org > (Michał Połtyn) http://toolserver.org/~holek/
## MOD Description: Aren't you tired of phpBB giving you dimension errors every time
##                  you upload new avatar? Why can't it resize the avatar on its own?
##                  Now it can! As long as the filesize is not exceeded,
##                  phpBB will upload and resize the file successfully.
## MOD Version: 1.1.1
## 
## Installation Level: Easy
## Installation Time: 1 minute
## Files To Edit: includes/functions_upload.php
## Included Files: 
## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##############################################################
## MOD History:
## 
## 2009-10-18 - Version 1.1.1
##     - fixed minor bug, causing wrong resizing method
##       to be applied to pallette-based images
##       (thanks to autinhyeu)
## 2009-10-18 - Version 1.1.0
##     - support for animated GIFs
##     - using Imagemagick if available
## 2009-10-17 - Version 1.0.0
## 
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
##############################################################

#
#----[ OPEN ]-----------------------------------------------------------
#

includes/function_upload.php

#
#----[ FIND ]-----------------------------------------------------------
#

		if (!$this->upload->valid_dimensions($this))
		{
			$valid = false;
			if (extension_loaded('gd'))
			{
				$valid = $this->create_thumb();
			}
			
			if (!$valid)
			{
				$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_SIZE'], $this->upload->min_width, $this->upload->min_height, $this->upload->max_width, $this->upload->max_height, $this->width, $this->height);

				return false;
			}
		}

#
#----[ REPLACE WITH ]---------------------------------------------------
#

		if (!$this->upload->valid_dimensions($this))
		{
			$valid = $this->create_thumb();
			
			if (!$valid)
			{
				$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_SIZE'], $this->upload->min_width, $this->upload->min_height, $this->upload->max_width, $this->upload->max_height, $this->width, $this->height);

				return false;
			}
		}


#
#----[ FIND ]-----------------------------------------------------------
#
	/**
	* Create a thumb if uploaded image is too big
	*/
	function create_thumb()
	{
		switch ($this->extension)
		{
			case 'jpg':
			case 'jpeg':
				$src_image = imagecreatefromjpeg($this->destination_file);
			break;
			case 'gif':
				$src_image = imagecreatefromgif($this->destination_file);
			break;
			case 'png':
				$src_image = imagecreatefrompng($this->destination_file);
			break;
			case 'wbmp':
				$src_image = imagecreatefromwbmp($this->destination_file);
			break;
			case 'xbm':
				$src_image = imagecreatefromxbm($this->destination_file);
			break;
			default:
				return false;
		}
		$this->width = imagesx($src_image);
		$this->height = imagesy($src_image);
		if ($this->width > $this->height) 
		{
			$thumb_width = $this->upload->max_width;
			$thumb_height = $this->height*($this->upload->max_height/$this->width);
		}
		if ($this->width < $this->height) 
		{
			$thumb_width = $this->width*($this->upload->max_width/$this->height);
			$thumb_height = $this->upload->max_height;
		}
		if ($this->width == $this->height) 
		{
			$thumb_width = $this->upload->max_width;
			$thumb_height = $this->upload->max_height;
		}
		$thumb = imagecreatetruecolor($thumb_width,$thumb_height);
		imagecopyresampled($thumb,$src_image,0,0,0,0,$thumb_width,$thumb_height,$this->width,$this->height); 
		switch ($this->extension)
		{
			case 'jpg':
			case 'jpeg':
				imagejpeg($thumb,$this->destination_file);
			break;
			case 'gif':
				imagegif($thumb,$this->destination_file);
			break;
			case 'png':
				imagepng($thumb,$this->destination_file);
			break;
			case 'wbmp':
				imagewbmp($thumb,$this->destination_file);
			break;
			case 'xbm':
				imagexbm($thumb,$this->destination_file);
			break;
		}
		imagedestroy($thumb);
		imagedestroy($src_image);
		$this->width = $thumb_width;
		$this->height = $thumb_height;
		return true;
	}

#
#----[ REPLACE WITH ]---------------------------------------------------
#

	/**
	* Create a thumb if uploaded image is too big.
	* This function was based mainly on MediaWiki's thumbnail creating process
	* and create_thumbnail function in functions_posting.php
	* @source MediaWiki
	*/
	function create_thumb()
	{
		global $config;

		if ($this->width > $this->height) 
		{
			$thumb_width = $this->upload->max_width;
			$thumb_height = $this->height*($this->upload->max_height/$this->width);
		}
		else if ($this->width < $this->height) 
		{
			$thumb_width = $this->width*($this->upload->max_width/$this->height);
			$thumb_height = $this->upload->max_height;
		}
		else /* $this->width == $this->height */
		{
			$thumb_width = $this->upload->max_width;
			$thumb_height = $this->upload->max_height;
		}

		// Only use imagemagick if defined and the passthru function not disabled
		if ($config['img_imagick'] && function_exists('passthru'))
		{
			$quality = '';
			$sharpen = '';
			$frame = '';
			$animation = '';
			if ( $this->mimetype == 'image/jpeg' )
			{
				$quality = '-quality 80'; // 80%
				/** Reduction in linear dimensions below which sharpening will be enabled */
				if ( ( $thumb_width + $thumb_height ) / ( $this->width + $this->height ) < 0.85 )
				{
					$sharpen = '-sharpen 0x0.4';
				}
			}
			elseif ($this->mimetype == 'image/png')
			{
				$quality = '-quality 95'; // zlib 9, adaptive filtering
			}
			elseif ($this->mimetype == 'image/gif')
			{
				/**
				 * Force thumbnailing of animated GIFs above this size to a single
				 * frame instead of an animated thumbnail. ImageMagick seems to
				 * get real unhappy and doesn't play well with resource limits. :P
				 * Defaulting to 1 megapixel (1000x1000)
				 */
				if($this->width * $this->height > 1.0e6)
				{
					// Extract initial frame only
					$frame = '[0]';
				}
				else
				{
					// Coalesce is needed to scale animated GIFs properly (MediaWiki bug 1017).
					$animation = ' -coalesce ';
				}
			}
			# Specify white background color, will be used for transparent images
			# in Internet Explorer/Windows instead of default black.

			# Note, we specify "-size {$this->width}" and NOT "-size {$this->width}x{$this->height}".
			# It seems that ImageMagick has a bug wherein it produces thumbnails of
			# the wrong size in the second case.

			if (substr($config['img_imagick'], -1) !== '/')
			{
				$config['img_imagick'] .= '/';
			}
			$cmd  = 
				escapeshellcmd($config['img_imagick']) . 'convert' . ((defined('PHP_OS') && preg_match('#^win#i', PHP_OS)) ? '.exe' : '') .
				" {$quality} -background white -size {$this->width} ".
				escapeshellarg($this->destination_file . $frame) .
				$animation .
				// For the -resize option a "!" is needed to force exact size,
				// or ImageMagick may decide your ratio is wrong and slice off
				// a pixel.
				' -thumbnail ' . escapeshellarg( "{$thumb_width}x{$thumb_height}!" ) .
				" -depth 8 $sharpen " .
				escapeshellarg($this->destination_file) . ' 2>&1';

			@passthru($cmd);

			// after converting let's check the file dimensions again
			if (($this->image_info = @getimagesize($this->destination_file)) !== false)
			{
				$this->width = $this->image_info[0]; // the _real_ width
				$this->height = $this->image_info[1]; // the _real_ height
				if ($this->upload->valid_dimensions($this)) 
				{
					return true;
				}
			}
		}
		if (extension_loaded('gd'))
		{
			/* This code is greatly based on MediaWiki's thumbnail generation process */
			$typemap = array(
				'image/gif'          => array( 'imagecreatefromgif',  'palette',   'imagegif'  ),
				'image/jpeg'         => array( 'imagecreatefromjpeg', 'truecolor', array( __CLASS__, 'imagejpegwrapper' ) ),
				'image/png'          => array( 'imagecreatefrompng',  'bits',      'imagepng'  ),
				'image/vnd.wap.wbmp' => array( 'imagecreatefromwbmp', 'palette',   'imagewbmp'  ),
				'image/xbm'          => array( 'imagecreatefromxbm',  'palette',   'imagexbm'  ),
			);
			if (!isset( $typemap[$this->mimetype] ))
			{
				return false;
			}

			list($loader, $color_style, $save_type) = $typemap[$this->mimetype];
		
			if (!function_exists($loader))
			{
				return false;
			}
			$src_image = call_user_func( $loader, $this->destination_file );
			$thumb = imagecreatetruecolor($thumb_width, $thumb_height);

			// Initialise the destination image to transparent instead of
			// the default solid black, to support PNG and GIF transparency nicely
			$background = imagecolorallocate( $thumb, 0, 0, 0 );
			imagecolortransparent( $thumb, $background );
			imagealphablending( $thumb, false );
		
			if( $color_style == 'palette' ) {
				// Don't resample for paletted GIF images.
				// It may just uglify them, and completely breaks transparency.
				imagecopyresized( $thumb, $src_image,
					0,0,0,0,
					$thumb_width, $thumb_height, $this->width, $this->height );
			} else {
				imagecopyresampled( $thumb, $src_image,
					0,0,0,0,
					$thumb_width, $thumb_height, $this->width, $this->height );
			}

			imagesavealpha( $thumb, true );

			call_user_func( $save_type, $thumb, $this->destination_file );
			imagedestroy($thumb);
			imagedestroy($src_image);
			$this->width = $thumb_width;
			$this->height = $thumb_height;
			return true;
		}
		return false;
	}

	static function imagejpegwrapper( $dst_image, $thumb_path ) {
		imageinterlace( $dst_image );
		imagejpeg( $dst_image, $thumb_path, 95 );
	}

#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM
(Also updated the first post)
Last edited by Hołek on Sat Oct 17, 2009 9:21 pm, edited 2 times in total.

Peter77sx
Registered User
Posts: 3257
Joined: Wed Nov 09, 2005 2:51 pm

Re: [RC] Resize sent avatars

Post by Peter77sx » Sat Oct 17, 2009 12:29 pm

Very nice. :)

User avatar
kber
Registered User
Posts: 986
Joined: Sun May 07, 2006 9:22 am
Location: Egypt
Contact:

Re: [RC] Resize sent avatars

Post by kber » Sat Oct 17, 2009 2:34 pm

gif images still not animated
Hello All

Hołek
Registered User
Posts: 6
Joined: Wed Oct 14, 2009 5:52 pm

Re: [RC] Resize sent avatars

Post by Hołek » Sat Oct 17, 2009 2:50 pm

You have to specify a correct path to ImageMagick in your ACP. If you don't have ImageMagick, resized GIF avatars will not be animated.

autinhyeu
Registered User
Posts: 685
Joined: Mon Apr 14, 2008 6:34 am
Location: Vietnamese
Contact:

Re: [RC] Resize sent avatars

Post by autinhyeu » Sat Oct 17, 2009 8:09 pm

very nice ;)
thanks!

Edit: i think you had mistake at this line

Code: Select all

if( $colorStyle == 'palette' ) {
have be:

Code: Select all

if( $color_style == 'palette' ) {
phpBB Community in Vietnamese: http://www.phpbbvn.com

Sorry, my English language is fine impossible

Hołek
Registered User
Posts: 6
Joined: Wed Oct 14, 2009 5:52 pm

Re: [RC] Resize sent avatars

Post by Hołek » Sat Oct 17, 2009 9:20 pm

thanks, mod updated :)

DJ Skitzo
Registered User
Posts: 596
Joined: Sun Aug 12, 2007 7:25 pm

Re: [RC] Resize sent avatars

Post by DJ Skitzo » Fri Oct 23, 2009 7:03 pm

Thanks for adding animated image support but I'm afraid it still doesn't seem to work for me on my localhost, however, transparent backgrounds remain transparent.

Thanks.

EDIT: Didn't realise that I hadn't entered my ImageMagick settings within the admin panel, everything works fine now, thanks ;)

Peter77sx
Registered User
Posts: 3257
Joined: Wed Nov 09, 2005 2:51 pm

Re: [RC] Resize sent avatars

Post by Peter77sx » Sun Nov 29, 2009 11:12 am

Works a charm. thanks :)

nirok
Registered User
Posts: 14
Joined: Sun Oct 18, 2009 2:02 am

Re: [RC] Resize sent avatars

Post by nirok » Sun Dec 20, 2009 9:01 am

brillant after searching for several days and installing numerious avatar resizing mod, this simple one is perfect! great work, i wish this came packaged with phpbb

User avatar
knmt
Registered User
Posts: 110
Joined: Fri Aug 29, 2008 10:13 am
Contact:

Re: [RC] Resize sent avatars

Post by knmt » Sun Dec 20, 2009 10:23 am

nirok wrote:brillant after searching for several days and installing numerious avatar resizing mod, this simple one is perfect! great work, i wish this came packaged with phpbb
I would like to second this :mrgreen:

User avatar
BucsFan
Registered User
Posts: 253
Joined: Tue Nov 04, 2008 2:41 pm

Re: [RC] Resize sent avatars

Post by BucsFan » Tue Dec 22, 2009 5:25 pm

Hołek wrote:And there you go. Now supporting transparent and animated files. Update script from 1.0.0 to 1.1.1:

Code: Select all

##############################################################
## MOD Title: Resize sent avatars (1.0.0 > 1.1.1 UPDATE script)
## MOD Author: Hołek < holek@toolserver.org > (Michał Połtyn) http://toolserver.org/~holek/
## MOD Description: Aren't you tired of phpBB giving you dimension errors every time
##                  you upload new avatar? Why can't it resize the avatar on its own?
##                  Now it can! As long as the filesize is not exceeded,
##                  phpBB will upload and resize the file successfully.
## MOD Version: 1.1.1
## 
## Installation Level: Easy
## Installation Time: 1 minute
## Files To Edit: includes/functions_upload.php
## Included Files: 
## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##############################################################
## MOD History:
## 
## 2009-10-18 - Version 1.1.1
##     - fixed minor bug, causing wrong resizing method
##       to be applied to pallette-based images
##       (thanks to autinhyeu)
## 2009-10-18 - Version 1.1.0
##     - support for animated GIFs
##     - using Imagemagick if available
## 2009-10-17 - Version 1.0.0
## 
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
##############################################################

#
#----[ OPEN ]-----------------------------------------------------------
#

includes/function_upload.php

#
#----[ FIND ]-----------------------------------------------------------
#

		if (!$this->upload->valid_dimensions($this))
		{
			$valid = false;
			if (extension_loaded('gd'))
			{
				$valid = $this->create_thumb();
			}
			
			if (!$valid)
			{
				$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_SIZE'], $this->upload->min_width, $this->upload->min_height, $this->upload->max_width, $this->upload->max_height, $this->width, $this->height);

				return false;
			}
		}

#
#----[ REPLACE WITH ]---------------------------------------------------
#

		if (!$this->upload->valid_dimensions($this))
		{
			$valid = $this->create_thumb();
			
			if (!$valid)
			{
				$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_SIZE'], $this->upload->min_width, $this->upload->min_height, $this->upload->max_width, $this->upload->max_height, $this->width, $this->height);

				return false;
			}
		}


#
#----[ FIND ]-----------------------------------------------------------
#
	/**
	* Create a thumb if uploaded image is too big
	*/
	function create_thumb()
	{
		switch ($this->extension)
		{
			case 'jpg':
			case 'jpeg':
				$src_image = imagecreatefromjpeg($this->destination_file);
			break;
			case 'gif':
				$src_image = imagecreatefromgif($this->destination_file);
			break;
			case 'png':
				$src_image = imagecreatefrompng($this->destination_file);
			break;
			case 'wbmp':
				$src_image = imagecreatefromwbmp($this->destination_file);
			break;
			case 'xbm':
				$src_image = imagecreatefromxbm($this->destination_file);
			break;
			default:
				return false;
		}
		$this->width = imagesx($src_image);
		$this->height = imagesy($src_image);
		if ($this->width > $this->height) 
		{
			$thumb_width = $this->upload->max_width;
			$thumb_height = $this->height*($this->upload->max_height/$this->width);
		}
		if ($this->width < $this->height) 
		{
			$thumb_width = $this->width*($this->upload->max_width/$this->height);
			$thumb_height = $this->upload->max_height;
		}
		if ($this->width == $this->height) 
		{
			$thumb_width = $this->upload->max_width;
			$thumb_height = $this->upload->max_height;
		}
		$thumb = imagecreatetruecolor($thumb_width,$thumb_height);
		imagecopyresampled($thumb,$src_image,0,0,0,0,$thumb_width,$thumb_height,$this->width,$this->height); 
		switch ($this->extension)
		{
			case 'jpg':
			case 'jpeg':
				imagejpeg($thumb,$this->destination_file);
			break;
			case 'gif':
				imagegif($thumb,$this->destination_file);
			break;
			case 'png':
				imagepng($thumb,$this->destination_file);
			break;
			case 'wbmp':
				imagewbmp($thumb,$this->destination_file);
			break;
			case 'xbm':
				imagexbm($thumb,$this->destination_file);
			break;
		}
		imagedestroy($thumb);
		imagedestroy($src_image);
		$this->width = $thumb_width;
		$this->height = $thumb_height;
		return true;
	}

#
#----[ REPLACE WITH ]---------------------------------------------------
#

	/**
	* Create a thumb if uploaded image is too big.
	* This function was based mainly on MediaWiki's thumbnail creating process
	* and create_thumbnail function in functions_posting.php
	* @source MediaWiki
	*/
	function create_thumb()
	{
		global $config;

		if ($this->width > $this->height) 
		{
			$thumb_width = $this->upload->max_width;
			$thumb_height = $this->height*($this->upload->max_height/$this->width);
		}
		else if ($this->width < $this->height) 
		{
			$thumb_width = $this->width*($this->upload->max_width/$this->height);
			$thumb_height = $this->upload->max_height;
		}
		else /* $this->width == $this->height */
		{
			$thumb_width = $this->upload->max_width;
			$thumb_height = $this->upload->max_height;
		}

		// Only use imagemagick if defined and the passthru function not disabled
		if ($config['img_imagick'] && function_exists('passthru'))
		{
			$quality = '';
			$sharpen = '';
			$frame = '';
			$animation = '';
			if ( $this->mimetype == 'image/jpeg' )
			{
				$quality = '-quality 80'; // 80%
				/** Reduction in linear dimensions below which sharpening will be enabled */
				if ( ( $thumb_width + $thumb_height ) / ( $this->width + $this->height ) < 0.85 )
				{
					$sharpen = '-sharpen 0x0.4';
				}
			}
			elseif ($this->mimetype == 'image/png')
			{
				$quality = '-quality 95'; // zlib 9, adaptive filtering
			}
			elseif ($this->mimetype == 'image/gif')
			{
				/**
				 * Force thumbnailing of animated GIFs above this size to a single
				 * frame instead of an animated thumbnail. ImageMagick seems to
				 * get real unhappy and doesn't play well with resource limits. :P
				 * Defaulting to 1 megapixel (1000x1000)
				 */
				if($this->width * $this->height > 1.0e6)
				{
					// Extract initial frame only
					$frame = '[0]';
				}
				else
				{
					// Coalesce is needed to scale animated GIFs properly (MediaWiki bug 1017).
					$animation = ' -coalesce ';
				}
			}
			# Specify white background color, will be used for transparent images
			# in Internet Explorer/Windows instead of default black.

			# Note, we specify "-size {$this->width}" and NOT "-size {$this->width}x{$this->height}".
			# It seems that ImageMagick has a bug wherein it produces thumbnails of
			# the wrong size in the second case.

			if (substr($config['img_imagick'], -1) !== '/')
			{
				$config['img_imagick'] .= '/';
			}
			$cmd  = 
				escapeshellcmd($config['img_imagick']) . 'convert' . ((defined('PHP_OS') && preg_match('#^win#i', PHP_OS)) ? '.exe' : '') .
				" {$quality} -background white -size {$this->width} ".
				escapeshellarg($this->destination_file . $frame) .
				$animation .
				// For the -resize option a "!" is needed to force exact size,
				// or ImageMagick may decide your ratio is wrong and slice off
				// a pixel.
				' -thumbnail ' . escapeshellarg( "{$thumb_width}x{$thumb_height}!" ) .
				" -depth 8 $sharpen " .
				escapeshellarg($this->destination_file) . ' 2>&1';

			@passthru($cmd);

			// after converting let's check the file dimensions again
			if (($this->image_info = @getimagesize($this->destination_file)) !== false)
			{
				$this->width = $this->image_info[0]; // the _real_ width
				$this->height = $this->image_info[1]; // the _real_ height
				if ($this->upload->valid_dimensions($this)) 
				{
					return true;
				}
			}
		}
		if (extension_loaded('gd'))
		{
			/* This code is greatly based on MediaWiki's thumbnail generation process */
			$typemap = array(
				'image/gif'          => array( 'imagecreatefromgif',  'palette',   'imagegif'  ),
				'image/jpeg'         => array( 'imagecreatefromjpeg', 'truecolor', array( __CLASS__, 'imagejpegwrapper' ) ),
				'image/png'          => array( 'imagecreatefrompng',  'bits',      'imagepng'  ),
				'image/vnd.wap.wbmp' => array( 'imagecreatefromwbmp', 'palette',   'imagewbmp'  ),
				'image/xbm'          => array( 'imagecreatefromxbm',  'palette',   'imagexbm'  ),
			);
			if (!isset( $typemap[$this->mimetype] ))
			{
				return false;
			}

			list($loader, $color_style, $save_type) = $typemap[$this->mimetype];
		
			if (!function_exists($loader))
			{
				return false;
			}
			$src_image = call_user_func( $loader, $this->destination_file );
			$thumb = imagecreatetruecolor($thumb_width, $thumb_height);

			// Initialise the destination image to transparent instead of
			// the default solid black, to support PNG and GIF transparency nicely
			$background = imagecolorallocate( $thumb, 0, 0, 0 );
			imagecolortransparent( $thumb, $background );
			imagealphablending( $thumb, false );
		
			if( $color_style == 'palette' ) {
				// Don't resample for paletted GIF images.
				// It may just uglify them, and completely breaks transparency.
				imagecopyresized( $thumb, $src_image,
					0,0,0,0,
					$thumb_width, $thumb_height, $this->width, $this->height );
			} else {
				imagecopyresampled( $thumb, $src_image,
					0,0,0,0,
					$thumb_width, $thumb_height, $this->width, $this->height );
			}

			imagesavealpha( $thumb, true );

			call_user_func( $save_type, $thumb, $this->destination_file );
			imagedestroy($thumb);
			imagedestroy($src_image);
			$this->width = $thumb_width;
			$this->height = $thumb_height;
			return true;
		}
		return false;
	}

	static function imagejpegwrapper( $dst_image, $thumb_path ) {
		imageinterlace( $dst_image );
		imagejpeg( $dst_image, $thumb_path, 95 );
	}

#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM
(Also updated the first post)
The first edit is different... and the second edit doesn't exist in my functions_upload.php file AT ALL. What shall I do?

Below is a copy of my includes/functions_upload.php file...

Code: Select all

<?php
/**
*
* @package phpBB3
* @version $Id: functions_upload.php 8555 2008-05-15 14:10:11Z Kellanved $
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* Responsible for holding all file relevant information, as well as doing file-specific operations.
* The {@link fileupload fileupload class} can be used to upload several files, each of them being this object to operate further on.
* @package phpBB3
*/
class filespec
{
	var $filename = '';
	var $realname = '';
	var $uploadname = '';
	var $mimetype = '';
	var $extension = '';
	var $filesize = 0;
	var $width = 0;
	var $height = 0;
	var $image_info = array();

	var $destination_file = '';
	var $destination_path = '';

	var $file_moved = false;
	var $init_error = false;
	var $local = false;

	var $error = array();

	var $upload = '';

	/**
	* File Class
	* @access private
	*/
	function filespec($upload_ary, $upload_namespace)
	{
		if (!isset($upload_ary))
		{
			$this->init_error = true;
			return;
		}

		$this->filename = $upload_ary['tmp_name'];
		$this->filesize = $upload_ary['size'];
		$name = trim(htmlspecialchars(basename($upload_ary['name'])));
		$this->realname = $this->uploadname = (STRIP) ? stripslashes($name) : $name;
		$this->mimetype = $upload_ary['type'];

		// Opera adds the name to the mime type
		$this->mimetype	= (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype;

		if (!$this->mimetype)
		{
			$this->mimetype = 'application/octetstream';
		}

		$this->extension = strtolower($this->get_extension($this->realname));

		// Try to get real filesize from temporary folder (not always working) ;)
		$this->filesize = (@filesize($this->filename)) ? @filesize($this->filename) : $this->filesize;

		$this->width = $this->height = 0;
		$this->file_moved = false;

		$this->local = (isset($upload_ary['local_mode'])) ? true : false;
		$this->upload = $upload_namespace;
	}

	/**
	* Cleans destination filename
	*
	* @param real|unique|unique_ext $mode real creates a realname, filtering some characters, lowering every character. Unique creates an unique filename
	* @param string $prefix Prefix applied to filename
	* @access public
	*/
	function clean_filename($mode = 'unique', $prefix = '', $user_id = '')
	{
		if ($this->init_error)
		{
			return;
		}

		switch ($mode)
		{
			case 'real':
				// Remove every extension from filename (to not let the mime bug being exposed)
				if (strpos($this->realname, '.') !== false)
				{
					$this->realname = substr($this->realname, 0, strpos($this->realname, '.'));
				}

				// Replace any chars which may cause us problems with _
				$bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|');

				$this->realname = rawurlencode(str_replace($bad_chars, '_', strtolower($this->realname)));
				$this->realname = preg_replace("/%(\w{2})/", '_', $this->realname);

				$this->realname = $prefix . $this->realname . '.' . $this->extension;
			break;

			case 'unique':
				$this->realname = $prefix . md5(unique_id());
			break;

			case 'avatar':
				$this->extension = strtolower($this->extension);
				$this->realname = $prefix . $user_id . '.' . $this->extension;
				
			break;
			
			case 'unique_ext':
			default:
				$this->realname = $prefix . md5(unique_id()) . '.' . $this->extension;
			break;
		}
	}

	/**
	* Get property from file object
	*/
	function get($property)
	{
		if ($this->init_error || !isset($this->$property))
		{
			return false;
		}

		return $this->$property;
	}

	/**
	* Check if file is an image (mimetype)
	*
	* @return true if it is an image, false if not
	*/
	function is_image()
	{
		return (strpos($this->mimetype, 'image/') !== false) ? true : false;
	}

	/**
	* Check if the file got correctly uploaded
	*
	* @return true if it is a valid upload, false if not
	*/
	function is_uploaded()
	{
		if (!$this->local && !is_uploaded_file($this->filename))
		{
			return false;
		}

		if ($this->local && !file_exists($this->filename))
		{
			return false;
		}

		return true;
	}

	/**
	* Remove file
	*/
	function remove()
	{
		if ($this->file_moved)
		{
			@unlink($this->destination_file);
		}
	}

	/**
	* Get file extension
	*/
	function get_extension($filename)
	{
		if (strpos($filename, '.') === false)
		{
			return '';
		}

		$filename = explode('.', $filename);
		return array_pop($filename);
	}

	/**
	* Get mimetype. Utilize mime_content_type if the function exist.
	* Not used at the moment...
	*/
	function get_mimetype($filename)
	{
		$mimetype = '';

		if (function_exists('mime_content_type'))
		{
			$mimetype = mime_content_type($filename);
		}

		// Some browsers choke on a mimetype of application/octet-stream
		if (!$mimetype || $mimetype == 'application/octet-stream')
		{
			$mimetype = 'application/octetstream';
		}

		return $mimetype;
	}

	/**
	* Get filesize
	*/
	function get_filesize($filename)
	{
		return @filesize($filename);
	}
	
	
	/**
	* Check the first 256 bytes for forbidden content
	*/
	function check_content($disallowed_content)
	{
		if (empty($disallowed_content))
		{
			return true;
		}
		
		$fp = @fopen($this->filename, 'rb');

		if ($fp !== false)
		{
			$ie_mime_relevant = fread($fp, 256);
			fclose($fp);
			foreach ($disallowed_content as $forbidden)
			{
				if (stripos($ie_mime_relevant, '<' . $forbidden) !== false)
				{
					return false;
				}
			}
		}
		return true;
	}

	/**
	* Move file to destination folder
	* The phpbb_root_path variable will be applied to the destination path
	*
	* @param string $destination_path Destination path, for example $config['avatar_path']
	* @param bool $overwrite If set to true, an already existing file will be overwritten
	* @param octal $chmod Permission mask for chmodding the file after a successful move
	* @access public
	*/
	function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = 0666)
	{
		global $user, $phpbb_root_path;

		if (sizeof($this->error))
		{
			return false;
		}

		// We need to trust the admin in specifying valid upload directories and an attacker not being able to overwrite it...
		$this->destination_path = $phpbb_root_path . $destination;

		// Check if the destination path exist...
		if (!file_exists($this->destination_path))
		{
			@unlink($this->filename);
			return false;
		}

		$upload_mode = (@ini_get('open_basedir') || @ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'on') ? 'move' : 'copy';
		$upload_mode = ($this->local) ? 'local' : $upload_mode;
		$this->destination_file = $this->destination_path . '/' . basename($this->realname);

		// Check if the file already exist, else there is something wrong...
		if (file_exists($this->destination_file) && !$overwrite)
		{
			@unlink($this->filename);
		}
		else
		{
			if (file_exists($this->destination_file))
			{
				@unlink($this->destination_file);
			}

			switch ($upload_mode)
			{
				case 'copy':

					if (!@copy($this->filename, $this->destination_file))
					{
						if (!@move_uploaded_file($this->filename, $this->destination_file))
						{
							$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
							return false;
						}
					}

					@unlink($this->filename);

				break;

				case 'move':

					if (!@move_uploaded_file($this->filename, $this->destination_file))
					{
						if (!@copy($this->filename, $this->destination_file))
						{
							$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
							return false;
						}
					}

					@unlink($this->filename);

				break;

				case 'local':

					if (!@copy($this->filename, $this->destination_file))
					{
						$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
						return false;
					}
					@unlink($this->filename);

				break;
			}

			@chmod($this->destination_file, $chmod);
		}

		// Try to get real filesize from destination folder
		$this->filesize = (@filesize($this->destination_file)) ? @filesize($this->destination_file) : $this->filesize;

		if ($this->is_image() && !$skip_image_check)
		{
			$this->width = $this->height = 0;

			if (($this->image_info = @getimagesize($this->destination_file)) !== false)
			{
				$this->width = $this->image_info[0];
				$this->height = $this->image_info[1];

				if (!empty($this->image_info['mime']))
				{
					$this->mimetype = $this->image_info['mime'];
				}

				// Check image type
				$types = $this->upload->image_types();

				if (!isset($types[$this->image_info[2]]) || !in_array($this->extension, $types[$this->image_info[2]]))
				{
					if (!isset($types[$this->image_info[2]]))
					{
						$this->error[] = sprintf($user->lang['IMAGE_FILETYPE_INVALID'], $this->image_info[2], $this->mimetype);
					}
					else
					{
						$this->error[] = sprintf($user->lang['IMAGE_FILETYPE_MISMATCH'], $types[$this->image_info[2]][0], $this->extension);
					}
				}

				// Make sure the dimensions match a valid image
				if (empty($this->width) || empty($this->height))
				{
					$this->error[] = $user->lang['ATTACHED_IMAGE_NOT_IMAGE'];
				}
			}
			else
			{
				$this->error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
			}
		}

		$this->file_moved = true;
		$this->additional_checks();
		unset($this->upload);

		return true;
	}

	/**
	* Performing additional checks
	*/
	function additional_checks()
	{
		global $user;

		if (!$this->file_moved)
		{
			return false;
		}

		// Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
		if ($this->upload->max_filesize && ($this->get('filesize') > $this->upload->max_filesize || $this->filesize == 0))
		{
			$size_lang = ($this->upload->max_filesize >= 1048576) ? $user->lang['MIB'] : (($this->upload->max_filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES'] );
			$max_filesize = get_formatted_filesize($this->upload->max_filesize, false);
	
			$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang);

			return false;
		}

		if (!$this->upload->valid_dimensions($this))
		{
			$this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_SIZE'], $this->upload->min_width, $this->upload->min_height, $this->upload->max_width, $this->upload->max_height, $this->width, $this->height);

			return false;
		}

		return true;
	}
}

/**
* Class for assigning error messages before a real filespec class can be assigned
*
* @package phpBB3
*/
class fileerror extends filespec
{
	function fileerror($error_msg)
	{
		$this->error[] = $error_msg;
	}
}

/**
* File upload class
* Init class (all parameters optional and able to be set/overwritten separately) - scope is global and valid for all uploads
*
* @package phpBB3
*/
class fileupload
{
	var $allowed_extensions = array();
	var $disallowed_content = array();
	var $max_filesize = 0;
	var $min_width = 0;
	var $min_height = 0;
	var $max_width = 0;
	var $max_height = 0;
	var $error_prefix = '';

	/**
	* Init file upload class.
	*
	* @param string $error_prefix Used error messages will get prefixed by this string
	* @param array $allowed_extensions Array of allowed extensions, for example array('jpg', 'jpeg', 'gif', 'png')
	* @param int $max_filesize Maximum filesize
	* @param int $min_width Minimum image width (only checked for images)
	* @param int $min_height Minimum image height (only checked for images)
	* @param int $max_width Maximum image width (only checked for images)
	* @param int $max_height Maximum image height (only checked for images)
	*
	*/
	function fileupload($error_prefix = '', $allowed_extensions = false, $max_filesize = false, $min_width = false, $min_height = false, $max_width = false, $max_height = false, $disallowed_content = false)
	{
		$this->set_allowed_extensions($allowed_extensions);
		$this->set_max_filesize($max_filesize);
		$this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height);
		$this->set_error_prefix($error_prefix);
		$this->set_disallowed_content($disallowed_content);
	}

	/**
	* Reset vars
	*/
	function reset_vars()
	{
		$this->max_filesize = 0;
		$this->min_width = $this->min_height = $this->max_width = $this->max_height = 0;
		$this->error_prefix = '';
		$this->allowed_extensions = array();
		$this->disallowed_content = array();
	}

	/**
	* Set allowed extensions
	*/
	function set_allowed_extensions($allowed_extensions)
	{
		if ($allowed_extensions !== false && is_array($allowed_extensions))
		{
			$this->allowed_extensions = $allowed_extensions;
		}
	}

	/**
	* Set allowed dimensions
	*/
	function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height)
	{
		$this->min_width = (int) $min_width;
		$this->min_height = (int) $min_height;
		$this->max_width = (int) $max_width;
		$this->max_height = (int) $max_height;
	}

	/**
	* Set maximum allowed filesize
	*/
	function set_max_filesize($max_filesize)
	{
		if ($max_filesize !== false && (int) $max_filesize)
		{
			$this->max_filesize = (int) $max_filesize;
		}
	}
	
	/**
	* Set disallowed strings
	*/
	function set_disallowed_content($disallowed_content)
	{
		if ($disallowed_content !== false && is_array($disallowed_content))
		{
			$this->disallowed_content = $disallowed_content;
		}
	}

	/**
	* Set error prefix
	*/
	function set_error_prefix($error_prefix)
	{
		$this->error_prefix = $error_prefix;
	}

	/**
	* Form upload method
	* Upload file from users harddisk
	*
	* @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified)
	* @return object $file Object "filespec" is returned, all further operations can be done with this object
	* @access public
	*/
	function form_upload($form_name)
	{
		global $user;

		unset($_FILES[$form_name]['local_mode']);
		$file = new filespec($_FILES[$form_name], $this);

		if ($file->init_error)
		{
			$file->error[] = '';
			return $file;
		}

		// Error array filled?
		if (isset($_FILES[$form_name]['error']))
		{
			$error = $this->assign_internal_error($_FILES[$form_name]['error']);

			if ($error !== false)
			{
				$file->error[] = $error;
				return $file;
			}
		}

		// Check if empty file got uploaded (not catched by is_uploaded_file)
		if (isset($_FILES[$form_name]['size']) && $_FILES[$form_name]['size'] == 0)
		{
			$file->error[] = $user->lang[$this->error_prefix . 'EMPTY_FILEUPLOAD'];
			return $file;
		}

		// PHP Upload filesize exceeded
		if ($file->get('filename') == 'none')
		{
			$file->error[] = (@ini_get('upload_max_filesize') == '') ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], @ini_get('upload_max_filesize'));
			return $file;
		}

		// Not correctly uploaded
		if (!$file->is_uploaded())
		{
			$file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
			return $file;
		}

		$this->common_checks($file);

		return $file;
	}

	/**
	* Move file from another location to phpBB
	*/
	function local_upload($source_file, $filedata = false)
	{
		global $user;

		$form_name = 'local';

		$_FILES[$form_name]['local_mode'] = true;
		$_FILES[$form_name]['tmp_name'] = $source_file;

		if ($filedata === false)
		{
			$_FILES[$form_name]['name'] = basename($source_file);
			$_FILES[$form_name]['size'] = 0;
			$mimetype = '';

			if (function_exists('mime_content_type'))
			{
				$mimetype = mime_content_type($source_file);
			}

			// Some browsers choke on a mimetype of application/octet-stream
			if (!$mimetype || $mimetype == 'application/octet-stream')
			{
				$mimetype = 'application/octetstream';
			}

			$_FILES[$form_name]['type'] = $mimetype;
		}
		else
		{
			$_FILES[$form_name]['name'] = $filedata['realname'];
			$_FILES[$form_name]['size'] = $filedata['size'];
			$_FILES[$form_name]['type'] = $filedata['type'];
		}

		$file = new filespec($_FILES[$form_name], $this);

		if ($file->init_error)
		{
			$file->error[] = '';
			return $file;
		}

		if (isset($_FILES[$form_name]['error']))
		{
			$error = $this->assign_internal_error($_FILES[$form_name]['error']);

			if ($error !== false)
			{
				$file->error[] = $error;
				return $file;
			}
		}

		// PHP Upload filesize exceeded
		if ($file->get('filename') == 'none')
		{
			$file->error[] = (@ini_get('upload_max_filesize') == '') ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], @ini_get('upload_max_filesize'));
			return $file;
		}

		// Not correctly uploaded
		if (!$file->is_uploaded())
		{
			$file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
			return $file;
		}

		$this->common_checks($file);

		return $file;
	}

	/**
	* Remote upload method
	* Uploads file from given url
	*
	* @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif
	* @return object $file Object "filespec" is returned, all further operations can be done with this object
	* @access public
	*/
	function remote_upload($upload_url)
	{
		global $user, $phpbb_root_path;

		$upload_ary = array();
		$upload_ary['local_mode'] = true;

		if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match))
		{
			$file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
			return $file;
		}

		if (empty($match[2]))
		{
			$file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
			return $file;
		}

		$url = parse_url($upload_url);

		$host = $url['host'];
		$path = $url['path'];
		$port = (!empty($url['port'])) ? (int) $url['port'] : 80;

		$upload_ary['type'] = 'application/octet-stream';

		$url['path'] = explode('.', $url['path']);
		$ext = array_pop($url['path']);

		$url['path'] = implode('', $url['path']);
		$upload_ary['name'] = basename($url['path']) . (($ext) ? '.' . $ext : '');
		$filename = $url['path'];
		$filesize = 0;

		$errno = 0;
		$errstr = '';

		if (!($fsock = @fsockopen($host, $port, $errno, $errstr)))
		{
			$file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
			return $file;
		}

		// Make sure $path not beginning with /
		if (strpos($path, '/') === 0)
		{
			$path = substr($path, 1);
		}

		fputs($fsock, 'GET /' . $path . " HTTP/1.1\r\n");
		fputs($fsock, "HOST: " . $host . "\r\n");
		fputs($fsock, "Connection: close\r\n\r\n");

		$get_info = false;
		$data = '';
		while (!@feof($fsock))
		{
			if ($get_info)
			{
				$data .= @fread($fsock, 1024);
			}
			else
			{
				$line = @fgets($fsock, 1024);

				if ($line == "\r\n")
				{
					$get_info = true;
				}
				else
				{
					if (stripos($line, 'content-type: ') !== false)
					{
						$upload_ary['type'] = rtrim(str_replace('content-type: ', '', strtolower($line)));
					}
					else if (stripos($line, '404 not found') !== false)
					{
						$file = new fileerror($user->lang[$this->error_prefix . 'URL_NOT_FOUND']);
						return $file;
					}
				}
			}
		}
		@fclose($fsock);

		if (empty($data))
		{
			$file = new fileerror($user->lang[$this->error_prefix . 'EMPTY_REMOTE_DATA']);
			return $file;
		}

		$tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $phpbb_root_path . 'cache';
		$filename = tempnam($tmp_path, unique_id() . '-');

		if (!($fp = @fopen($filename, 'wb')))
		{
			$file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
			return $file;
		}

		$upload_ary['size'] = fwrite($fp, $data);
		fclose($fp);
		unset($data);

		$upload_ary['tmp_name'] = $filename;

		$file = new filespec($upload_ary, $this);
		$this->common_checks($file);

		return $file;
	}

	/**
	* Assign internal error
	* @access private
	*/
	function assign_internal_error($errorcode)
	{
		global $user;

		switch ($errorcode)
		{
			case 1:
				$error = (@ini_get('upload_max_filesize') == '') ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], @ini_get('upload_max_filesize'));
			break;

			case 2:
				$size_lang = ($this->max_filesize >= 1048576) ? $user->lang['MIB'] : (($this->max_filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES']);
				$max_filesize = get_formatted_filesize($this->max_filesize, false);

				$error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang);
			break;

			case 3:
				$error = $user->lang[$this->error_prefix . 'PARTIAL_UPLOAD'];
			break;

			case 4:
				$error = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
			break;

			case 6:
				$error = 'Temporary folder could not be found. Please check your PHP installation.';
			break;

			default:
				$error = false;
			break;
		}

		return $error;
	}

	/**
	* Perform common checks
	*/
	function common_checks(&$file)
	{
		global $user;

		// Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
		if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0))
		{
			$size_lang = ($this->max_filesize >= 1048576) ? $user->lang['MIB'] : (($this->max_filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES']);
			$max_filesize = get_formatted_filesize($this->max_filesize, false);

			$file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang);
		}

		// check Filename
		if (preg_match("#[\\/:*?\"<>|]#i", $file->get('realname')))
		{
			$file->error[] = sprintf($user->lang[$this->error_prefix . 'INVALID_FILENAME'], $file->get('realname'));
		}

		// Invalid Extension
		if (!$this->valid_extension($file))
		{
			$file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_EXTENSION'], $file->get('extension'));
		}
		
		// MIME Sniffing
		if (!$this->valid_content($file))
		{
			$file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_CONTENT']);
		}
	}

	/**
	* Check for allowed extension
	*/
	function valid_extension(&$file)
	{
		return (in_array($file->get('extension'), $this->allowed_extensions)) ? true : false;
	}

	/**
	* Check for allowed dimension
	*/
	function valid_dimensions(&$file)
	{
		if (!$this->max_width && !$this->max_height && !$this->min_width && !$this->min_height)
		{
			return true;
		}

		if (($file->get('width') > $this->max_width && $this->max_width) ||
			($file->get('height') > $this->max_height && $this->max_height) ||
			($file->get('width') < $this->min_width && $this->min_width) ||
			($file->get('height') < $this->min_height && $this->min_height))
		{
			return false;
		}

		return true;
	}

	/**
	* Check if form upload is valid
	*/
	function is_valid($form_name)
	{
		return (isset($_FILES[$form_name]) && $_FILES[$form_name]['name'] != 'none') ? true : false;
	}


	/**
	* Check for allowed extension
	*/
	function valid_content(&$file)
	{
		return ($file->check_content($this->disallowed_content));
	}

	/**
	* Return image type/extension mapping
	*/
	function image_types()
	{
		return array(
			1 => array('gif'),
			2 => array('jpg', 'jpeg'),
			3 => array('png'),
			4 => array('swf'),
			5 => array('psd'),
			6 => array('bmp'),
			7 => array('tif', 'tiff'),
			8 => array('tif', 'tiff'),
			9 => array('jpg', 'jpeg'),
			10 => array('jpg', 'jpeg'),
			11 => array('jpg', 'jpeg'),
			12 => array('jpg', 'jpeg'),
			13 => array('swc'),
			14 => array('iff'),
			15 => array('wbmp'),
			16 => array('xbm'),
		);
	}
}

?>
Also, where (in the admin panel) do I enter the path to imagemagick?

My PHPBB version: 3.0.2
Hostgator - The very best webhosting available on the net
The most reliable uptime - The lowest prices - Excellent tech support

Locked

Return to “[3.0.x] Abandoned MODs”