MyBB Community Forums

Full Version: Save resized JPEGs as PNGs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have so far been unable to get a plugin created for doing this, so have decided to just edit the core files.

What I want to do is to make it so that if someone uploads a JPEG avatar that needs resizing, instead of keeping its original format, producing JPEG artefacts, I'd like it to be stored as a good ol' PNG.

I've narrowed it down to inc/functions_image.php, and it would appear that making the following modification would work, but it doesn't:

case 2:
	@imagejpeg($thumbim, $path."/".$filename);
	break;
case 2:
	@imagepng($thumbim, $path."/".$filename);
	break;

Performing this modification causes any JPEG larger than my forum's maximum avatar resolution to return "The file upload failed. Please choose a valid file and try again."

Can someone perhaps take a closer look and figure out what's going wrong? I understand doing this will probably cause a file extension mismatch, but that can be fixed another day.

Edit: Moved from 1.8 Support to Plugin Development by the look of things. I mean, I'm not making a plugin, but sure.
Alright I think I solved it, took me like 2 hours to figure out MyBB's process of uploading and resizing but I understand it now. Firstly this is a core edit so it's always risky, but I tested it out and it seems to work on vanilla 1.8.10. That said, definitely back your stuff up just in case.
So first, add this new function somewhere in
inc/functions_image.php
:
/**
* Convert a jpeg file to a png if resizing is necessary to maintain quality.
* Requested: https://community.mybb.com/thread-207332
* @author fizz (https://community.mybb.com/user-36020.html)
* Use at your own risk!!
*
* @param string $file the full path to the original image
* @param string $path the directory path to where to save the new image
* @param string $newname the filename to save the new image as
* @return array new image info on success, false on failure
*/
function jpgToPng($file, $path, $newname)
{
	$r = array();

	$img = getimagesize($file);
	$type = $img[2];

	if($img[0] == 0 || $img[1] == 0)
		return false;

	if(@function_exists('imagecreatefromjpeg') && $type == 2)
		$jpg = @imagecreatefromjpeg($file);

	if(!$jpg)
		return false;

	// Create new file
	@imagepng($jpg, $path.'/'.$newname);

	// Delete old file
	@unlink($file);

	@imagedestroy($jpg);

	$r['filename'] = $newname;
	return $r;
}

This converts the uploaded jpg to png if the avatar needs resizing. Note, I didn't add a ton of error checking so you may want to add your own.

Next, in
inc/functions_upload.php
add this right after
require_once MYBB_ROOT."inc/functions_image.php";
around line 259 and BEFORE
$thumbnail = generate_thumbnail($avatarpath."/".$filename, $avatarpath, $filename, $maxheight, $maxwidth);
. It's important that this be in the right place or it won't work at all:
// Fizz edit for auto PNG conversion from JPG
				// https://community.mybb.com/thread-207332
				if(in_array($ext, array('jpg', 'jpeg', 'jpe')))
				{
					$pngname = 'avatar_'.$uid.'.png';
					$newfile = jpgToPng($avatarpath.'/'.$filename, $avatarpath, $pngname);
					if(isset($newfile['filename']))
					{
						$filename = $newfile['filename'];
						if(substr(strrchr($filename, '.'), 1) == 'png')
							$avatar['type'] = 'image/png';
					}
				}

And that should do it! Not sure how the quality turns out, like I said I only tested it minimally to ensure files were being saved as png's but it should work! Let me know if there's any problems or you have any questions.
(2017-01-12, 10:31 PM)fizz Wrote: [ -> ]Alright I think I solved it, took me like 2 hours to figure out the process of uploading and resizing but I understand it now. Firstly this is a core edit so it's always risky, but I tested it out and it seems to work on vanilla 1.8.10. That said, definitely back your stuff up just in case.

Now that is dedication! I've only just upgraded to 1.8.9 so, fortunately, I already have a recent backup of everything, except from posts made by members in the last two days or so and some CSS tweaks, but I can't see this modification wiping the entire server. XD

Thank you so much for doing this. I will update to 1.8.10 and make these changes at the same time this weekend and let you know how things go!

I'll probably make some sort of credits/thanks page the following week. Do you want to be credited under a particular name and have a link to a certain page?
(2017-01-13, 01:01 AM)The Switch Club Wrote: [ -> ]
(2017-01-12, 10:31 PM)fizz Wrote: [ -> ]Alright I think I solved it, took me like 2 hours to figure out the process of uploading and resizing but I understand it now. Firstly this is a core edit so it's always risky, but I tested it out and it seems to work on vanilla 1.8.10. That said, definitely back your stuff up just in case.

Now that is dedication! I've only just upgraded to 1.8.9 so, fortunately, I already have a recent backup of everything, except from posts made by members in the last two days or so and some CSS tweaks, but I can't see this modification wiping the entire server.  XD

Thank you so much for doing this. I will update to 1.8.10 and make these changes at the same time this weekend and let you know how things go!

I'll probably make some sort of credits/thanks page the following week. Do you want to be credited under a particular name and have a link to a certain page?
Nice! No problem, kept me occupied lol. Just credit fizz on the board here and link to my profile please, if you wanted to throw in a paypal donation link for me too that'd be cool but not necessary Big Grin !
(2017-01-24, 03:06 AM)fizz Wrote: [ -> ]Nice! No problem, kept me occupied lol. Just credit fizz on the board here and link to my profile please, if you wanted to throw in a paypal donation link for me too that'd be cool but not necessary Big Grin !

I'll give your profile a link then, once I set up the page, get 'round to upgrading MyBB and applying your code modifications. Busy times...