Version Notes
Add ability to resize old images
Download this release
Release Info
Developer | ImageRecycle |
Extension | ImageRecycle_image_pdf_optimize |
Version | 1.0.6 |
Comparing to | |
See all releases |
Code changes from version 1.0.5 to 1.0.6
app/code/community/Imagerecycle/Imagerecycle/Helper/Data.php
CHANGED
@@ -1,167 +1,160 @@
|
|
1 |
-
<?php
|
2 |
-
Class Imagerecycle_Imagerecycle_Helper_Data extends Mage_Core_Helper_Abstract{
|
3 |
-
|
4 |
-
private $allowed_ext = array('jpg', 'jpeg', 'png', 'gif','pdf');
|
5 |
-
public $settings = null;
|
6 |
-
|
7 |
-
public function getSettings() {
|
8 |
-
return array(
|
9 |
-
'api_key' => Mage::getStoreConfig('mageio_api_key'),
|
10 |
-
'api_secret' => Mage::getStoreConfig('mageio_api_secret'),
|
11 |
-
'installed_time' => Mage::getStoreConfig('mageio_installed_time'),
|
12 |
-
'exclude_folders' => Mage::getStoreConfig('mageio_exclude_folders'),
|
13 |
-
'resize_auto' => Mage::getStoreConfig('mageio_resize_auto'),
|
14 |
-
'resize_image' => Mage::getStoreConfig('mageio_resize_image'),
|
15 |
-
'min_size' => Mage::getStoreConfig('mageio_min_size'),
|
16 |
-
'max_size' => Mage::getStoreConfig('mageio_max_size'),
|
17 |
-
'compression_type_pdf' => Mage::getStoreConfig('mageio_compression_type_pdf'),
|
18 |
-
'compression_type_png' => Mage::getStoreConfig('mageio_compression_type_png'),
|
19 |
-
'compression_type_jpg' => Mage::getStoreConfig('mageio_compression_type_jpg'),
|
20 |
-
'compression_type_gif' => Mage::getStoreConfig('mageio_compression_type_gif'),
|
21 |
-
'compression_type' => Mage::getStoreConfig('mageio_compression_type'),
|
22 |
-
);
|
23 |
-
}
|
24 |
-
public function optimize($image, $savePath='') {
|
25 |
-
|
26 |
-
$response = new stdClass();
|
27 |
-
$response->status = false;
|
28 |
-
$response->msg = Mage::helper('imagerecycle')->__('Not be optimized yet');
|
29 |
-
$file = realpath($image);
|
30 |
-
if (!in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), $this->allowed_ext )) {
|
31 |
-
$response->msg = Mage::helper('imagerecycle')->__('This image type is not allowed');
|
32 |
-
return $response;
|
33 |
-
}
|
34 |
-
|
35 |
-
if (!file_exists($file)) {
|
36 |
-
$response->msg = Mage::helper('imagerecycle')->__('File not found');
|
37 |
-
return $response;
|
38 |
-
}
|
39 |
-
|
40 |
-
$resourceR = $this->getCoreRead('imagerecycle/images');
|
41 |
-
$resourceW = $this->getCoreWrite('imagerecycle/images');
|
42 |
-
if(!$this->settings) {
|
43 |
-
$this->settings = $this->getSettings();
|
44 |
-
}
|
45 |
-
$ext = array_pop( explode('.',$file));
|
46 |
-
$compressionType= $this->settings['compression_type_'.$ext];
|
47 |
-
if($compressionType=='none') return $response;
|
48 |
-
|
49 |
-
if (!$this->settings['api_key'] || !$this->settings['api_secret'] )
|
50 |
-
{
|
51 |
-
|
52 |
-
$response->msg = Mage::helper('imagerecycle')->__("You haven't configured Image recycle setting correctly yet.");
|
53 |
-
return $response;
|
54 |
-
}
|
55 |
-
$fparams = array("compression_type"=> $compressionType);
|
56 |
-
$resize_image = $this->settings['resize_image'];
|
57 |
-
$resize_auto = $this->settings['resize_auto'];
|
58 |
-
if($resize_image && $resize_auto) { //Only apply on new images
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
$
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
$
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
if
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
$
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
$
|
154 |
-
$
|
155 |
-
$
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
$resourceW = $resource->getConnection('core_write');
|
162 |
-
$tableName = $resource->getTableName($entityId);
|
163 |
-
$resourceW->tableName = $tableName;
|
164 |
-
return $resourceW;
|
165 |
-
}
|
166 |
-
|
167 |
-
}
|
1 |
+
<?php
|
2 |
+
Class Imagerecycle_Imagerecycle_Helper_Data extends Mage_Core_Helper_Abstract{
|
3 |
+
|
4 |
+
private $allowed_ext = array('jpg', 'jpeg', 'png', 'gif','pdf');
|
5 |
+
public $settings = null;
|
6 |
+
|
7 |
+
public function getSettings() {
|
8 |
+
return array(
|
9 |
+
'api_key' => Mage::getStoreConfig('mageio_api_key'),
|
10 |
+
'api_secret' => Mage::getStoreConfig('mageio_api_secret'),
|
11 |
+
'installed_time' => Mage::getStoreConfig('mageio_installed_time'),
|
12 |
+
'exclude_folders' => Mage::getStoreConfig('mageio_exclude_folders'),
|
13 |
+
'resize_auto' => Mage::getStoreConfig('mageio_resize_auto'),
|
14 |
+
'resize_image' => Mage::getStoreConfig('mageio_resize_image'),
|
15 |
+
'min_size' => Mage::getStoreConfig('mageio_min_size'),
|
16 |
+
'max_size' => Mage::getStoreConfig('mageio_max_size'),
|
17 |
+
'compression_type_pdf' => Mage::getStoreConfig('mageio_compression_type_pdf'),
|
18 |
+
'compression_type_png' => Mage::getStoreConfig('mageio_compression_type_png'),
|
19 |
+
'compression_type_jpg' => Mage::getStoreConfig('mageio_compression_type_jpg'),
|
20 |
+
'compression_type_gif' => Mage::getStoreConfig('mageio_compression_type_gif'),
|
21 |
+
'compression_type' => Mage::getStoreConfig('mageio_compression_type'),
|
22 |
+
);
|
23 |
+
}
|
24 |
+
public function optimize($image, $savePath='') {
|
25 |
+
|
26 |
+
$response = new stdClass();
|
27 |
+
$response->status = false;
|
28 |
+
$response->msg = Mage::helper('imagerecycle')->__('Not be optimized yet');
|
29 |
+
$file = realpath($image);
|
30 |
+
if (!in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), $this->allowed_ext )) {
|
31 |
+
$response->msg = Mage::helper('imagerecycle')->__('This image type is not allowed');
|
32 |
+
return $response;
|
33 |
+
}
|
34 |
+
|
35 |
+
if (!file_exists($file)) {
|
36 |
+
$response->msg = Mage::helper('imagerecycle')->__('File not found');
|
37 |
+
return $response;
|
38 |
+
}
|
39 |
+
|
40 |
+
$resourceR = $this->getCoreRead('imagerecycle/images');
|
41 |
+
$resourceW = $this->getCoreWrite('imagerecycle/images');
|
42 |
+
if(!$this->settings) {
|
43 |
+
$this->settings = $this->getSettings();
|
44 |
+
}
|
45 |
+
$ext = array_pop( explode('.',$file));
|
46 |
+
$compressionType= $this->settings['compression_type_'.$ext];
|
47 |
+
if($compressionType=='none') return $response;
|
48 |
+
|
49 |
+
if (!$this->settings['api_key'] || !$this->settings['api_secret'] )
|
50 |
+
{
|
51 |
+
|
52 |
+
$response->msg = Mage::helper('imagerecycle')->__("You haven't configured Image recycle setting correctly yet.");
|
53 |
+
return $response;
|
54 |
+
}
|
55 |
+
$fparams = array("compression_type"=> $compressionType);
|
56 |
+
$resize_image = $this->settings['resize_image'];
|
57 |
+
$resize_auto = $this->settings['resize_auto'];
|
58 |
+
if($resize_image && $resize_auto) { //Only apply on new images
|
59 |
+
|
60 |
+
$size = @getimagesize($file);
|
61 |
+
if($size && ($size[0]> $resize_image) ) {
|
62 |
+
$fparams['resize'] = array("width"=> $resize_image);
|
63 |
+
}
|
64 |
+
}
|
65 |
+
|
66 |
+
include_once(Mage::getModuleDir('', 'Imagerecycle_Imagerecycle') . '/classes/ioa.class.php');
|
67 |
+
$ioa = new ioaphp($this->settings['api_key'], $this->settings['api_secret']);
|
68 |
+
$return = $ioa->uploadFile($file,$fparams);
|
69 |
+
Mage::log($return);
|
70 |
+
|
71 |
+
if ($return === false || $return === null || is_string($return)) {
|
72 |
+
$response->msg = $ioa->getLastError();
|
73 |
+
return $response;
|
74 |
+
}
|
75 |
+
$md5 = md5_file($file);
|
76 |
+
clearstatcache();
|
77 |
+
$sizebefore = filesize($file);
|
78 |
+
|
79 |
+
$optimizedFileContent = $this->getContent($return->optimized_url);
|
80 |
+
if ($optimizedFileContent === false) {
|
81 |
+
$response->msg = Mage::helper('imagerecycle')->__("optimized url not found");
|
82 |
+
return $response;
|
83 |
+
}
|
84 |
+
|
85 |
+
if (file_put_contents($file, $optimizedFileContent) === false) {
|
86 |
+
$response->msg = Mage::helper('imagerecycle')->__("Download optimized image fail");
|
87 |
+
return $response;
|
88 |
+
}
|
89 |
+
clearstatcache();
|
90 |
+
$size_after = filesize($file);
|
91 |
+
|
92 |
+
if($savePath=='') { $savePath = $image;}
|
93 |
+
$id = $resourceW->fetchOne("SELECT id FROM {$resourceW->tableName} WHERE `file` = " . $resourceW->quote($savePath));
|
94 |
+
if (!$id) {
|
95 |
+
$resourceW->query("INSERT INTO `{$resourceW->tableName}` (`file`,`md5`,`api_id`,`size_before`, `size_after`,`date`) VALUES ("
|
96 |
+
. $resourceW->quote($savePath) . "," . $resourceW->quote($md5) . "," . $return->id . "," . (int) $sizebefore . "," . (int) $size_after . ", '" . date('Y-m-d H:i:s') . "' )");
|
97 |
+
} else {
|
98 |
+
$resourceW->query("UPDATE `{$resourceW->tableName}` SET `size_after` = " . (int) $size_after . " WHERE `id` = " . $id);
|
99 |
+
}
|
100 |
+
|
101 |
+
$response->status = true;
|
102 |
+
|
103 |
+
$response->msg = 'Optimized at '. round(($sizebefore-$size_after)/$sizebefore*100,2).'%';
|
104 |
+
$response->newSize = number_format($size_after/1000, 2, '.', '') ;
|
105 |
+
return $response;
|
106 |
+
}
|
107 |
+
|
108 |
+
public function checkOptimize($image) {
|
109 |
+
$resourceW = $this->getCoreWrite('imagerecycle/images');
|
110 |
+
$id = $resourceW->fetchOne("SELECT id FROM {$resourceW->tableName} WHERE `file` = " . $resourceW->quote($image));
|
111 |
+
return $id;
|
112 |
+
}
|
113 |
+
|
114 |
+
/**
|
115 |
+
* Get content of specified resource via curl or file_get_content() function
|
116 |
+
*/
|
117 |
+
protected function getContent($url) {
|
118 |
+
if ($url == '') {
|
119 |
+
return '';
|
120 |
+
}
|
121 |
+
|
122 |
+
if (!function_exists('curl_version')) {
|
123 |
+
if (!$content = @file_get_contents($url)) {
|
124 |
+
return '';
|
125 |
+
}
|
126 |
+
} else {
|
127 |
+
$options = array(
|
128 |
+
CURLOPT_RETURNTRANSFER => true, // return content
|
129 |
+
CURLOPT_FOLLOWLOCATION => true, // follow redirects
|
130 |
+
CURLOPT_AUTOREFERER => true, // set referer on redirect
|
131 |
+
CURLOPT_CONNECTTIMEOUT => 60, // timeout on connect
|
132 |
+
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
|
133 |
+
);
|
134 |
+
|
135 |
+
$ch = curl_init($url);
|
136 |
+
curl_setopt_array($ch, $options);
|
137 |
+
$content = curl_exec($ch);
|
138 |
+
curl_close($ch);
|
139 |
+
}
|
140 |
+
|
141 |
+
return $content;
|
142 |
+
}
|
143 |
+
|
144 |
+
public function getCoreRead($entityId) {
|
145 |
+
$resource = Mage::getSingleton('core/resource');
|
146 |
+
$resourceR = $resource->getConnection('core_read');
|
147 |
+
$tableName = $resource->getTableName($entityId);
|
148 |
+
$resourceR->tableName = $tableName;
|
149 |
+
return $resourceR;
|
150 |
+
}
|
151 |
+
|
152 |
+
public function getCoreWrite($entityId) {
|
153 |
+
$resource = Mage::getSingleton('core/resource');
|
154 |
+
$resourceW = $resource->getConnection('core_write');
|
155 |
+
$tableName = $resource->getTableName($entityId);
|
156 |
+
$resourceW->tableName = $tableName;
|
157 |
+
return $resourceW;
|
158 |
+
}
|
159 |
+
|
160 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/code/community/Imagerecycle/Imagerecycle/controllers/IndexController.php
CHANGED
@@ -35,6 +35,13 @@ Class Imagerecycle_Imagerecycle_IndexController extends Mage_Adminhtml_Controlle
|
|
35 |
$response->success = true;
|
36 |
}
|
37 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
$cache = Mage::getSingleton('core/cache');
|
39 |
$cache->flush();
|
40 |
$response->msg = "All modifications were saved!";
|
35 |
$response->success = true;
|
36 |
}
|
37 |
}
|
38 |
+
$installed_time = Mage::getStoreConfig('mageio_installed_time');
|
39 |
+
if(empty($installed_time)) {
|
40 |
+
$installed_time = time();
|
41 |
+
$coreConfig = Mage::getConfig();
|
42 |
+
$coreConfig->saveConfig('mageio_installed_time', $installed_time);
|
43 |
+
}
|
44 |
+
|
45 |
$cache = Mage::getSingleton('core/cache');
|
46 |
$cache->flush();
|
47 |
$response->msg = "All modifications were saved!";
|
package.xml
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
<?xml version="1.0"?>
|
2 |
<package>
|
3 |
<name>ImageRecycle_image_pdf_optimize</name>
|
4 |
-
<version>1.0.
|
5 |
<stability>stable</stability>
|
6 |
<license>GPL</license>
|
7 |
<channel>community</channel>
|
@@ -32,11 +32,11 @@ The jpg, png, gif optimization algorithm is capable of reducing the image size b
|
|
32 |
- Define a min-max size to optimize
|
33 |
- Optimize png, jpg, gif
|
34 |
- Define optimization quality per file type, jpg, png, gif</description>
|
35 |
-
<notes>
|
36 |
<authors><author><name>ImageRecycle</name><user>ImageRecycle</user><email>contact@imagerecycle.com</email></author></authors>
|
37 |
-
<date>2016-01-
|
38 |
-
<time>14:
|
39 |
-
<contents><target name="magecommunity"><dir name="Imagerecycle"><dir name="Imagerecycle"><dir name="Block"><file name="Images.php" hash="e87aada5017900e066c031a73f41c096"/></dir><dir name="Helper"><file name="Data.php" hash="
|
40 |
<compatible/>
|
41 |
<dependencies><required><php><min>5.3.0</min><max>5.6.0</max></php><extension><name>curl</name><min></min><max></max></extension></required></dependencies>
|
42 |
</package>
|
1 |
<?xml version="1.0"?>
|
2 |
<package>
|
3 |
<name>ImageRecycle_image_pdf_optimize</name>
|
4 |
+
<version>1.0.6</version>
|
5 |
<stability>stable</stability>
|
6 |
<license>GPL</license>
|
7 |
<channel>community</channel>
|
32 |
- Define a min-max size to optimize
|
33 |
- Optimize png, jpg, gif
|
34 |
- Define optimization quality per file type, jpg, png, gif</description>
|
35 |
+
<notes>Add ability to resize old images</notes>
|
36 |
<authors><author><name>ImageRecycle</name><user>ImageRecycle</user><email>contact@imagerecycle.com</email></author></authors>
|
37 |
+
<date>2016-01-26</date>
|
38 |
+
<time>13:14:07</time>
|
39 |
+
<contents><target name="magecommunity"><dir name="Imagerecycle"><dir name="Imagerecycle"><dir name="Block"><file name="Images.php" hash="e87aada5017900e066c031a73f41c096"/></dir><dir name="Helper"><file name="Data.php" hash="e3311eec7c749f39863537d9b6feaed2"/></dir><dir name="Model"><file name="Observer.php" hash="ecd7abe292e072f6c02badfb8815ba4b"/><dir name="Resource"><dir name="Images"><file name="Collection.php" hash="a8f7aba5403f8469959053630e509898"/></dir><file name="Images.php" hash="82c27d80a05aa3af8329100ad1ca2762"/><file name="Setup.php" hash="7950a28c598f21168a319fcbf7946583"/></dir></dir><dir name="classes"><file name="ioa.class.php" hash="a8f04430a71d4e91bbaff179f4cd6856"/></dir><dir name="controllers"><file name="IndexController.php" hash="c50e39d917942bbd943b8a76531a2f6a"/></dir><dir name="etc"><file name="adminhtml.xml" hash="66cb48702ff1456d6ca9fbff533f3ee4"/><file name="config.xml" hash="ab0471925aa5207520c96453dc34ad42"/></dir><dir name="sql"><dir name="imagerecycle_setup"><file name="mysql4-install-1.0.0.php" hash="e9113b9c19d91ed6cebd34180c90ff76"/></dir></dir></dir></dir></target><target name="mageweb"><dir name="js"><dir name="imagerecycle"><dir name="imagerecycle"><file name="bluebox.js" hash="36304b24f301b11d5abc23d6b5c1ef45"/><file name="jquery.min.js" hash="8101d596b2b8fa35fe3a634ea342d7c3"/><file name="script.js" hash="4a3fe3225f3bfc8c54ed591d19e970de"/></dir></dir></dir></target><target name="mageskin"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="imagerecycle"><dir name="imagerecycle"><file name="imagerecycle.css" hash="5c70db79a6e7ac29b0b1e9927ae6f31f"/><dir name="images"><file name="ajax-loader-tr.gif" hash="1ae32bc8232ff2527c627e5b38eb319a"/><file name="gift-message-collapse.gif" hash="5b710f5faa9c7352a39c10e3ebbee6fc"/><file name="gift-message-expand.gif" hash="fd53a70cbb1ea048754657a63787e9cb"/><file name="grouped_to_order_icon.png" hash="4bdae9a6199655027b8b8aeee8f80ce4"/><file name="i_question-mark.png" hash="23201b7ac207eebd9be0776ea44098a4"/><file name="pdf.png" hash="9c819141f3537f9f3ea181369332be85"/></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Imagerecycle_ImageRecycle.xml" hash="a20d14dfe71ec27c3f048cbb2eb03d1d"/></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="layout"><file name="imagerecycle_imagerecycle.xml" hash="f26968bc8644e3e3d7c412a73d7f3ba0"/></dir></dir></dir></dir><dir><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="imagerecycle"><dir name="imagerecycle"><file name="images.phtml" hash="5bfc9e839c069388560292abb074cfd6"/></dir></dir></dir></dir></dir></dir></dir></target></contents>
|
40 |
<compatible/>
|
41 |
<dependencies><required><php><min>5.3.0</min><max>5.6.0</max></php><extension><name>curl</name><min></min><max></max></extension></required></dependencies>
|
42 |
</package>
|