OllieMackJames Posted March 10, 2016 Share Posted March 10, 2016 I want to use optimus.io api for image compression. I am looking for some help for the following: I use thumbnails module from apeisa which still works fine, thanks again apeisa!!! After processwire does it's magic I end up with all image files in all sorts of different directories in /site/assets/files And these I want to compress... Optimus gives me the following php code to use to compress an image: <?php /** * PHP Test Client for the Optimus API * * optimus.io - Lossless compression and optimization of your images * * @author KeyCDN * @version 0.1 */ // load optimus lib require 'src/optimus.php'; // create object and pass API license key $optimus = new Optimus('<your_license_key>'); // optimize image $result = $optimus->optimize('img/102kb.jpg'); // save optimized image file_put_contents('opt/102kb.jpg', $result); What I would like help on is the following: How can I get something like the following coded: (I am not a coder, just guessing stuff) for each subdirectory in /site/assets/files get list of images then for each image in list $optimus = new Optimus('<your_license_key>'); // optimize image $result = $optimus->optimize('location/name.jpg'); // save optimized image file_put_contents('location/name.jpg', $result); Of course this will just overwrite the files, but I have no idea to do this otherwise so if anybody has a brilliant idea for that as well, that would even be better. What I can think of is: for each directory in /site/assets/files that does not have a subdirectory called optimized get list of images from subdirectory and subdirectory/optimized then for each image in list that does not already have an optimized version $optimus = new Optimus('<your_license_key>'); // optimize image $result = $optimus->optimize('location/name.jpg'); // save optimized image file_put_contents('optimized/name.jpg', $result); In this case optimized images get saved in a new directory, and optimization only takes place when optimized subdirectory does not exist already and image is not already optimized. Now I would need to on the fly change the location of all images in templates to have the /optimized/ subdirectory appended to the file url. All this together would then be a new module for lossless image compression using optimus.io So if anyone can help me forward, thanks! Link to comment Share on other sites More sharing options...
LostKobrakai Posted March 10, 2016 Share Posted March 10, 2016 You wouldn't want to optimize images multiple times. You'd rather want to hook into some point, where you you'd get the images, which are added successfully and just optimize those newly added ones. For details on which hook might be fitting you'd have to wait for other answers. 1 Link to comment Share on other sites More sharing options...
OllieMackJames Posted March 10, 2016 Author Share Posted March 10, 2016 You wouldn't want to optimize images multiple times. You'd rather want to hook into some point, where you you'd get the images, which are added successfully and just optimize those newly added ones. For details on which hook might be fitting you'd have to wait for other answers. Thanks for chiming in LostKobrakai, looking forward to info on how to hook etc! Link to comment Share on other sites More sharing options...
horst Posted March 10, 2016 Share Posted March 10, 2016 (edited) @OllieMackJames: Are you able to use PW 3.0.10 for your site, or do you need to stick with PW 2.7 ? If you can use PW 3, you simply may copy one of the ImageSizerEngines into your site modules directory, change the classname, and add the optimization functionality. To add the functionality, I highly would suggest to use an API image option, e.g. array("optimize" => true). This way you are able to control which variation should be passed to the optimisation service and which one not. At the end of the image manipulation, you can ask if optimize is set: if($this->optimize) { $optimus = new Optimus('<your_license_key>'); $result = $optimus->optimize($dstFilename); file_put_contents($dstFilename, $result); } That's all you need. You also may set the optimize default to true in site/config.php $config->imageSizerOptions = array(..., ..., ..., "optimize" => true)); and then explicitly disable it via individual API option settings in your template files. Just choose what fits best for your site. Edited March 10, 2016 by horst 4 Link to comment Share on other sites More sharing options...
OllieMackJames Posted March 10, 2016 Author Share Posted March 10, 2016 @horst, thanks! I can use PW3 so that should be ok. My son wrote the following code - we just tested this code standalone in a separate directory, and it works fine: <?php echo "starting - wait until you see the end message"; require_once 'src/optimus.php'; /** ------- Configuration variables for script: ------- */ $inputParentBaseDir = '../site/assets/files'; //base directory for non optimized images $outputParentBaseDir = '../site/assets/optimized'; //output directory for optimized images $imageFileExtensions = Array('jpeg', 'jpg', 'png'); $optimus = new Optimus('<Your Optimus Key here>'); /** --------------------------------------------------- */ /** Goes through all files in the main directory and all subdirectories and compresses the image files: */ $subDirectoryIterator = new RecursiveDirectoryIterator($inputParentBaseDir); $imagesOptimized = 0; $imagesSkipped = 0; foreach (new RecursiveIteratorIterator($subDirectoryIterator) as $fileName => $file) { /** Checks if the file is an image file: */ if (in_array(get_file_extension($fileName), $imageFileExtensions)) { /** Checks if an optimized version of the image file does not exist yet: */ $optimizedImagePath = str_replace_first_occurance($fileName, $inputParentBaseDir, $outputParentBaseDir); //echo 'Found an image file: ' . $fileName . '. Optimized path = ' . $optimizedImagePath . '<br>'; if (!file_exists($optimizedImagePath)) { if(!file_exists(dirname($optimizedImagePath))) mkdir(dirname($optimizedImagePath), 0777, true); /** Optimizes the image and stores the result in a different location: */ $optimizedImage = $optimus->optimize($fileName); file_put_contents($optimizedImagePath, $optimizedImage); $imagesOptimized++; } else $imagesSkipped++; } } echo 'Optimized ' . $imagesOptimized . ' images. Skipped ' . $imagesSkipped . ' images since they were already optimized.<br>'; function str_replace_first_occurance($haystack, $needle, $replace) { $pos = strpos($haystack, $needle); if ($pos !== false) return substr_replace($haystack, $replace, $pos, strlen($needle)); throw new Exception($needle . ' could not be found in ' . $haystack . '.'); } function get_file_extension($fileName) { $fileNameAsArray = explode('.', $fileName); return strtolower(array_pop($fileNameAsArray)); //gets the last part of the array (the extension) and returns it as lower case } ?> We tested this standalone on my server and it nicely compressed all images into the optimized directory, with all subdirectories. If anyone can tell me how to turn this into a module, that would be great. Of course now I need to figure out how to get PW to pull images from the /site/assets/optimized directories Anyone any ideas how to take this one step further. BTW, i now have all compressed and optimized images on the server in the directory /site/assets/optimized Lots of subdirectories and much smaller files. Thanks for the input so far! Further help much appreciated! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now