config
9 years ago
css
8 years ago
data
9 years ago
images
9 years ago
js
8 years ago
vendor
9 years ago
views
8 years ago
class-tiny-compress-client.php
9 years ago
class-tiny-compress-fopen.php
9 years ago
class-tiny-compress.php
9 years ago
class-tiny-exception.php
9 years ago
class-tiny-image-size.php
9 years ago
class-tiny-image.php
9 years ago
class-tiny-notices.php
9 years ago
class-tiny-php.php
9 years ago
class-tiny-plugin.php
8 years ago
class-tiny-settings.php
8 years ago
class-tiny-wp-base.php
9 years ago
class-tiny-compress.php
168 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Tiny Compress Images - WordPress plugin. |
| 4 | * Copyright (C) 2015-2017 Voormedia B.V. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | abstract class Tiny_Compress { |
| 22 | const KEY_MISSING = 'Register an account or provide an API key first'; |
| 23 | const FILE_MISSING = 'File does not exist'; |
| 24 | const WRITE_ERROR = 'No permission to write to file'; |
| 25 | |
| 26 | protected $after_compress_callback; |
| 27 | |
| 28 | public static function create( $api_key, $after_compress_callback = null ) { |
| 29 | if ( Tiny_PHP::client_supported() ) { |
| 30 | $class = 'Tiny_Compress_Client'; |
| 31 | } elseif ( Tiny_PHP::fopen_available() ) { |
| 32 | $class = 'Tiny_Compress_Fopen'; |
| 33 | } else { |
| 34 | throw new Tiny_Exception( |
| 35 | 'No HTTP client is available (cURL or fopen)', |
| 36 | 'NoHttpClient' |
| 37 | ); |
| 38 | } |
| 39 | return new $class($api_key, $after_compress_callback); |
| 40 | } |
| 41 | |
| 42 | /* Based on pricing April 2016. */ |
| 43 | public static function estimate_cost( $compressions, $usage ) { |
| 44 | return round( |
| 45 | self::compression_cost( $compressions + $usage ) - |
| 46 | self::compression_cost( $usage ), |
| 47 | 2 |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | protected function __construct( $after_compress_callback ) { |
| 52 | $this->after_compress_callback = $after_compress_callback; |
| 53 | } |
| 54 | |
| 55 | public abstract function can_create_key(); |
| 56 | public abstract function get_compression_count(); |
| 57 | public abstract function get_key(); |
| 58 | public abstract function limit_reached(); |
| 59 | |
| 60 | public function get_status() { |
| 61 | if ( $this->get_key() == null ) { |
| 62 | return (object) array( |
| 63 | 'ok' => false, |
| 64 | 'message' => self::KEY_MISSING, |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | $result = false; |
| 69 | $message = null; |
| 70 | |
| 71 | try { |
| 72 | $result = $this->validate(); |
| 73 | } catch ( Tiny_Exception $err ) { |
| 74 | if ( $err->get_status() == 401 ) { |
| 75 | $message = 'The key that you have entered is not valid'; |
| 76 | } else { |
| 77 | list( $message ) = explode( ' (HTTP', $err->getMessage(), 2 ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | $this->call_after_compress_callback(); |
| 82 | |
| 83 | return (object) array( |
| 84 | 'ok' => $result, |
| 85 | 'message' => $message, |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | public function compress_file( $file, $resize_opts = array(), $preserve_opts = array() ) { |
| 90 | if ( $this->get_key() == null ) { |
| 91 | throw new Tiny_Exception( self::KEY_MISSING, 'KeyError' ); |
| 92 | } |
| 93 | |
| 94 | if ( ! file_exists( $file ) ) { |
| 95 | throw new Tiny_Exception( self::FILE_MISSING, 'FileError' ); |
| 96 | } |
| 97 | |
| 98 | if ( ! is_writable( $file ) ) { |
| 99 | throw new Tiny_Exception( self::WRITE_ERROR, 'FileError' ); |
| 100 | } |
| 101 | |
| 102 | if ( ! $this->needs_resize( $file, $resize_opts ) ) { |
| 103 | $resize_opts = false; |
| 104 | } |
| 105 | |
| 106 | try { |
| 107 | list( $output, $details ) = $this->compress( |
| 108 | file_get_contents( $file ), |
| 109 | $resize_opts, |
| 110 | $preserve_opts |
| 111 | ); |
| 112 | } catch ( Tiny_Exception $err ) { |
| 113 | $this->call_after_compress_callback(); |
| 114 | throw $err; |
| 115 | } |
| 116 | |
| 117 | $this->call_after_compress_callback(); |
| 118 | file_put_contents( $file, $output ); |
| 119 | |
| 120 | if ( $resize_opts ) { |
| 121 | $details['output']['resized'] = true; |
| 122 | } |
| 123 | |
| 124 | return $details; |
| 125 | } |
| 126 | |
| 127 | protected abstract function validate(); |
| 128 | protected abstract function compress( $input, $resize_options, $preserve_options ); |
| 129 | |
| 130 | protected static function identifier() { |
| 131 | return 'WordPress/' . Tiny_Plugin::wp_version() . ' Plugin/' . Tiny_Plugin::version(); |
| 132 | } |
| 133 | |
| 134 | private function call_after_compress_callback() { |
| 135 | if ( $this->after_compress_callback ) { |
| 136 | call_user_func( $this->after_compress_callback, $this ); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private static function needs_resize( $file, $resize_options ) { |
| 141 | if ( ! $resize_options ) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | list($width, $height) = getimagesize( $file ); |
| 146 | |
| 147 | return ( $width > $resize_options['width'] || $height > $resize_options['height'] ); |
| 148 | } |
| 149 | |
| 150 | private static function compression_cost( $total ) { |
| 151 | $cost = 0; |
| 152 | |
| 153 | if ( $total > 10000 ) { |
| 154 | $compressions = $total - 10000; |
| 155 | $cost += $compressions * 0.002; |
| 156 | $total -= $compressions; |
| 157 | } |
| 158 | |
| 159 | if ( $total > 500 ) { |
| 160 | $compressions = $total - 500; |
| 161 | $cost += $compressions * 0.009; |
| 162 | $total -= $compressions; |
| 163 | } |
| 164 | |
| 165 | return $cost; |
| 166 | } |
| 167 | } |
| 168 |