-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e88f340
commit 379c523
Showing
10 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Like\Codeception; | ||
|
||
use GuzzleHttp\Client; | ||
use LogicException; | ||
|
||
class UploadImage | ||
{ | ||
const URL = 'https://api.imgbb.com/1/upload'; | ||
const EXPIRATION_IN_SECONDS = 10080; // One week | ||
|
||
/** @var string */ | ||
private $token; | ||
|
||
public function __construct($token) | ||
{ | ||
$this->token = $token; | ||
} | ||
|
||
public function upload($fileToUpload) | ||
{ | ||
$url = self::URL . | ||
'?expiration=' . self::EXPIRATION_IN_SECONDS . | ||
'&key=' . $this->token; | ||
|
||
$client = new Client(); | ||
$response = $client->request('POST', $url, [ | ||
'multipart' => [ | ||
[ | ||
'name' => 'image', | ||
'contents' => fopen($fileToUpload, 'r'), | ||
], | ||
], | ||
]); | ||
|
||
$body = (string) $response->getBody(); | ||
if (! $body) { | ||
throw new LogicException('Body is empty.'); | ||
} | ||
|
||
$json = json_decode($body, true); | ||
if (! is_array($json)) { | ||
throw new LogicException('Body is json. Body: ' . $body); | ||
} | ||
|
||
if (! isset($json['data']) || ! isset($json['data']['url'])) { | ||
throw new LogicException('Body is valid json. Body: ' . $body); | ||
} | ||
|
||
return $json['data']['url']; | ||
} | ||
} |