Image resizer

Just a testscript to test the quality of Magento’s image processing:

<?php
/**
 *
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * It is  available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 *
 *
 * @category   AskSheldon
 * @package    AskSheldon_FeaturedProducts
 * @copyright  Copyright (c) 2016 Marcel Lange (https://www.ask-sheldon.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Marcel Lange <info@ask-sheldon.com>
 *
 * Einfaches Testscript, um die quallität des Imageprocessings zu testen
 *
 * AskSheldon_SHELL_Imageprocessing
 *
 * 
 */
require_once("abstract.php");
 
class AskSheldon_SHELL_Imageprocessing extends Mage_Shell_Abstract {
 
 
    /**
     * @param $imagePath
     * @param $width
     * @param $height
     * @param bool $constrainOnly
     * @param bool $keepAspectRatio
     * @param bool $keepFrame
     * @param array $bgColor
     * @param int $quality
     *
     * Converts an image to jpg and copies it to target folder
     *
     */
    private function _convert($imagePath, $targetPath, $width, $height, $constrainOnly = true, $keepAspectRatio = true, $keepFrame = false, $bgColor = array(255, 255, 255), $quality = 80)
    {
        try {
            if (!is_file($imagePath)) {
                Mage::throwException($imagePath . ' does not exist!');
            }
            if (!is_array($bgColor)) {
                Mage::throwException('Wrong Background-Color-Format given');
            }
 
            $targetImagePath = dirname($targetPath);
            $this->log('New Image Foldername: ' . $targetImagePath);
            if (!is_dir($targetImagePath)) {
                if (!mkdir($targetImagePath, 0777)) {
                    Mage::throwException('Could not create damed folder: ' . $targetImagePath);
                };
            }
            //now use Magentos nice scaling featueres.
            $imageObj = new Varien_Image ($imagePath);
            $imageObj->backgroundColor($bgColor);
            $imageObj->constrainOnly($constrainOnly);
            $imageObj->keepAspectRatio($keepAspectRatio);
            $imageObj->keepFrame($keepFrame);
            $imageObj->quality($quality);
            $imageObj->resize($width, $height);
            $imageObj->save($targetPath);
            $this->log('New Image successfully build: ' . $targetPath);
            $this->log('Imagemimetype: ' . $imageObj->getMimeType());
        } catch (Exception $ex) {
            $this->log('Imageconvertererror: ' . $imagePath);
            $this->log('Imageconvertermessage:');
            $this->log($ex->getMessage());
            $this->log('Imageconvertertranestring:');
            $this->log($ex->getTraceAsString());
            throw $ex;
        }
 
    }
 
    /**
     * General log function for this nice module
     * @param string $content if empty only the timestamp ist logged
     */
    static function log($content = "")
    {
        Mage::log($content, null, __CLASS__ . '.log', true);
    }
 
    public function run()
    {
        ini_set('memory_limit','4096M');
        ini_set('display_errors', '1');
 
        echo "ARGS:\n";
        print_r($this->_args);
        echo "\n";
 
        if(!$this->getArg('file')
            || !$this->getArg('target')
            || !$this->getArg('width')
            || !$this->getArg('height')
            || !$this->getArg('constrainOnly')
            || !$this->getArg('keepAspectRatio')
            || !$this->getArg('keepFrame')
            || !$this->getArg('quality')
        ){
            echo $this->usageHelp()."\n";
            exit -1;
        }
 
 
 
        $this->_convert(
            $this->getArg('file'),
            $this->getArg('target'),
            $this->getArg('width'),
            $this->getArg('height'),
            $this->getArg('constrainOnly'),
            $this->getArg('keepAspectRatio'),
            $this->getArg('keepFrame'),
            array(255, 255, 255),
            $this->getArg('quality')
        );
        echo "Ready Master!!!\n";
    }
 
 
    /**
     * Retrieve Usage Help Message
     *
     */
    public function usageHelp()
    {
        return <<<USAGE
 
Please make shure you now what you're doing guy!!!
 
Usage:  php image_resize_test.php
            -file   pathToFileToProcess   |  path to file to process
            -target pathToFileToProcess   |  path to file to process
            -width                        |
            -height                       |
            -constrainOnly                |
            -keepAspectRatio              |
            -keepFrame                    |
            -quality                      |
            help                          |  display this help
 
USAGE;
    }
}
 
$shell = new AskSheldon_SHELL_Imageprocessing();
$shell->run();

 

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.