Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
added option to optimize Original images, too
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect committed Sep 27, 2017
1 parent d033230 commit 90f53ed
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
69 changes: 67 additions & 2 deletions Components/OptimusOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TinectOptimusOptimizer\Components;

use Shopware\Bundle\MediaBundle\Optimizer\OptimizerInterface;
use Shopware\Components\Plugin\CachedConfigReader;

/**
* Class OptimusOptimizer
Expand All @@ -20,15 +21,22 @@ class OptimusOptimizer implements OptimizerInterface
*/
private $rootDir;

/**
* @var CachedConfigReader
*/
private $cachedConfigReader;

/**
* OptimusOptimizer constructor.
* @param OptimusService $optimusService
* @param $rootDir
* @param CachedConfigReader $cachedConfigReader
*/
public function __construct(OptimusService $optimusService, $rootDir)
public function __construct(OptimusService $optimusService, $rootDir, CachedConfigReader $cachedConfigReader)
{
$this->optimusService = $optimusService;
$this->rootDir = $rootDir;
$this->cachedConfigReader = $cachedConfigReader;
}

/**
Expand Down Expand Up @@ -81,7 +89,7 @@ public function run($filepath)
'webp',
$filepath);
}

}

break;
Expand All @@ -90,6 +98,63 @@ public function run($filepath)
break;
}

$config = $this->cachedConfigReader->getByPluginName('TinectOptimusOptimizer');
if ($config['optimizeOriginal']) {
$this->optimizeOriginalFiles();
}

}


private function optimizeOriginalFiles()
{

//TODO: optimize code!
ignore_user_abort(true);
ini_set('max_execution_time', -1);
ini_set('memory_limit', -1);
set_time_limit(0);

$sql = "SELECT * FROM s_media where albumID<>-13 AND type='IMAGE' and path not like('%thumb_export%') and extension in('jpg','png') and userID<>-1 ORDER by id DESC /* LIMIT 0,20*/";

$mediaResource = \Shopware\Components\Api\Manager::getResource('media');

foreach (Shopware()->Db()->fetchAll($sql) as $media) {

$mediainfo = $mediaResource->getOne($media["id"]);
$path = explode("/media", $mediainfo["path"]);
$filepath = $this->rootDir . "/media" . $path[1];
$origFilesize = @filesize($filepath);
$masse = @getimagesize($filepath);
$breite = $masse[0];
$hoehe = $masse[1];

if ($origFilesize > 0) {

/*
* TODO: move this to optimusService
* + make images smaller automatically
*/
if (($origFilesize / 1024) < 5000 && $breite < 10000 && $hoehe < 10000 && $breite > 0 && $hoehe > 0) {


try {
$this->optimusService->optimize($filepath);
$filesize = @filesize($filepath);
Shopware()->Db()->query("UPDATE s_media SET file_size=" . $filesize . ",userID=-1 WHERE id=" . $media["id"]);

} catch (\Exception $e) {
}

}

}


}

return true;

}

/**
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Works great with [ShyimWebP](https://github.com/shyim/shyimwebp)

## future features:
* add license-key check
* option to optimize original image, too
* ...

## [Buy me a coffee](https://www.paypal.me/tinect/)
7 changes: 7 additions & 0 deletions Resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@
<label lang="de">Lizenzschlüssel</label>
<label>license key</label>
</element>

<element type="boolean">
<name>optimizeOriginal</name>
<label lang="de">Optimiere auch Originalbilder</label>
<label>optimize original images, too</label>
<value>true</value>
</element>
</elements>
</config>
1 change: 1 addition & 0 deletions Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<tag name="shopware_media.optimizer" priority="500"/>
<argument type="service" id="optimus_optimizer.service"/>
<argument type="string">%kernel.root_dir%/</argument>
<argument type="service" id="shopware.plugin.cached_config_reader"/>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<label lang="de">TinectOptimusOptimizer</label>
<label lang="en">TinectOptimusOptimizer</label>

<version>1.0.1</version>
<version>1.0.2</version>
<link>http://www.tinect.de/</link>
<author>Sebastian König</author>
<copyright>(c)</copyright>
Expand Down

0 comments on commit 90f53ed

Please sign in to comment.