Jump to content

Need help with script that copies JPG to different quality settings


mreish

Recommended Posts

 I'm trying to write a script to demenostrate JPG lossy compression. The idea is to start with an image called copy-0000.jpg that is at 100% quality then save it 100 times at 50 quality renaming the copies as it goes. So each copy will be half the quality of the previous.

$j = 0;
for ($i = 0; $i < 100; $i ++) {
	$j ++;
	$fileToCompress = imagecreatefromjpeg('copy-' . sprintf('%04d',$i)  . '.jpg');
	imagejpeg($fileToCompress, 'copy-' . sprintf('%04d',$j)  . '.jpg', 50);
	imagedestroy($fileToCompress);
}

But I wind up with 100 copies of the original, each at 50 quality.

 

I am open for ideas.

Link to comment
Share on other sites

 

But I wind up with 100 copies of the original, each at 50 quality.

That is what you said you want to do.

 

then save it 100 times at 50 quality renaming the copies as it goes

If you want to reduce the quality each time then don't hard-code the "50", put it in a variable and reduce the value in each loop

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.