Automatic Resizing of Uploaded Avatars and Image Attachments
Posted September 6th 2012, 11:51pm
Note: Edited on 8/27/14 to include bug fix.

Here's something people have requested for years! The following modifications will allow your members to upload avatars of any size, and they will be automatically resized. Please read the last two paragraphs in this post before you install these modifications! For some reason the phpBB developers never added this feature, which is surprising, because they already wrote most of the code that is required to resize avatars and image attachments. The function that creates thumbnails for uploaded images in posts can be modified to resize avatars as well!

So here is the procedure. First, open the includes/functions_posting.php file and find the create_thumbnail() function. It starts with this code:

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

and ends with this code:

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

phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);

return true;
}

Replace the entire function with the following code:

/**
* Create Thumbnail
*/

function create_thumbnail($source, $destination, $mimetype, $new_width=0, $new_height=0) {
global $config;

if ($new_width == 0) {
$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;
}
}
else {
$destination = $source;
}

$dimension = @getimagesize($source);
if ($dimension === false) {
return false;
}

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

if ($new_width == 0) {
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;
}
phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
return array($new_width, $new_height);
}

Save the file. Now open the includes/functions_user.php file and find the following code:

	$prefix = $config['avatar_salt'] . '_';
$file->clean_filename('avatar', $prefix, $data['user_id']);

Replace this code with:

	$prefix = $config['avatar_salt'] . '_';

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

$file->clean_filename('avatar', $prefix, $data['user_id']);

And that's it! You now have the necessary code to allow automatic resizing of uploaded avatars. However, there are some important details you need to know.

First and foremost, image resizing is a memory-intensive procedure. The above code tries to increase your memory limit to allow images up to 1MB in filesize, and/or 1600x1200 in dimensions, to be resized. However, if your hosting company will not allow a PHP script to request additional memory, the maximum sizes are probably closer to 256K filesize and/or 720x540 dimensions. If your users complain about getting blank screens when attempting to upload avatars, the images they're trying to upload are too big.

Finally, here is how to configure the avatar settings in the ACP. The dimensions should be what you want for the displayed avatars -- meaning, you can leave these exactly where you have them now. However, you should increase the maximum file size based on what I wrote in the previous paragraph. Try setting the size to 999999 bytes, and if you receive too many complaints about blank screens, reduce it to 262144 bytes.

Enjoy your new feature!
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Automatic Resizing of Uploaded Avatars
Posted September 7th 2012, 1:43am
You might now be thinking, "Can this be used to resize image attachments?". Why, yes it can. :D

Please log in to view this content.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted December 12th 2012, 8:17pm
Avatar cropping would be awsome, but its too much to ask xD
φ
Posts: 23
Joined: September 25th 2012, 4:06pm
Likes Given: 14
Likes Received: 3
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted December 12th 2012, 8:53pm
balboag said:
Avatar cropping would be awsome, but its too much to ask xD

I never gave it any thought, but it could certainly be done. It would require a complete rewrite of the UCP avatar module and several changes to the _users table in the database, so don't expect it done anytime soon. ;)
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 8:26am
Hi. Sorry for my first post being a question..

I don't have an issue with oversized avatars but do require the attached image resizing.
Will this work if I just use that part of this mod?

Cheers.. E.
φ
Posts: 11
Joined: October 14th 2013, 7:43am
Location: Mid-Suffolk, UK
Likes Given: 2
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 8:50am
Yes, it will work fine for only image attachments. Follow the instructions in post #2 and don't modify includes/functions_user.php as described in post #1. If you want avatar image resizing at a later date, all you'll need to do is modify includes/functions_user.php.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 9:01am
Done that but there's a problem, tried uploading a large image and got a load of characters followed by these error messages:

[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4794: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions_posting.php:682)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4796: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions_posting.php:682)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4797: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions_posting.php:682)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4798: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions_posting.php:682)
φ
Posts: 11
Joined: October 14th 2013, 7:43am
Location: Mid-Suffolk, UK
Likes Given: 2
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 9:22am
That error means you have extraneous characters in the includes_functions_posting.php file on line 682. Make sure you're using a quality editor such as Notepad++, and then check your edits.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 9:31am
Thanks.
line 682 is nowhere near any edits I've made and I always use Notepad++ (after a series of facepalm moments with other editors)

This is the code around line 682.
Line 9 is the offending line (starting @passthru )

// 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;
}
}
φ
Posts: 11
Joined: October 14th 2013, 7:43am
Location: Mid-Suffolk, UK
Likes Given: 2
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 10:04am
You should have replaced the entire create_thumbnail() function, and the offending line is inside that function. However, at first glance I don't see anything in that code which would cause an error.

While I take a closer look, please comment out the code you quoted as follows:

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

That will hardcode the function to use GD to resize the image and should eliminate your error. If you don't have ImageMagick installed on your server, this is a permanent solution since the above code only pertains to ImageMagick.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 10:11am
Ok.. now I get an error when trying to post a reply (no reply box, just this error on a blank screen)

Parse error: syntax error, unexpected $end in /var/sites/o/oldschoolmtb.org.uk/public_html/forum/includes/functions_posting.php on line 2780

Line 2780 in that file is

?>
φ
Posts: 11
Joined: October 14th 2013, 7:43am
Location: Mid-Suffolk, UK
Likes Given: 2
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 10:45am
I think you should replace includes/functions_posting.php with an unmodified copy and make sure it works. Then please follow my instructions in post #6. You originally did not replace the create_thumbnail() function, and I have just verified that not replacing that function would cause the exact errors you mentioned in post #7.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 11:49am
I am running an unmodified file at the moment and it's working lovely.

**edited** re-read your posts.. sorry, will try and get it right this time..

Thanks for your time.. really appreciate it.
φ
Posts: 11
Joined: October 14th 2013, 7:43am
Location: Mid-Suffolk, UK
Likes Given: 2
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 12:17pm
Right.. gottit!!..

I think it's working.. the image uploads as originally, with the scrollbars, but when it's clicked it changes to the resized size (and back again when clicked).

Thanks Dion, sorry I wasted your time with my foolish error :oops:

Now to have a bash at coppermine (gawd help me)..
φ
Posts: 11
Joined: October 14th 2013, 7:43am
Location: Mid-Suffolk, UK
Likes Given: 2
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted October 14th 2013, 1:39pm
I'm glad everything is now working!

I've been meaning to start some topics about Coppermine Gallery. I managed to integrate parts of it into phpBB and planned to share those integration techniques. However, other projects have limited my ability to add many of the things I've wanted to add.

Please feel free to start a topic about Coppermine in the Server-Side Applications forum.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted December 29th 2013, 8:41pm
Hi Guys,
I'm looking for a means of lowering the bandwidth usage (and thus the cost!) of my wife's site.
At the moment we are serving full size images and then re-sizing them client-side using the Advanced BBCode Box 3 by mssti Mod which I suspect is a major contributor to our 40+ Gig per month transfer.
Ideally what I would like is that our users can upload whatever size images they have, normally these are directly off their cameras or phones, these images are re-sized server-side to whatever size is chosen and then displayed at that size in posts and should they wish to view a full size image they can click on the image or a link or whatever and the full size image is then downloaded and displayed in Advanced Box JS or similar in a Modal page.
Will the edits above perform these functions?

Cheers,
Bob
φ
Posts: 3
Joined: December 29th 2013, 5:46pm
Location: Rockhampton Australia
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted December 29th 2013, 9:31pm
Resizing client-side (with either what you're using, or with ReIMG) creates bandwidth problems, as well as pages which may take more than a minute to load if a user has a slow (less than 5Mbps) connection.

The modification here will provide server-side resizing to the maximum image size you have set in the ACP. The original image is lost; only the resized image (and optionally a thumbnail) will be saved.

If you want to save bandwidth, you should be creating and then displaying thumbnails (not the original image) in posts.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted December 29th 2013, 9:55pm
Dion said:
If you want to save bandwidth, you should be creating and then displaying thumbnails (not the original image) in posts.


I'm aware of that but unfortunately when the site was created did not expect the member numbers to grow to where they are today.
OK, so what of the images we already have, can they be resized using this hack?
φ
Posts: 3
Joined: December 29th 2013, 5:46pm
Location: Rockhampton Australia
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted December 30th 2013, 12:37am
Resizing already-saved attachments would require a custom program that reads the attachment files, resizes the images if required, and then saves the resized attachments.

How many images are we talking about here? A program such as this will take more than an hour to run if there are more than 15,000 images to resize, and it will cause quite a strain on the server while it's running. I suspect you would need to shut down the site to run the program.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Automatic Resizing of Uploaded Avatars and Image Attachments
Posted December 30th 2013, 3:34am
Just did a count and I have:
Disk space utilization = 2895 MB
Total file count = 7340
Any suggestions on a program/script that will do the job?
φ
Posts: 3
Joined: December 29th 2013, 5:46pm
Location: Rockhampton Australia

Who is online

Users browsing this forum: No registered users and 1 guest