public function __construct() { parent::__construct(); $this->settings = new Tiny_Settings(); try { $this->compressor = Tiny_Compress::get_compressor($this->settings->get_api_key()); } catch (Tiny_Exception $e) { $this->add_admin_notice(self::translate_escape($e->getMessage())); } }
public function compress(Varien_Event_Observer $observer) { $imageObject = $observer->getEvent()->getObject(); $destinationSubdir = $imageObject->getDestinationSubdir(); switch ($destinationSubdir) { case "image": $allowCompression = Mage::getStoreConfig('compress_images/image_sizes/compress_image'); break; case "small_image": $allowCompression = Mage::getStoreConfig('compress_images/image_sizes/compress_small_image'); break; case "thumbnail": $allowCompression = Mage::getStoreConfig('compress_images/image_sizes/compress_thumbnail'); break; default: $allowCompression = Mage::getStoreConfig('compress_images/image_sizes/compress_other_images'); break; } if ($allowCompression) { $apiKey = Mage::getStoreConfig('compress_images/settings/api_key'); if (!empty($apiKey)) { $newFile = $imageObject->getNewFile(); $width = $imageObject->getWidth(); $height = $imageObject->getHeight(); $compressor = Tiny_Compress::get_compressor($apiKey); try { $details = $compressor->compress_file($newFile); $logDescription = "Variant " . $destinationSubdir . " allowed " . $allowCompression . " width " . $width . " height " . $height . " API " . $apiKey . " JSON response " . json_encode($details); } catch (Tiny_Exception $e) { $logDescription = $e->get_error() . ': ' . $e->getMessage(); } } else { $logDescription = "No API key found for compression."; } } else { $logDescription = "Variant {$destinationSubdir} not selected for compression."; } Mage::log($logDescription, null, 'image-compression.log'); }
public function admin_init() { if (current_user_can('manage_options') && !$this->get_api_key()) { $link = sprintf('<a href="options-media.php#%s">%s</a>', self::NAME, self::translate_escape('Please fill in an API key to start compressing images')); $this->notices->show('setting', $link, 'error', false); } try { $this->compressor = Tiny_Compress::get_compressor($this->get_api_key(), $this->get_method('after_compress_callback')); } catch (Tiny_Exception $e) { $this->notices->show('compressor_exception', self::translate_escape($e->getMessage()), 'error', false); } $section = self::get_prefixed_name('settings'); add_settings_section($section, self::translate('PNG and JPEG compression'), $this->get_method('render_section'), 'media'); $field = self::get_prefixed_name('api_key'); register_setting('media', $field); add_settings_field($field, self::translate('TinyPNG API key'), $this->get_method('render_api_key'), 'media', $section, array('label_for' => $field)); $field = self::get_prefixed_name('sizes'); register_setting('media', $field); add_settings_field($field, self::translate('File compression'), $this->get_method('render_sizes'), 'media', $section); $field = self::get_prefixed_name('status'); register_setting('media', $field); add_settings_field($field, self::translate('Connection status'), $this->get_method('render_pending_status'), 'media', $section); add_action('wp_ajax_tiny_compress_status', $this->get_method('connection_status')); }
/** * @expectedException Tiny_Exception */ public function testShouldThrowErrorWhenCurlAndFopenUnavailable() { $this->php_mock->shouldReceive('is_fopen_available')->andReturn(false); $compressor = Tiny_Compress::get_compressor('api1234'); $this->assertInstanceOf('Tiny_Compress', $compressor); }
private function init_compressor() { $this->compressor = Tiny_Compress::get_compressor($this->get_api_key(), $this->get_method('after_compress_callback')); }
public function testShouldReturnCurlCompressorByDefault() { $compressor = Tiny_Compress::get_compressor('api1234'); $this->assertInstanceOf('Tiny_Compress_Curl', $compressor); }