Jump to content


Júnior2015

Member Since 05/08/2015
Offline Last Active 09/08/2015, 02:08
-----

Topics I've Started

Função cortar imagem redimensionada

05/08/2015, 22:03

Olá, pessoal! Tenho um fórum phpBB e nele adicionei um script que redimensiona os avatares automaticamente no arquivo (functions_posting.php).

 

Porém, o redimensionamento é feito mantendo proporções. Eu defini em meu 'board' dimensões mínimas e máximas de avatar para 170x170, porém, a imagem é redimensionada proporcionalmente desrespeitando essas dimensões impostas.

 

Como insiro a função crop no meio desse script para além de redimensionar o avatar, a imagem dele ser cortada para suas dimensões ficarem exatamente 170x170px?

 

O script que tenho atualmente:

 

no arquivo drive_upload.php

//	MODIFICATION BY DION DESIGNS
		// resize uploaded avatar
		include_once($this->phpbb_root_path . 'includes/functions_posting.' . $this->php_ext);
		@ini_set('memory_limit','128M');
		$result = create_thumbnail($file->get('filename'), '', '', $this->config['avatar_max_width'], $this->config['avatar_max_height']);
		if ($result) {
			list($file->width, $file->height) = $result;
		}
//	END MODIFICATION[/code]
No arquivo functions_posting.php
 
[/code]

/**
* Create Thumbnail
*
* Modified by Dion Designs to support resizing avatars
*/
function create_thumbnail($source, $destination, $mimetype, $new_width = 0, $new_height = 0) {
	global $config;

	if ($new_width) {
		$destination = $source;
	}
	else {
		$min_filesize = (int) $config['img_min_thumb_filesize'];
		$img_filesize = (file_exists($source)) ? @filesize($source) : false;

		if (!$img_filesize || $img_filesize <= $min_filesize) {
			return false;
		}
	}

	$dimension = @getimagesize($source);

	if ($dimension === false) {
		return false;
	}
	list($width, $height, $type, ) = $dimension;

	if (empty($width) || empty($height)) {
		return false;
	}

	if (!$new_width) {
		list($new_width, $new_height) = get_img_size_format($width, $height);
	}
	else if ($height > $new_height || $width > $new_width) {
		$h_ratio = $new_height / $height;
		$w_ratio = $new_width / $width;
		$scale_factor = ($h_ratio < $w_ratio) ? $h_ratio : $w_ratio;
		$new_width = ($h_ratio < $w_ratio) ? round($width * $scale_factor) : $new_width;
		$new_height = ($w_ratio < $h_ratio) ? round($height * $scale_factor) : $new_height;
	}
	else {
		return false;
	}

	// Do not create a thumbnail if the resulting width/height is bigger than the original one
	if ($new_width >= $width && $new_height >= $height) {
		return false;
	}

	$used_imagick = false;

	// Only use imagemagick if defined and the passthru function not disabled
	if ($config['img_imagick'] && function_exists('passthru')) {
		if (substr($config['img_imagick'], -1) !== '/')	{
			$config['img_imagick'] .= '/';
		}

		@passthru(escapeshellcmd($config['img_imagick']) . 'convert' . ((defined('PHP_OS') && preg_match('#^win#i', PHP_OS)) ? '.exe' : '') . ' -quality 85 -geometry ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $source) . '" "' . str_replace('\\', '/', $destination) . '"');

		if (file_exists($destination)) {
			$used_imagick = true;
		}
	}

	if (!$used_imagick) {
		$type = get_supported_image_types($type);

		if ($type['gd']) {
			// If the type is not supported, we are not able to create a thumbnail
			if ($type['format'] === false) {
				return false;
			}

			switch ($type['format']) {
				case IMG_GIF:
					$image = @imagecreatefromgif($source);
				break;

				case IMG_JPG:
					@ini_set('gd.jpeg_ignore_warning', 1);
					$image = @imagecreatefromjpeg($source);
				break;

				case IMG_PNG:
					$image = @imagecreatefrompng($source);
				break;

				case IMG_WBMP:
					$image = @imagecreatefromwbmp($source);
				break;
			}

			if (empty($image)) {
				return false;
			}

			if ($type['version'] == 1) {
				$new_image = imagecreate($new_width, $new_height);

				if ($new_image === false) {
					return false;
				}

				imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
			}
			else {
				$new_image = imagecreatetruecolor($new_width, $new_height);

				if ($new_image === false) {
					return false;
				}

				// Preserve alpha transparency (png for example)
				@imagealphablending($new_image, false);
				@imagesavealpha($new_image, true);

				imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
			}

			// If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
			if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on') {
				@touch($destination);
			}

			switch ($type['format']) {
				case IMG_GIF:
					imagegif($new_image, $destination);
				break;

				case IMG_JPG:
					imagejpeg($new_image, $destination, 90);
				break;

				case IMG_PNG:
					imagepng($new_image, $destination);
				break;

				case IMG_WBMP:
					imagewbmp($new_image, $destination);
				break;
			}

			imagedestroy($new_image);
		}
		else {
			return false;
		}
	}

	if (!file_exists($destination)) {
		return false;
	}

	if (!$new_width) {
		phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
	}
	return array($new_width, $new_height);
}

IPB Skin By Virteq