can i optmize image to 50% quality before uploading 2 server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryanfern86goa
Forum Newbie
Posts: 16
Joined: Tue Sep 22, 2009 5:26 pm

can i optmize image to 50% quality before uploading 2 server

Post by ryanfern86goa »

Hi there

i have jus written a php program to upload and display images but when i try to upload them it takes a long time to upload if the file is big. But the big problem is when i try to display them. The time taken to display them is huge. so i was just wondering if its posible to optimize a image before uploading it to the server and also save some bandwidth. can i do this directly from the upload form for jpeg image

$name=$_FILES['file']['name'];
$tmpname=$_FILES['file']['tmp_name'];
$path=images/$name;
$tmp=imagecreatefromjpeg($tmpname);
imagejpeg($tmp,$path,50);

Any comments would be grately be appreciated.

thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: can i optmize image to 50% quality before uploading 2 server

Post by pickle »

Using HTML/Javascript/PHP, there is no way to optimize the image before uploading. To do that you'll need a Java applet or Flash file. SWFUpload provides just that functionality in their latest beta.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
ryanfern86goa
Forum Newbie
Posts: 16
Joined: Tue Sep 22, 2009 5:26 pm

Re: can i optmize image to 50% quality before uploading 2 server

Post by ryanfern86goa »

hi pickle

thanks for your reply...

appreciated...
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: can i optmize image to 50% quality before uploading 2 server

Post by SimpleManWeb »

You can however optimize it AFTER you upload it which means that at least your images will load faster on your site. You need to have the GD library installed on your server, which most hosting companies already provide.

Your code was close, try this:

Code: Select all

 
        $Directory = "../Images/ImageName.jpg";
        $Quality = 50;
        $Image = imagecreatefromjpeg($Directory);
    imagejpeg($Image, $Directory, $Quality);
 
I didn't test this code, but it should work.

Hope this helps
Post Reply