示例#1
0
 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 set_up()
 {
     parent::set_up();
     $this->after_compress_called = false;
     $after_compress_called =& $this->after_compress_called;
     $callback = function ($compressor) use(&$after_compress_called) {
         $after_compress_called = true;
     };
     $this->compressor = Tiny_Compress::create('api1234', $callback);
 }
 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'));
 }
 public function render_bulk_optimization_page()
 {
     $stats = Tiny_Image::get_optimization_statistics($this->settings);
     $estimated_costs = Tiny_Compress::estimate_cost($stats['available-unoptimised-sizes'], $this->settings->get_compression_count());
     $admin_colors = self::retrieve_admin_colors();
     $active_tinify_sizes = $this->settings->get_active_tinify_sizes();
     $auto_start_bulk = isset($_REQUEST['ids']);
     include dirname(__FILE__) . '/views/bulk-optimization.php';
 }
 public function test_estimate_cost_cheap_and_normal_and_free()
 {
     $this->assertEquals(500 * 0 + 9500 * 0.008999999999999999 + 40000 * 0.002, Tiny_Compress::estimate_cost(50000, 0));
 }
 public function update_api_key()
 {
     $key = $_POST['key'];
     if (empty($key)) {
         /* Always save if key is blank, so the key can be deleted. */
         $status = (object) array('ok' => true, 'message' => null);
     } else {
         $status = Tiny_Compress::create($key)->get_status();
     }
     if ($status->ok) {
         update_option(self::get_prefixed_name('api_key_pending'), false);
         update_option(self::get_prefixed_name('api_key'), $key);
     }
     $status->message = __($status->message, 'tiny-compress-images');
     echo json_encode($status);
     exit;
 }
 /**
  * @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);
 }
示例#9
0
 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);
 }
 protected function shrink_options($input)
 {
     return array(CURLOPT_URL => $this->config['api']['url'], CURLOPT_USERPWD => 'api:' . $this->api_key, CURLOPT_POSTFIELDS => $input, CURLOPT_BINARYTRANSFER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_CAINFO => self::get_ca_file(), CURLOPT_SSL_VERIFYPEER => true, CURLOPT_USERAGENT => Tiny_Compress::identification() . ' cURL');
 }
 public function test_should_throw_error_when_curl_and_fopen_unavailable()
 {
     $this->setExpectedException('Tiny_Exception');
     Tiny_Compress::create('api1234');
 }
 protected function shrink_options($input)
 {
     return array('http' => array('method' => 'POST', 'header' => array('Content-type: image/png', 'Authorization: Basic ' . base64_encode('api:' . $this->api_key), 'User-Agent: ' . Tiny_Compress::identification() . ' fopen'), 'content' => $input), 'ssl' => array('cafile' => self::get_ca_file(), 'verify_peer' => true));
 }
 protected function __construct($api_key, $after_compress_callback)
 {
     parent::__construct($after_compress_callback);
     $this->api_key = $api_key;
 }
 protected function __construct($api_key, $after_compress_callback)
 {
     parent::__construct($after_compress_callback);
     $this->proxy = new WP_HTTP_Proxy();
     \Tinify\setAppIdentifier(self::identifier());
     \Tinify\setKey($api_key);
 }