jpeg optimize and make progressive

Anyone got a handy script that will run through an image folder optimizing the images and converting them to progressive?
Using GD
LVL 58
GaryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
This does the 'progressive' part: http://www.php.net/manual/en/function.imageinterlace.php  What are you going to 'optimize' them for?
0
Ray PaseurCommented:
I've never tried the "interlace" idea, but I know that when you use ImageJPEG() you can set the $quality option to reduce the file size.

Internet connections are very fast today.  But here is how you might do it.

1. Scandir() to collect the filenames
2. Iterator like foreach() to look for image files
3. ImageCreateXXX() to choose the images
4. ImageInterlace() to make them progressive
5. ImageJPEG() to store the interlaced image files

I think I would use a different output image directory and then you could look at the files side-by-side to compare file sizes and visual quality.
0
GaryAuthor Commented:
I'm just being lazy, I do have progressive script somewhere - cannot find it at the mo.
Basically looking for a script that will go thru a folder with about 4000 images, optimize their size - quite a few (well most of them) are not optimized i.e. I could halve if not more their size - and save them as progressive jpg's at the same time.
Running from a console so timeout's is not a problem.
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Slick812Commented:
Hi GaryC123, this web image file size "Make Smaller" PROBLEM is older than the internet web and older than the very idea of digital images and file size, so you are asking a common question that has been asked thousands of times and partially  solved for jpg images in thousands of ways, in PHP and many other setups! ! You need to be more informed about JPG images and their levels of file compression, , , especially the trade offs necessary for file size reduction for any digital image.
I just did a web search for "php jpg image optimizer" and got  14,700,000 results!

Maybe you could tell us about why you think you need more image compression, and some of the factors you see in the 4000 images you have, , to make you think about mass "optimize and make progressive" for them.   As it is, you do not give enough information to give you an answer that narrows down the many options available to us for you about the  half the size thing.
0
GaryAuthor Commented:
None of the images are saved for the web - they need optimizing - simple.
Quality taken down to about 80%

Thought someone might have a ready made script handy to save me time writing it.
0
Ray PaseurCommented:
Maybe I can tinker around with this.  Please clarify... Do you want to reduce the image size at all, or keep the same pixel dimensions?  And are you willing to accept the fact that progressive JPGs may be larger than non-progressive JPGs of equal or even lower resolution?
0
GaryAuthor Commented:
Same size - no resizing in dimensions.
A lot of the images are around 100kb and could be optimized to around 70-80kb without any loss in quality.
Yes I know the progressive may increase the size (a few kb) but that's why I want to optimize as well before applying the progressive.
0
GaryAuthor Commented:
Attached is an example image 187kb, saved optimized and progressive takes it down to 33kb (using Photoshop).
I could setup a batch script in PS but then I would need to reupload all the images again
e22669.jpg
0
GaryAuthor Commented:
I actually reduced that image to 30%
The image I attached was the optimized one, reattaching the original.
You will see there is no discernible difference in quality but it is a sixth of the size.
e22669--2-.jpg
0
Ray PaseurCommented:
Try this
<?php // RAY_temp_garyc123.php 2013-10-11
error_reporting(E_ALL);
echo '<pre>';

// PROCESS IMAGES TO INTERLACE AND SAVE AT REDUCED QUALITY
// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28264157.html


// QUALITY VALUE
$qv  = 80;

// INPUT AND OUTPUT DIRECTORIES
$inp = 'RAY_temp_garyc123_input';
$out = 'RAY_temp_garyc123_outgo';

// FIND THE UNPROCESSED FILES
$undone = scandir($inp);
$finish = scandir($out);
$predun = array_intersect($undone, $finish);
$needed = array_diff($undone, $finish);

// PLANNED WORK
print_r($predun);
print_r($needed);

// WITH EACH FILE
foreach ($needed as $file)
{
    // RELATIVE PATHS
    $raw = $inp . DIRECTORY_SEPARATOR . $file;
    $dun = $out . DIRECTORY_SEPARATOR . $file;

    // CREATE THE COPY
    $img = imageCreateFromJPEG($raw);
    imageInterlace($img, TRUE);
    imageJPEG($img, $dun, $qv);

    echo PHP_EOL
    . 'From: <a target="raw" href="'
    . $raw
    . '">'
    . $file
    . '</a>'
    . ' To: <a target="dun" href="'
    . $dun
    . '">'
    . "processed $file"
    . '</a>'
    ;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GaryAuthor Commented:
Yeah that seems to work great - thanks.
0
Ray PaseurCommented:
J' Welcome!  Thanks for the points, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.