Skip to content

Commit

Permalink
Add support to upload directly to s3
Browse files Browse the repository at this point in the history
  • Loading branch information
muruganstr committed Nov 16, 2016
1 parent 4fdc9eb commit 0cfb395
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ under "providers":
```php
yasmuru\LaravelTinify\LaravelTinifyServiceProvider::class,
```
uder "aliases":
under "aliases":

```php
'Tinify' => yasmuru\LaravelTinify\Facades\Tinify::class
```


And set a env variable `TINIFY_APIKEY`
And set a env variable `TINIFY_APIKEY` with your tinypng api key.

If you want to directly upload the image to `aws s3`, you need set the env variables of following with your aws s3 credentials.

```php
S3_KEY=
S3_SECRET=
S3_REGION=
S3_BUCKET=
```
55 changes: 54 additions & 1 deletion src/Services/TinifyService.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

namespace yasmuru\LaravelTinify\Services;

use Tinify\Source;
use Tinify\Tinify;

class TinifyService {

/**
* Get api key from env, fail if any are missing.
* Instantiate API client and set api key.
Expand All @@ -17,6 +19,10 @@ public function __construct() {
}
$this->client = new Tinify();
$this->client->setKey($this->apikey);

$this->s3_key = env('S3_KEY');
$this->s3_secret = env('S3_SECRET');
$this->s3_region = env('S3_REGION');
}
public function setKey($key) {
return $this->client->setKey($key);
Expand Down Expand Up @@ -46,6 +52,53 @@ public function fromUrl($string) {
return Source::fromUrl($string);
}

function isS3Set() {
if($this->s3_key && $this->s3_secret && $this->s3_region ) {
return true;
}

throw new \InvalidArgumentException('Please set S3 environment variables.');
}

public function fileToS3($source_path, $bucket, $destination_path) {
if($this->isS3Set()) {
return Source::fromFile($source_path)
->store(array(
"service" => "s3",
"aws_access_key_id" => $this->s3_key,
"aws_secret_access_key" => $this->s3_secret,
"region" => $this->s3_region,
"path" => $bucket . $destination_path,
));
}
}

public function bufferToS3($string, $bucket, $path) {
if($this->isS3Set()) {
return Source::fromBuffer($string)
->store(array(
"service" => "s3",
"aws_access_key_id" => $this->s3_key,
"aws_secret_access_key" => $this->s3_secret,
"region" => $this->s3_region,
"path" => $bucket . $path,
));
}
}

public function urlToS3($url, $bucket, $path) {
if($this->isS3Set()) {
return Source::fromUrl($url)
->store(array(
"service" => "s3",
"aws_access_key_id" => $this->s3_key,
"aws_secret_access_key" => $this->s3_secret,
"region" => $this->s3_region,
"path" => $bucket . $path,
));
}
}

public function validate() {
try {
$this->client->getClient()->request("post", "/shrink");
Expand Down

1 comment on commit 0cfb395

@Ratha8
Copy link

@Ratha8 Ratha8 commented on 0cfb395 Oct 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m using Tinify with my Laravel application. Part of my deployment process is to optimize my configuration environment by using php artisan config:cache. This speeds my app’s load time as well as access to configuration variables. Unfortunately, one of the side effects of using this is that the env(‘’) function no longer works to retrieve environment variable settings. You instead need to create a file in /config and pre-load all of your .env settings. Here’s the best explanation I could find of the procedure: https://scotch.io/tutorials/how-to-use-laravel-config-files

The TinifyService class uses env(‘’) in its constructor. Since I’m using php artisan config:cache, this always returns a null string.

So … what I’m asking is if you could modify your code to include a config/tinify.php file that pulls in the S3_KEY, S3_SECRET, S3_REGION from the .env file. You would then need to load the .env value in the new config/tinify.php file and modify the TinifyService class to use config(‘tinify.s3_key’), config(‘tinify.s3_secret’), config(‘tinify.s3_region’) (or similar).

This approach would then work correctly whether the user implements the config cache or not.

Something similar to below:

Current TinifyService constructor

class TinifyService {

/**
 * Get api key from env, fail if any are missing.
 * Instantiate API client and set api key.
 *
 * @throws Exception
 */
public function __construct() {
    $this->apikey = env('TINIFY_APIKEY');
    if(!$this->apikey) {
        throw new \InvalidArgumentException('Please set TINIFY_APIKEY environment variables.');
    }
    $this->client = new Tinify();
    $this->client->setKey($this->apikey);

    $this->s3_key = env('S3_KEY');
    $this->s3_secret = env('S3_SECRET');
    $this->s3_region = env('S3_REGION');
}

Proposed TinifyService constructor

class TinifyService {
/**
* Get api key from env, fail if any are missing.
* Instantiate API client and set api key.
*
* @throws Exception
*/
public function __construct() {
$this->apikey = config(‘tinify.apikey’);
if(!$this->apikey) {
throw new \InvalidArgumentException('Please set TINIFY_APIKEY environment variables.');
}
$this->client = new Tinify();
$this->client->setKey($this->apikey);

    $this->s3_key = env('tinify.s3_key');
    $this->s3_secret = env('tinify.s3_secret');
    $this->s3_region = env('tinify.s3_region');
}

current config/tinify.php

env(‘TINIFY_APIKEY’), ]; Proposed config/tinify.php env(‘TINIFY_APIKEY’), "s3_key" => env('S3_KEY'), "s3_secret" => env('S3_SECRET'), "s3_region" => env('S3_REGION'), ];

Please sign in to comment.