Automatic file size reduction

This is a discussion on "Automatic file size reduction" within the PHP Forum section. This forum, and the thread "Automatic file size reduction are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Feb 12th, 2008, 09:41
Junior Member
Join Date: Aug 2007
Location: wakefield
Age: 25
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Automatic file size reduction

Hi

I have a web site that allows users to upload pictures but i only allow them a maximum of 500kb per picture. What would be nice if it can be done is that when they upload there pictures it will automatically reduce the file size to 500kb, can this be done? This would then save my advertisers time and inconvineience hence i might get more people advertising.

TY
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Feb 12th, 2008, 10:17
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Automatic file size reduction

The best way to do this would be to resize the image to a predetermined max height/width. You would be able to do this with the PHP GD module.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Feb 12th, 2008, 10:28
Junior Member
Join Date: Aug 2007
Location: wakefield
Age: 25
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Automatic file size reduction

Thanks, any resources you can point me to to achieve this. Or a working example wouild be great.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Feb 12th, 2008, 11:45
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Automatic file size reduction

Okay here's a quick example using a file found in $_FILES['pic']..

The error handling is tack as it's only an example

PHP: Select all



$allowed 
= array('jpg''jpeg''gif''png');

$picH '500'// maxium height
$picW '500'// maximum width
// first we'll check this is a valid file and go from there

if (!is_uploaded_file($_FILES['pic']['tmp_name']))
   die (
'You\'re naughty! Don\'t try hack me!');

// okay so its a valid file let's get the extension

$ext explode('.'$_FILES['pic']['name']);
$ext strtolower($ext[count($ext)-1]);

//check if file type is okay...
if (!in_array($ext$allowed))
    die (
'That file type is not allowed');

$ext $ext == 'jpg' 'jpeg' $ext// need to turn jpg to jpeg for later.

// let's get the uploaded images dimensions

$dim getimagesize($_FILES['pic']['tmp_name']); // gets an array of image dimenstions [0]= width [1] = height

// work by what percentage we would have to resize to for each

$ph ceil(($picH $dim[1]) * 100);
$pw ceil(($picW $dim[0]) * 100);

// okay, now, if the either dimension is greater than 100% of the allowed we need to recalculate

if ($ph 100 || $pw 100)
   
$rc $pc $pw $ph $pw 100 $ph 100// take the largest percentage decrease

$width ceil($dim[0] * $rc); // new width
$height ceil($dim[1] * $rc); // new height

// get rid of our working variables
unset($ph$pw);

// get the binary data from the old image
$func 'imagecreatefrom' $ext
$om 
$func($_FILES['pic']['tmp_name']);

// create the new image


$im imagecreatetruecolor($width$height);

// now resample the old image into the new one based on our new width and height

$im imagecopyresampled($im$ol0,0,0,0$width$height$dim[0], $dim[1]);

// now we'll store it ... as a jpg (but you can just replace imagejpg with image $ext

imagejpeg($im'C:/Path/to/where/you/want/' $_FILES['pic']['name']);


I don't work with images too often but that's a quick look at how I would do it... I went pretty quickly so the maths is a bit chunky
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)

Last edited by Rakuli; Feb 12th, 2008 at 15:29. Reason: syntax error in first and second ifs - and allowed
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Feb 12th, 2008, 12:07
Junior Member
Join Date: Aug 2007
Location: wakefield
Age: 25
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Automatic file size reduction

wow thanks thats a great example, wasnt expecting anything that good I will work on that and get it to work with my current site.
Thanks again
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to reduce file size of .jpg lmc148 Graphics and 3D 12 Jan 17th, 2008 07:43
Image file size help danny322 Graphics and 3D 6 Nov 9th, 2007 11:20
Reducing File (jpg) size in asp ish Classic ASP 2 Aug 1st, 2007 11:44
large php file size geoffmuskett PHP Forum 5 Jun 20th, 2007 14:54
Need help with E-Card file size amber Graphics and 3D 7 Jun 19th, 2007 06:24


All times are GMT. The time now is 19:25.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42