Skip to content

Instantly share code, notes, and snippets.

@emilstahl
Created April 7, 2014 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilstahl/10020304 to your computer and use it in GitHub Desktop.
Save emilstahl/10020304 to your computer and use it in GitHub Desktop.
Resize WordPress wp-content images.
<?php
/*
Recursive Image Resizer (For Wordpress) 1.0
By Frands B. Hansen <fbh@helpsoft.dk>
Copyright (c) 2013, Frands B. Hansen
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the HelpSoft nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Set this to the max width images may have
$maxWidth = 1600;
// Set this to 1 if you want to output changes rather than doing them.
$dry_run = 0;
///////////////////////////////
//
// Do not touch anything below unless you know
// what you are doing!
//
///////////////////////////////
echo "<pre>";
$path = realpath($_SERVER['DOCUMENT_ROOT'].'/wp-content/');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
$filetypes = array("jpg", "jpeg", "png");
$i = 0;
foreach($objects as $name => $object){
$filetype = strtolower(end(explode(".", $name)));
if(in_array($filetype, $filetypes)) {
if($imgData = getimagesize($name)) {
if($imgData[0] > $maxWidth) {
$shortFilename = end(explode("/", $name));
$oldFilesize = filesize($name);
$width = $imgData[0];
$height = $imgData[1];
$ratio = $width / $height;
$newWidth = $maxWidth;
$newHeight = round($newWidth / $ratio);
$i++;
if(!$dry_run) {
switch($filetype) {
case 'jpg':
case 'jpeg':
$img = imagecreatefromjpeg($name);
$finalfunc = "imagejpeg";
break;
case 'png':
$img = imagecreatefrompng($name);
$finalfunc = "imagepng";
break;
default:
$img = false;
break;
}
$new_img = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($new_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$finalfunc($new_img, $name, 90);
$logtext = "[".date("Y-m-d H:i:s")."] Processed $name (From: $width"."x$height to $newWidth"."x$newHeight) \n";
file_put_contents("resizeimages.txt", $logtext, FILE_APPEND);
echo $logtext;
} else {
echo "Found $name Current size: $width x $height - New size: $newWidth x $newHeight \n";
}
}
}
}
if(!$dry_run) {
if($i == 1) die('<br /> <br />Processed image. Wait. <meta http-equiv="refresh" content="0">');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment