06.03.2013 Мод обновлён - исправлено масштабирование, добавлена поддержка "imagemagick". Всё работает корректно. Дебуг больше не ругается ...
Мод для уменьшения картинок вложений на стороне сервера - Resize images with the true diemensions version 0.32
Мод позволяет уменьшить фотографии и другие картинки физически как по ширине и длине, так и по весу при загрузке вложений на сервер.
К примеру, уменьшим размер до 1024х768 px.
В файле includes/functions_upload.php
Найти:
Код: Выделить всё
phpbb_chmod($this->destination_file, $chmod);
Перед добавить:
Код: Выделить всё
if ( $this->is_image() )
{
$this->resize_images();
}
Найти:
Код: Выделить всё
$this->file_moved = true;
$this->additional_checks();
unset($this->upload);
return true;
}
После добавить:
Код: Выделить всё
/**
* Resize images with the true diemensions
* version 0.32
*/
function resize_images()
{
global $config;
if ( $this->is_image() )
{
$limit_width = 1024;
$limit_height = 768;
$quality = 90;
$size = getimagesize($this->destination_file);
$width = $size[0];
$height = $size[1];
if($height > $limit_height OR $width > $limit_width)
{
$int_factor = min(($limit_width / $width), ($limit_height / $height));
// pano-test add
// if (max($width, $height) / min($width, $height) > 2) $int_factor = $int_factor * 1.5;
// end
$resize_width = round($width * $int_factor);
$resize_height = round($height * $int_factor);
// Only use imagemagick if defined and the passthru function not disabled
if ($config['img_imagick'] && function_exists('passthru'))
{
$quality = '';
$sharpen = '';
$frame = '';
$animation = '';
if ( $this->extension == "jpg" || $this->extension == "jpeg" )
{
$quality = '-quality 80'; // 80%
/** Reduction in linear dimensions below which sharpening will be enabled */
if ( ( $resize_width + $resize_height ) / ( $width + $height ) < 0.85 )
{
$sharpen = '-sharpen 0x0.4';
}
}
elseif ($this->extension == "png")
{
$quality = '-quality 95'; // zlib 9, adaptive filtering
}
elseif ($this->extension == "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($width * $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 {$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( "{$resize_width}x{$resize_height}!" ) .
" -depth 8 $sharpen " .
escapeshellarg($this->destination_file) . ' 2>&1';
@passthru($cmd);
return true;
}
if (extension_loaded('gd'))
{
$destination = imagecreatetruecolor($resize_width, $resize_height);
if ( $this->extension == "jpg" || $this->extension == "jpeg" )
{
@ini_set('gd.jpeg_ignore_warning', 1);
$source = imagecreatefromjpeg($this->destination_file);
}
elseif ( $this->extension == "png" )
{
@imagealphablending($destination, false);
@imagesavealpha($destination, true);
$source = imagecreatefrompng($this->destination_file);
}
elseif ( $this->extension == "gif" )
{
$source = imagecreatefromgif($this->destination_file);
$trnprt_indx = imagecolortransparent($source);
if ($trnprt_indx >= 0) //transparent
{
$trnprt_color = imagecolorsforindex($source, $trnprt_indx);
$trnprt_indx = imagecolorallocate($destination, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($destination, 0, 0, $trnprt_indx);
imagecolortransparent($destination, $trnprt_indx);
}
}
imagecopyresampled($destination, $source, 0, 0, 0, 0, $resize_width, $resize_height, $size[0], $size[1]);
if ( $this->extension == "jpg" || $this->extension == "jpeg" ) imagejpeg($destination, $this->destination_file, $quality);
elseif ( $this->extension == "png" ) imagepng($destination, $this->destination_file);
elseif ( $this->extension == "gif" ) imagegif($destination, $this->destination_file);
imagedestroy($destination);
imagedestroy($source);
return true;
}
return false;
}
}
}
$limite_largeur = "1024px"; - максимальная ширина изображения.
$limite_hauteur = "768px"; - максимальная высота изображения.
Измените эти данные на свое усмотрение.
Убедитесь что у вас установлена библиотека GD
Выделите под PHP ~96-128mb памяти
В настройках php прверьте значения post_max_size, upload_max_filesize
В настройках форума проверьте параметры максимального веса аттачмента и максимальное разрешение картинок.
Для полного счастья осталось прилепить копирайт - водяной знак- watermark.
Ставим watermark на загружаемых вложениях.
В тот же файл includes/functions_upload.php
Найти:
Код: Выделить всё
if ( $this->extension == "jpg" || $this->extension == "jpeg" ) imagejpeg($destination, $this->destination_file, $quality);
Перед добавить:
Код: Выделить всё
$black = imagecolorallocate($destination,255,255,255);
$str = "my watermark";
imagestring($destination,5,10,30,$str, $black);
Где 10, 30 - координаты водяного знака по горизонтали и вертикали. Правда, watermark будет ставиться только на тех изображениях, которые были автоматически уменьшены с помощь вышеописанного хака.