Mark H
Mark H

Reputation: 23

Degrading a jpg image quality each time a user visits a page?

I'm trying to work out the best way to degrade the quality of a jpeg image in a web page like this http://vimeo.com/3750507

Is there a server-side way of saving and re-saving at a lower quality then displaying an image each time the page is served?

Thanks in advance.

Upvotes: 1

Views: 114

Answers (1)

timclutton
timclutton

Reputation: 13014

The following script will degrade the original image by 1 'quality point' on each page load, starting at 99. This isn't very robust, but should clearly demonstrate the concept.

Warning: this will replace the original image! Use a copy if the original is important!

session_start();
if (empty($_SESSION['quality'])) $_SESSION['quality'] = 99;

$file = 'degrade.jpg';

imagejpeg(imagecreatefromjpeg($file), $file, $_SESSION['quality']);

$_SESSION['quality']--;

header('Content-type: image/jpeg');
echo file_get_contents($file);

On first page load:

first run

Tenth page load:

tenth run

One hundredth page load:

one hundredth run

Upvotes: 3

Related Questions