Server side image resize on upload

Looking for a MOD? Have a MOD request? Post here for help. (Note: This forum is community supported; phpBB does not have official MOD authors)
Ideas Centre
sp3ctre
Registered User
Posts: 307
Joined: Sun Jul 15, 2007 11:06 am

Server side image resize on upload

Post by sp3ctre »

I have been looking for ages for a mod that will take the users 1940x1150 (or whatever) image, upload it, then resize the original image to match the maximum (800x600) image size on the forum.

All the mods I see keep the original image and either make a thumbnail or scale it client-side.

The reason I want this is the disk space my forum is taking up is huge now, all full of these massive images.

I realise I could prevent large images being posted, but some of these users don't know how to resize, and would have a tough job learning.

Is there anything out there that I have missed, or could someone point me in the right direction of how to do it?

Thanks,

Jim
Kragothius
Registered User
Posts: 5
Joined: Tue Apr 14, 2009 8:44 pm

Re: Server side image resize on upload

Post by Kragothius »

Thats exactly what i´m also searching for.
I´m not good in coding, but maybe it is possible to modify the functions_upload.php file to resize an image to a specified size while uploading using the GD or Imagemagick functions.

I would be very glad if anyone has an idea how to handle this

best regards

Dirk
blackbird.rus
Registered User
Posts: 2
Joined: Thu Apr 02, 2009 8:51 am

Re: Server side image resize on upload

Post by blackbird.rus »

functions_upload.php
Add function

Code: Select all

	
/**
* Resize images with the true diemensions (800*600)
*/
function resize_images()
{
	if ( $this->is_image() ) {
	$limite_largeur = "800px";
	$limite_hauteur = "600px";
	$size = getimagesize($this->destination_file);
	$largeur = $size[0];
	$hauteur = $size[1];
	if($hauteur > $limite_hauteur OR $largeur > $limite_largeur)
	{
	if($largeur > $limite_largeur)
	{
	$hauteur = $hauteur / ($largeur / $limite_largeur);
	$largeur = $limite_largeur;
	}
	if($hauteur > $limite_hauteur)
	{
	$largeur = $largeur / ($hauteur / $limite_hauteur);
	$hauteur = $limite_hauteur;
	}
	
	$destination = imagecreatetruecolor($largeur, $hauteur);
	if ( $this->extension == "jpg" || $this->extension == "jpeg" ) $source = imagecreatefromjpeg($this->destination_file);
	elseif ( $this->extension == "png" ) $source = imagecreatefrompng($this->destination_file);
	elseif ( $this->extension == "gif" ) $source = imagecreatefromgif($this->destination_file);
				
	imagecopyresampled($destination, $source, 0, 0, 0, 0, $largeur, $hauteur, $size[0], $size[1]);
	if ( $this->extension == "jpg" || $this->extension == "jpeg" ) imagejpeg($destination, $this->destination_file);
	elseif ( $this->extension == "png" ) imagepng($destination, $this->destination_file);
	elseif ( $this->extension == "gif" ) imagegif($destination, $this->destination_file);
	}
}
}
in function "move_file" add

Code: Select all

	if ( $this->is_image() ) 
	{
		$this->resize_images();
	}
befor

Code: Select all

	phpbb_chmod($this->destination_file, $chmod); 
sp3ctre
Registered User
Posts: 307
Joined: Sun Jul 15, 2007 11:06 am

Re: Server side image resize on upload

Post by sp3ctre »

Nice work, I will try this tonight! Thank you very much for this!
sp3ctre
Registered User
Posts: 307
Joined: Sun Jul 15, 2007 11:06 am

Re: Server side image resize on upload

Post by sp3ctre »

One question... does this end up with the correct image sizes in phpBB? At the moment, my crude hacked version resizes the image but phpBB still thinks the size of the image is that of the original image, so in the control panel it tells me it is using a lot more space than it really is.
Kragothius
Registered User
Posts: 5
Joined: Tue Apr 14, 2009 8:44 pm

Re: Server side image resize on upload

Post by Kragothius »

I just added the function to my Forum and it works very fine for me.
Also the size of the Uploaded Files is correct and does not tell something else.

Good work :!:
JimM68
Registered User
Posts: 47
Joined: Mon Dec 21, 2009 2:54 am

Re: Server side image resize on upload

Post by JimM68 »

I was just pointed this way, this lil change seems like just what I'm looking for.

Does anyone have anything further on this?

The first change, where it says "Add function" does that need to go any particular place in the file?
JimM68
Registered User
Posts: 47
Joined: Mon Dec 21, 2009 2:54 am

Re: Server side image resize on upload

Post by JimM68 »

well, I'm getting an error.

Fatal error: Call to undefined function: resize_image() in /home/ragtop68/public_html/phpbb/includes/functions_upload.php on line 352

It appears that I have the:

if ( $this->is_image() )
{
$this->resize_images();
}

right, but it isn't recognizing the new function??

/**
* Resize images with the true diemensions (800*600)
*/
function resize_images()
{
if ( $this->is_image() ) {
$limite_largeur = "800px";
$limite_hauteur = "600px";

Anyone see what I'm doing wrong?
JimM68
Registered User
Posts: 47
Joined: Mon Dec 21, 2009 2:54 am

Re: Server side image resize on upload

Post by JimM68 »

nobody??
lbowenc
Registered User
Posts: 5
Joined: Wed Apr 07, 2010 2:57 am

Re: Server side image resize on upload

Post by lbowenc »

I had the same problem. The instructions for inserting the resize_images() function should say to put it within the filespec class which is in the first half of the file. If you put it at the end like I did, then it will not be found since the call to it expects it to be defined as part of filespec.

Bowen
User avatar
4_seven
I've Been Banned!
Posts: 5155
Joined: Wed Apr 30, 2008 1:41 am

Re: Server side image resize on upload

Post by 4_seven »

Current Mods | Mod Base | php(BB) programming | No help via PM
JimM68
Registered User
Posts: 47
Joined: Mon Dec 21, 2009 2:54 am

Re: Server side image resize on upload

Post by JimM68 »

lbowenc wrote:I had the same problem. The instructions for inserting the resize_images() function should say to put it within the filespec class which is in the first half of the file. If you put it at the end like I did, then it will not be found since the call to it expects it to be defined as part of filespec.

Bowen
That's what I did too.
The writer, 4_seven, fixed it for me (thanks!) but didn't tell me how or why.

It is an extremely slick mod, and works great. There is a limit to how big a submitted picture can be, somewhere between 1 and 2 megs, this is a server limitation I think, and big files take some time to process, but it will shrink a meg file to 70k no sweat.
User avatar
4_seven
I've Been Banned!
Posts: 5155
Joined: Wed Apr 30, 2008 1:41 am

Re: Server side image resize on upload

Post by 4_seven »

open

.htaccess (in forum root)

add

Code: Select all

php_value memory_limit 128M
now u can upload ~ 3000x3000 px images

note: for server limitation by resizing an image only the px is interesting, not the MB of an image ;)
Current Mods | Mod Base | php(BB) programming | No help via PM
faulksie47
Registered User
Posts: 78
Joined: Sat Dec 25, 2010 10:11 am
Location: Reading UK and France

Re: Server side image resize on upload

Post by faulksie47 »

have tried adding this to my phpbb 3.09 forum and works great, but i get this debug message and the screen size text changes to very large before submt, but after, goes to to correct size text ..

debug message =

[phpBB Debug] PHP Notice: in file /includes/functions_upload.php on line 288: A non well formed numeric value encountered
[phpBB Debug] PHP Notice: in file /includes/functions_upload.php on line 293: A non well formed numeric value encountered

line 288 = $destination = imagecreatetruecolor($largeur, $hauteur);

line 293 = imagecopyresampled($destination, $source, 0, 0, 0, 0, $largeur, $hauteur, $size[0], $size[1]);

could anyone help please ?
User avatar
4_seven
I've Been Banned!
Posts: 5155
Joined: Wed Apr 30, 2008 1:41 am

Re: Server side image resize on upload

Post by 4_seven »

How does this in your part look like:

Code: Select all

       $limite_largeur = "800px";
       $limite_hauteur = "600px"; 
Current Mods | Mod Base | php(BB) programming | No help via PM
Locked

Return to “[3.0.x] MOD Requests”