├── .gitignore ├── LICENSE.md ├── README.md ├── _config.yml ├── composer.json ├── composer.lock ├── config └── tinify.php └── src ├── Facades └── Tinify.php ├── LaravelTinifyServiceProvider.php └── Services └── TinifyService.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | 3 | ### OSX ### 4 | *.DS_Store 5 | .AppleDouble 6 | .LSOverride 7 | 8 | # Icon must end with two \r 9 | Icon 10 | 11 | 12 | # Thumbnails 13 | ._* 14 | 15 | # Files that might appear in the root of a volume 16 | .DocumentRevisions-V100 17 | .fseventsd 18 | .Spotlight-V100 19 | .TemporaryItems 20 | .Trashes 21 | .VolumeIcon.icns 22 | .com.apple.timemachine.donotpresent 23 | 24 | # Directories potentially created on remote AFP share 25 | .AppleDB 26 | .AppleDesktop 27 | Network Trash Folder 28 | Temporary Items 29 | .apdisk 30 | 31 | 32 | ### PhpStorm ### 33 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 34 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 35 | 36 | # User-specific stuff: 37 | .idea/ 38 | 39 | # Sensitive or high-churn files: 40 | .idea/dataSources.ids 41 | .idea/dataSources.xml 42 | .idea/dataSources.local.xml 43 | .idea/sqlDataSources.xml 44 | .idea/dynamic.xml 45 | .idea/uiDesigner.xml 46 | 47 | # Gradle: 48 | .idea/gradle.xml 49 | .idea/libraries 50 | 51 | # Mongo Explorer plugin: 52 | .idea/mongoSettings.xml 53 | 54 | ## File-based project format: 55 | *.iws 56 | 57 | ## Plugin-specific files: 58 | 59 | # IntelliJ 60 | /out/ 61 | 62 | # mpeltonen/sbt-idea plugin 63 | .idea_modules/ 64 | 65 | # JIRA plugin 66 | atlassian-ide-plugin.xml 67 | 68 | # Crashlytics plugin (for Android Studio and IntelliJ) 69 | com_crashlytics_export_strings.xml 70 | crashlytics.properties 71 | crashlytics-build.properties 72 | fabric.properties 73 | 74 | ### PhpStorm Patch ### 75 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 76 | 77 | # *.iml 78 | # modules.xml 79 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [2016] [Marvin Oßwald] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ysTinify-laravel 2 | Tinify API support with laravel 3 | 4 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/yasmuru/ys-tinify-laravel.svg?style=flat-square)](https://packagist.org/packages/yasmuru/ys-tinify-laravel) 5 | 6 | ## Install 7 | 8 | ``` bash 9 | $ composer require yasmuru/ys-tinify-laravel 10 | ``` 11 | 12 | Add this to your config/app.php, 13 | 14 | under "providers": 15 | ```php 16 | yasmuru\LaravelTinify\LaravelTinifyServiceProvider::class, 17 | ``` 18 | under "aliases": 19 | 20 | ```php 21 | 'Tinify' => yasmuru\LaravelTinify\Facades\Tinify::class 22 | ``` 23 | 24 | 25 | And set a env variable `TINIFY_APIKEY` with your tinypng api key. 26 | 27 | If you want to directly upload the image to `aws s3`, you need set the env variables of following with your aws s3 credentials. 28 | 29 | ```php 30 | S3_KEY= 31 | S3_SECRET= 32 | S3_REGION= 33 | S3_BUCKET= 34 | ``` 35 | 36 | ## Examples 37 | 38 | ```php 39 | $result = Tinify::fromFile('\path\to\file'); 40 | 41 | 42 | $result = Tinify::fromBuffer($source_data); 43 | 44 | $result = Tinify::fromUrl($image_url); 45 | 46 | /** To save as File **/ 47 | $result->toFile('\path\to\save'); 48 | 49 | /** To get image as data **/ 50 | $data = $result->toBuffer(); 51 | ``` 52 | 53 | ```php 54 | 55 | $s3_result = Tinify::fileToS3('\path\to\file', $s3_bucket_name, '/path/to/save/in/bucket'); 56 | 57 | $s3_result = Tinify::bufferToS3($source_data, $s3_bucket_name, '/path/to/save/in/bucket'); 58 | 59 | $s3_result = Tinify::urlToS3($image_url, $s3_bucket_name, '/path/to/save/in/bucket'); 60 | 61 | /** To get the url of saved image **/ 62 | $s3_image_url = $s3_result->location(); 63 | $s3_image_width = $s3_result->width(); 64 | $s3_image_hight = $s3_result->height(); 65 | 66 | ``` 67 | 68 | `NOTE:` All the images directly save to s3 is publicably readable. And you can set permissions for s3 bucket folder in your aws console to make sure the privacy of images. 69 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yasmuru/ys-tinify-laravel", 3 | "type": "library", 4 | "description": "Tinify API support with laravel", 5 | "keywords": ["tinify","tinypng", "laravel"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "yasmuru", 10 | "email": "yasmuru@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.5.0", 15 | "illuminate/support": "~5.2", 16 | "tinify/tinify":"*" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "yasmuru\\LaravelTinify\\": "src" 21 | } 22 | }, 23 | "minimum-stability": "stable" 24 | } 25 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", 5 | "This file is @generated automatically" 6 | ], 7 | "hash": "46abc34d4a420cb7ca9e34c9bca3748f", 8 | "content-hash": "0367a57db2ea03ec84aefb803fde77f8", 9 | "packages": [ 10 | { 11 | "name": "doctrine/inflector", 12 | "version": "v1.1.0", 13 | "source": { 14 | "type": "git", 15 | "url": "https://github.com/doctrine/inflector.git", 16 | "reference": "90b2128806bfde671b6952ab8bea493942c1fdae" 17 | }, 18 | "dist": { 19 | "type": "zip", 20 | "url": "https://api.github.com/repos/doctrine/inflector/zipball/90b2128806bfde671b6952ab8bea493942c1fdae", 21 | "reference": "90b2128806bfde671b6952ab8bea493942c1fdae", 22 | "shasum": "" 23 | }, 24 | "require": { 25 | "php": ">=5.3.2" 26 | }, 27 | "require-dev": { 28 | "phpunit/phpunit": "4.*" 29 | }, 30 | "type": "library", 31 | "extra": { 32 | "branch-alias": { 33 | "dev-master": "1.1.x-dev" 34 | } 35 | }, 36 | "autoload": { 37 | "psr-0": { 38 | "Doctrine\\Common\\Inflector\\": "lib/" 39 | } 40 | }, 41 | "notification-url": "https://packagist.org/downloads/", 42 | "license": [ 43 | "MIT" 44 | ], 45 | "authors": [ 46 | { 47 | "name": "Roman Borschel", 48 | "email": "roman@code-factory.org" 49 | }, 50 | { 51 | "name": "Benjamin Eberlei", 52 | "email": "kontakt@beberlei.de" 53 | }, 54 | { 55 | "name": "Guilherme Blanco", 56 | "email": "guilhermeblanco@gmail.com" 57 | }, 58 | { 59 | "name": "Jonathan Wage", 60 | "email": "jonwage@gmail.com" 61 | }, 62 | { 63 | "name": "Johannes Schmitt", 64 | "email": "schmittjoh@gmail.com" 65 | } 66 | ], 67 | "description": "Common String Manipulations with regard to casing and singular/plural rules.", 68 | "homepage": "http://www.doctrine-project.org", 69 | "keywords": [ 70 | "inflection", 71 | "pluralize", 72 | "singularize", 73 | "string" 74 | ], 75 | "time": "2015-11-06 14:35:42" 76 | }, 77 | { 78 | "name": "illuminate/contracts", 79 | "version": "v5.2.32", 80 | "source": { 81 | "type": "git", 82 | "url": "https://github.com/illuminate/contracts.git", 83 | "reference": "411b851962c211078ade7664a6976e77a78cd2a5" 84 | }, 85 | "dist": { 86 | "type": "zip", 87 | "url": "https://api.github.com/repos/illuminate/contracts/zipball/411b851962c211078ade7664a6976e77a78cd2a5", 88 | "reference": "411b851962c211078ade7664a6976e77a78cd2a5", 89 | "shasum": "" 90 | }, 91 | "require": { 92 | "php": ">=5.5.9" 93 | }, 94 | "type": "library", 95 | "extra": { 96 | "branch-alias": { 97 | "dev-master": "5.2-dev" 98 | } 99 | }, 100 | "autoload": { 101 | "psr-4": { 102 | "Illuminate\\Contracts\\": "" 103 | } 104 | }, 105 | "notification-url": "https://packagist.org/downloads/", 106 | "license": [ 107 | "MIT" 108 | ], 109 | "authors": [ 110 | { 111 | "name": "Taylor Otwell", 112 | "email": "taylorotwell@gmail.com" 113 | } 114 | ], 115 | "description": "The Illuminate Contracts package.", 116 | "homepage": "http://laravel.com", 117 | "time": "2016-03-07 20:37:17" 118 | }, 119 | { 120 | "name": "illuminate/support", 121 | "version": "v5.2.32", 122 | "source": { 123 | "type": "git", 124 | "url": "https://github.com/illuminate/support.git", 125 | "reference": "61329ea409362fdae167fdca1125f46c997ce689" 126 | }, 127 | "dist": { 128 | "type": "zip", 129 | "url": "https://api.github.com/repos/illuminate/support/zipball/61329ea409362fdae167fdca1125f46c997ce689", 130 | "reference": "61329ea409362fdae167fdca1125f46c997ce689", 131 | "shasum": "" 132 | }, 133 | "require": { 134 | "doctrine/inflector": "~1.0", 135 | "ext-mbstring": "*", 136 | "illuminate/contracts": "5.2.*", 137 | "paragonie/random_compat": "~1.4", 138 | "php": ">=5.5.9" 139 | }, 140 | "suggest": { 141 | "illuminate/filesystem": "Required to use the composer class (5.2.*).", 142 | "jeremeamia/superclosure": "Required to be able to serialize closures (~2.2).", 143 | "symfony/polyfill-php56": "Required to use the hash_equals function on PHP 5.5 (~1.0).", 144 | "symfony/process": "Required to use the composer class (2.8.*|3.0.*).", 145 | "symfony/var-dumper": "Improves the dd function (2.8.*|3.0.*)." 146 | }, 147 | "type": "library", 148 | "extra": { 149 | "branch-alias": { 150 | "dev-master": "5.2-dev" 151 | } 152 | }, 153 | "autoload": { 154 | "psr-4": { 155 | "Illuminate\\Support\\": "" 156 | }, 157 | "files": [ 158 | "helpers.php" 159 | ] 160 | }, 161 | "notification-url": "https://packagist.org/downloads/", 162 | "license": [ 163 | "MIT" 164 | ], 165 | "authors": [ 166 | { 167 | "name": "Taylor Otwell", 168 | "email": "taylorotwell@gmail.com" 169 | } 170 | ], 171 | "description": "The Illuminate Support package.", 172 | "homepage": "http://laravel.com", 173 | "time": "2016-05-14 16:24:10" 174 | }, 175 | { 176 | "name": "paragonie/random_compat", 177 | "version": "v1.4.1", 178 | "source": { 179 | "type": "git", 180 | "url": "https://github.com/paragonie/random_compat.git", 181 | "reference": "c7e26a21ba357863de030f0b9e701c7d04593774" 182 | }, 183 | "dist": { 184 | "type": "zip", 185 | "url": "https://api.github.com/repos/paragonie/random_compat/zipball/c7e26a21ba357863de030f0b9e701c7d04593774", 186 | "reference": "c7e26a21ba357863de030f0b9e701c7d04593774", 187 | "shasum": "" 188 | }, 189 | "require": { 190 | "php": ">=5.2.0" 191 | }, 192 | "require-dev": { 193 | "phpunit/phpunit": "4.*|5.*" 194 | }, 195 | "suggest": { 196 | "ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes." 197 | }, 198 | "type": "library", 199 | "autoload": { 200 | "files": [ 201 | "lib/random.php" 202 | ] 203 | }, 204 | "notification-url": "https://packagist.org/downloads/", 205 | "license": [ 206 | "MIT" 207 | ], 208 | "authors": [ 209 | { 210 | "name": "Paragon Initiative Enterprises", 211 | "email": "security@paragonie.com", 212 | "homepage": "https://paragonie.com" 213 | } 214 | ], 215 | "description": "PHP 5.x polyfill for random_bytes() and random_int() from PHP 7", 216 | "keywords": [ 217 | "csprng", 218 | "pseudorandom", 219 | "random" 220 | ], 221 | "time": "2016-03-18 20:34:03" 222 | }, 223 | { 224 | "name": "tinify/tinify", 225 | "version": "1.3.0", 226 | "source": { 227 | "type": "git", 228 | "url": "https://github.com/tinify/tinify-php.git", 229 | "reference": "e53e5a6837cdb0f3c85a1a5332aff3d83fd5a2da" 230 | }, 231 | "dist": { 232 | "type": "zip", 233 | "url": "https://api.github.com/repos/tinify/tinify-php/zipball/e53e5a6837cdb0f3c85a1a5332aff3d83fd5a2da", 234 | "reference": "e53e5a6837cdb0f3c85a1a5332aff3d83fd5a2da", 235 | "shasum": "" 236 | }, 237 | "require": { 238 | "ext-curl": "*", 239 | "ext-json": "*", 240 | "lib-curl": ">=7.20.0", 241 | "php": ">=5.3.0" 242 | }, 243 | "require-dev": { 244 | "phpunit/phpunit": "~4.0", 245 | "symfony/yaml": "~2.0" 246 | }, 247 | "type": "library", 248 | "autoload": { 249 | "files": [ 250 | "lib/Tinify.php", 251 | "lib/Tinify/Exception.php" 252 | ], 253 | "psr-4": { 254 | "Tinify\\": "lib/Tinify/" 255 | } 256 | }, 257 | "notification-url": "https://packagist.org/downloads/", 258 | "license": [ 259 | "MIT" 260 | ], 261 | "authors": [ 262 | { 263 | "name": "Rolf Timmermans", 264 | "email": "rolftimmermans@voormedia.com" 265 | } 266 | ], 267 | "description": "PHP client for the Tinify API. Tinify compresses your images intelligently. Read more at https://tinify.com.", 268 | "homepage": "https://tinify.com/developers", 269 | "keywords": [ 270 | "api", 271 | "compress", 272 | "images", 273 | "tinify", 274 | "tinyjpg", 275 | "tinypng" 276 | ], 277 | "time": "2016-04-01 08:35:40" 278 | } 279 | ], 280 | "packages-dev": [], 281 | "aliases": [], 282 | "minimum-stability": "stable", 283 | "stability-flags": [], 284 | "prefer-stable": false, 285 | "prefer-lowest": false, 286 | "platform": { 287 | "php": ">=5.5.0" 288 | }, 289 | "platform-dev": [] 290 | } 291 | -------------------------------------------------------------------------------- /config/tinify.php: -------------------------------------------------------------------------------- 1 | env('TINIFY_APIKEY'), 5 | ]; 6 | -------------------------------------------------------------------------------- /src/Facades/Tinify.php: -------------------------------------------------------------------------------- 1 | publishes([$configPath => config_path('tinify.php')], 'config'); 25 | } 26 | 27 | /** 28 | * Register the service provider. 29 | * 30 | * @return void 31 | */ 32 | public function register() 33 | { 34 | $configPath = __DIR__ . '/../config/tinify.php'; 35 | $this->mergeConfigFrom($configPath, 'tinify'); 36 | $this->app->bind('tinify', 'yasmuru\LaravelTinify\Services\TinifyService'); 37 | 38 | } 39 | 40 | /** 41 | * Get the services provided by the provider. 42 | * 43 | * @return array 44 | */ 45 | public function provides() 46 | { 47 | return array(); 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /src/Services/TinifyService.php: -------------------------------------------------------------------------------- 1 | apikey = config('tinify.apikey'); 17 | if(!$this->apikey) { 18 | throw new \InvalidArgumentException('Please set TINIFY_APIKEY environment variables.'); 19 | } 20 | $this->client = new Tinify(); 21 | $this->client->setKey($this->apikey); 22 | 23 | $this->s3_key = env('S3_KEY'); 24 | $this->s3_secret = env('S3_SECRET'); 25 | $this->s3_region = env('S3_REGION'); 26 | } 27 | public function setKey($key) { 28 | return $this->client->setKey($key); 29 | } 30 | 31 | public function setAppIdentifier($appIdentifier) { 32 | return $this->client->setAppIdentifier($appIdentifier); 33 | } 34 | 35 | public function getCompressionCount() { 36 | return $this->client->getCompressionCount(); 37 | } 38 | 39 | public function compressionCount() { 40 | return $this->client->getCompressionCount(); 41 | } 42 | 43 | public function fromFile($path) { 44 | return Source::fromFile($path); 45 | } 46 | 47 | public function fromBuffer($string) { 48 | return Source::fromBuffer($string); 49 | } 50 | 51 | public function fromUrl($string) { 52 | return Source::fromUrl($string); 53 | } 54 | 55 | function isS3Set() { 56 | if($this->s3_key && $this->s3_secret && $this->s3_region ) { 57 | return true; 58 | } 59 | 60 | throw new \InvalidArgumentException('Please set S3 environment variables.'); 61 | } 62 | 63 | public function fileToS3($source_path, $bucket, $destination_path) { 64 | if($this->isS3Set()) { 65 | return Source::fromFile($source_path) 66 | ->store(array( 67 | "service" => "s3", 68 | "aws_access_key_id" => $this->s3_key, 69 | "aws_secret_access_key" => $this->s3_secret, 70 | "region" => $this->s3_region, 71 | "path" => $bucket . $destination_path, 72 | )); 73 | } 74 | } 75 | 76 | public function bufferToS3($string, $bucket, $path) { 77 | if($this->isS3Set()) { 78 | return Source::fromBuffer($string) 79 | ->store(array( 80 | "service" => "s3", 81 | "aws_access_key_id" => $this->s3_key, 82 | "aws_secret_access_key" => $this->s3_secret, 83 | "region" => $this->s3_region, 84 | "path" => $bucket . $path, 85 | )); 86 | } 87 | } 88 | 89 | public function urlToS3($url, $bucket, $path) { 90 | if($this->isS3Set()) { 91 | return Source::fromUrl($url) 92 | ->store(array( 93 | "service" => "s3", 94 | "aws_access_key_id" => $this->s3_key, 95 | "aws_secret_access_key" => $this->s3_secret, 96 | "region" => $this->s3_region, 97 | "path" => $bucket . $path, 98 | )); 99 | } 100 | } 101 | 102 | public function validate() { 103 | try { 104 | $this->client->getClient()->request("post", "/shrink"); 105 | } catch (ClientException $e) { 106 | return true; 107 | } 108 | } 109 | } --------------------------------------------------------------------------------