Gossamer Forum
Home : Products : Gossamer Forum : Development, Plugins and Globals :

[HACK] Simple way to shrink images

Quote Reply
[HACK] Simple way to shrink images
Hi,

Recently I setup a site using GForum, where people were used to uploading huge images ... so ended up having to make a hack into /admin/GForum/Post/Write.pm, so that gif/jpg images would automatically be shrunk down to a more reasonable size.

Here is a quick how-to:

In /admin/GForum/Post/Write.pm, find sub attachment_upload {, and then in that function. find:
Code:
else {
$STASH{tpl_vars}->{attachment_uploaded} = $filename;
$STASH{tpl_vars}->{attachment_uploaded_id} = $sth->insert_id;
}

Change this to:

Code:
else {
$STASH{tpl_vars}->{attachment_uploaded} = $filename;
$STASH{tpl_vars}->{attachment_uploaded_id} = $sth->insert_id;

# Mod by andy..,
shrink_image($STASH{tpl_vars}->{attachment_uploaded_id});

}

Then add the below function :

Code:
sub shrink_image {

my $attachment_id = $_[0];

my $file_stats = $DB->table("TempAttachment")->select( { tempatt_id => $attachment_id } )->fetchrow_hashref;

# print $IN->header;
#use Data::Dumper;
#print Dumper($file_stats);

if ($file_stats->{tempatt_content} eq "image/gif" || $file_stats->{tempatt_content} eq "image/jpeg") {

my $fletter = ( reverse split //, $file_stats->{tempatt_id} )[0];

my $path = "$CFG->{admin_root_path}/attachments/temp/$fletter/$file_stats->{tempatt_id}";

use Image::Size;
my ($x,$y) = imgsize($path);

if ($x > 700 || $y > 700) {

my $command = qq~convert '$path' -resize 700x700 ${path}.new~;

`$command`;

my $command2 = qq|rm -f $path|;
`$command2`;

my $command3 = qq~mv ${path}.new $path~;
`$command3`;

my $size = -s $path;

my $attach = $DB->table('TempAttachment');

$attach->update( { tempatt_size => $size } , { tempatt_id => $attachment_id } ) || die $GT::SQL::error;
}

}


}

That should do it Smile

When you do an inline image, or attachment - it should shrink down the image to 700x700 max (assuming its larger than 700x700 :))

Enjoy!

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Subject Author Views Date
Thread [HACK] Simple way to shrink images Andy 7061 Jan 11, 2011, 12:25 AM
Thread Re: [Andy] [HACK] Simple way to shrink images
SandraR 6900 Jan 11, 2011, 6:34 AM
Thread Re: [SandraR] [HACK] Simple way to shrink images
Andy 6917 Jan 11, 2011, 7:10 AM
Thread Re: [Andy] [HACK] Simple way to shrink images
SandraR 6917 Jan 11, 2011, 7:36 AM
Post Re: [SandraR] [HACK] Simple way to shrink images
Andy 6916 Jan 11, 2011, 7:38 AM