2
#1
Server-side resize of uploaded avatars
Posted October 22nd 2014, 2:01pm
Here is the first of what I hope to be many phpBB 3.1 hacks. :)

For some unknown reason, phpBB 3.1 does not support server-side resizing of uploaded avatars. I read the excuses, and none of them made sense. There is a function in includes/functions_posting.php called create_thumbnail() that creates thumbnails of image attachments. Well, duh...that's exactly what is required to resize avatars! If phpBB 3.1 has no resource issue with resizing image attachments into thumbnails, then it has no issue with resizing avatars.

The following modification will do exactly what the phpBB 3.0 version did...replace the create_thumbnail() function with a version that supports avatars (retains original aspect ratio of image), and add some code to the avatar upload function to use create_thumbnail() to resize the uploadced avatar if required.

So here's how to do that. Open phpbb/avatar/driver/upload.php and find the following line:

		$file->clean_filename('avatar', $prefix, $row['id']);

directly above it, add the following:

//	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

Save the file. Now, open includes/functions_posting.php and locate the create_thumbnail() function. It begins with this:

/**
* Create Thumbnail
*/
function create_thumbnail($source, $destination, $mimetype)
{

and ends with this:

	phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);

return true;
}

Replace the entire function with the following:

/**
* 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);
}

Save the file. And that's it! Your phpBB 3.1 installation will now support resizing of uploaded avatars. You still need to make some changes in the ACP; I suggest you read the phpBB 3.0 version of this modification and use the suggestions from that topic.

Enjoy!
φ
Posts: 1571
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 356
3
#2
Server-side resize of uploaded avatars
Posted October 31st 2014, 9:24pm
To make installation simpler, here is the avatar resize feature bundled as a MOD. It can be installed with AutoMOD 3.1.

avatar_resize.zip
Avatar_Resize for AutoMOD 3.1
28.42 KiB  |  Downloaded 742 times
φ
Posts: 1571
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 356
Server-side resize of uploaded avatars
Posted May 10th 2015, 5:22pm
@Dion:

cheers!
thanks you
φ
Posts: 9
Joined: August 26th 2014, 2:17pm
Server-side resize of uploaded avatars
Posted May 10th 2015, 8:26pm
@Dion

Can it resize attachment too?
φ
Posts: 9
Joined: August 26th 2014, 2:17pm
Server-side resize of uploaded avatars
Posted August 21st 2016, 8:59pm
Thanks a lot for this. I was looking for an avatar auto resize.

Btw is it possible to put the avatar selection on registration page?
φ
Posts: 12
Joined: November 16th 2015, 9:18am
Likes Given: 5
Server-side resize of uploaded avatars
Posted August 22nd 2016, 12:39am
I'm sure it's possible, but the only product I'm associated with where I offer any type of support for phpBB 3.1+ is BridgeDD PRO. And that support is specific to BridgeDD PRO.

I think you'll have better luck asking on phpbb.com.
φ
Posts: 1571
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 356
Server-side resize of uploaded avatars
Posted January 16th 2017, 7:43am
Nice hack, Dion!

But I have a question: is there a way to make image quality better? I noticed that after sending an image it is blurry, blurred ...

And this happens even with images that are not resized, but sent from the computer.
φ
Posts: 5
Joined: December 21st 2016, 11:08pm
Likes Given: 2
Server-side resize of uploaded avatars
Posted January 29th 2017, 12:48pm
I tried it with phpBB 3.2 and I think it could work but im getting a php error... maybe someone could try it too :)

This is the error:
Fatal error: Uncaught Error: Cannot access protected property phpbb\files\filespec::$width in .../phpbb/avatar/driver/upload.php:164 Stack trace: #0 ... /includes/ucp/ucp_profile.php(655): phpbb\avatar\driver\upload->process_form(Object(phpbb\request\request), Object(phpbb\template\twig\twig), Object(phpbb\user), Array, Array) #1 ... /includes/functions_module.php(676): ucp_profile->main('profile', 'avatar') #2 ... /ucp.php(369): p_master->load_active() #3 {main} thrown in ... /phpbb/avatar/driver/upload.php on line 164

Hopefully someone can help ;)
φ
Posts: 5
Joined: January 29th 2017, 12:44pm
Server-side resize of uploaded avatars
Posted January 29th 2017, 7:54pm
lucasivnni said:
Nice hack, Dion!

But I have a question: is there a way to make image quality better? I noticed that after sending an image it is blurry, blurred ...

And this happens even with images that are not resized, but sent from the computer.

If you're seeing blurred images that weren't resized, then the issue is in your server, your local system, or your browser. I suppose it could also be a CSS issue if you're resizing images.
φ
Posts: 1571
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 356
Server-side resize of uploaded avatars
Posted January 29th 2017, 8:01pm
longcat said:
I tried it with phpBB 3.2 and I think it could work but im getting a php error... maybe someone could try it too :)

It astounds me that, after all this time, phpBB still does not have the ability to resize uploaded avatars. It's a basic function available in all other board software.

Unlike some of the things I did with phpBB 3.1, I have no intention of supporting anything in phpBB 3.2. Perhaps you can ask one of the phpBB devs how to modify the contents of the protected variables in the phpbb\files\filespec class. Maybe you'll catch one in a good mood and you'll get an answer without being ridiculed.
φ
Posts: 1571
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 356
Server-side resize of uploaded avatars
Posted January 30th 2017, 1:12am
Thank you for the fast reply ;)

Yea same here... I don't know why they aren't supporting the resize of avatars yet...

I will try to catch a Dev but I think since they haven't implimented the function yet they also won't help me with that. But who knows^^ It's worth a try :P
φ
Posts: 5
Joined: January 29th 2017, 12:44pm
Server-side resize of uploaded avatars
Posted January 30th 2017, 8:29am
I've submit some request to that but phpBB team ignored it. They don't want to do it, and I don't know why. Maybe they're prioritizing things and they don't want to add stuff till all major problems are fixed, its the only thing that would make any sense.
Right now I'm using BBAvatarResize Extension from sitesplat forum. Its a huge improvement from the default phpBB one. Its basically similar to this one: https://www.phpbb.com/community/viewtop ... &t=2343741, but way simpler and better.

Hope that this can help you.
φ
Posts: 12
Joined: November 16th 2015, 9:18am
Likes Given: 5
Server-side resize of uploaded avatars
Posted January 30th 2017, 5:23pm
Tarantino said:
I've submit some request to that but phpBB team ignored it. They don't want to do it, and I don't know why. Maybe they're prioritizing things and they don't want to add stuff till all major problems are fixed, its the only thing that would make any sense.

Here's another thing that would make sense: the devs don't want to add features to the phpBB core because it lets them add features to user sites as paid jobs. I'm not saying that's the reason why so many basic features have been left out of the phpBB core in 3.1 and 3.2. However, I can't help but notice the number of phpBB staff that now have "contact me for paid development work" signatures.
φ
Posts: 1571
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 356
Server-side resize of uploaded avatars
Posted January 31st 2017, 4:16am
Tarantino said:
Right now I'm using BBAvatarResize Extension from sitesplat forum. Its a huge improvement from the default phpBB one. Its basically similar to this one: https://www.phpbb.com/community/viewtop ... &t=2343741, but way simpler and better.


I saw that extension and I also would use it but you have to buy the BOOTS theme to buy/download it...
Don't really want to buy a whole theme just to be able to download one extension.
φ
Posts: 5
Joined: January 29th 2017, 12:44pm
Server-side resize of uploaded avatars
Posted January 31st 2017, 6:24pm
If you ask to any Dev to create that extension it will cost you more then BBOOTS+Extension.
So... you need to think how much is worth the extension. ;)
φ
Posts: 12
Joined: November 16th 2015, 9:18am
Likes Given: 5
1
#16
Server-side resize of uploaded avatars
Posted January 31st 2017, 11:16pm
I changed my mind. :) Here's how to make avatar resizing work in phpBB 3.2. Open includes/functions_posting.php and locate the create_thumbnail() function. Replace the entire function with the following:

/**
* 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) {
global $phpbb_filesystem;

try
{
$phpbb_filesystem->phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
}
catch (\phpbb\filesystem\exception\filesystem_exception $e)
{
// Do nothing
}
}
return array($new_width, $new_height);
}

Save the file. Next, open the phpbb/avatar/driver/upload.php file and locate the following line:

		$file->clean_filename('avatar', $prefix, $row['id']);

Replace this line with the following:

//	MODIFICATION BY DION DESIGNS
$upload->set_allowed_dimensions($this->config['avatar_min_width'], $this->config['avatar_min_height'], 65535, 65535);
// END MODIFICATION

$file->clean_filename('avatar', $prefix, $row['id']);

// 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']);
$width = $file->get('width');
$height= $file->get('height');
if ($result) {
list($width, $height) = $result;
}
// END MODIFICATION

Finally, a few lines below what you just replaced, you'll find the following block of code:

		return array(
'avatar' => $row['id'] . '_' . time() . '.' . $file->get('extension'),
'avatar_width' => $file->get('width'),
'avatar_height' => $file->get('height'),
);

Change it to the following:

		return array(
'avatar' => $row['id'] . '_' . time() . '.' . $file->get('extension'),
'avatar_width' => $width,
'avatar_height' => $height,
);

Uploaded avatars should now be automatically resized. :)
φ
Posts: 1571
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 356
Server-side resize of uploaded avatars
Posted February 3rd 2017, 9:07am
I tried it and it's working ;)

Thank you very much m8 :D

Should I also change the maximum bytes of the avatar as you wrote at the 3.0 tutorial?
φ
Posts: 5
Joined: January 29th 2017, 12:44pm
Server-side resize of uploaded avatars
Posted February 3rd 2017, 10:36am
I recommend setting the max avatar upload size to 262144 (256KB). That will allow sufficient space for most legitimate uploads, but block the morons who try to upload 10-megapixel images for avatars. :)
φ
Posts: 1571
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 356
Server-side resize of uploaded avatars
Posted February 4th 2017, 2:27am
Dion said:
I recommend setting the max avatar upload size to 262144 (256KB). That will allow sufficient space for most legitimate uploads, but block the morons who try to upload 10-megapixel images for avatars. :)


Alright thank you ;)
φ
Posts: 5
Joined: January 29th 2017, 12:44pm
Post a reply
19 posts

Who is online

Users browsing this forum: No registered users and 0 guests