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


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] [HACK] Simple way to shrink images In reply to
Thanks :)


Sandra Roussel
Chonsa Group Design - Fresh Start Housing
Quote Reply
Re: [SandraR] [HACK] Simple way to shrink images In reply to
Np ;)

I may make this into a plugin at some point (not sure what "hooks" we have in regards to the upload function - but if its possible, then it would be better than editing the core codes, due to upgrades etc)

Was just in a bit of a rush , so did a "dirty" hack Whistle

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] [HACK] Simple way to shrink images In reply to
Not to be pessimistic but I seriously doubt there will be anymore upgrades. Crazy


Sandra Roussel
Chonsa Group Design - Fresh Start Housing
Quote Reply
Re: [SandraR] [HACK] Simple way to shrink images In reply to
I hope you're wrong Whistle

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates