Skip to content

Commit

Permalink
chained tool
Browse files Browse the repository at this point in the history
  • Loading branch information
maztch committed May 28, 2019
1 parent a687a12 commit 9eb15dc
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 76 deletions.
34 changes: 34 additions & 0 deletions samples/chained_task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
//include the autoloader
require_once('../vendor/autoload.php');
//if manual installation has been used comment line that requires the autoload and uncomment this line:
//require_once('../init.php');

use Iloveimg\Iloveimg;


//this is a sample for a chined task. You can perform multiple tasks on a files uploading just once.

// you can call task class directly
// to get your key pair, please visit https://developer.iloveimg.com/user/projects
$iloveimg = new Iloveimg('project_public_id','project_secret_key');

$rotateTask = $iloveimg->newTask('rotate');

// file var keeps info about server file id, name...
// it can be used latter to cancel file
$file = $rotateTask->addFile('/path/to/file/document.jpg');
$file->setRotation(90);

// run the task
$rotateTask->execute();

//and create a new task from last action
$compressTask = $rotateTask->next('compress');
$compressTask->setCompressionLevel('extreme');

// process files
$compressTask->execute();

// and finally download file. If no path is set, it will be downloaded on current folder
$compressTask->download();
4 changes: 2 additions & 2 deletions src/CompressImageTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class CompressImageTask extends ImageTask
* @param string $publicKey
* @param string $secretKey
*/
function __construct($publicKey, $secretKey)
function __construct($publicKey, $secretKey, $makeStart = true)
{
$this->tool = 'compressimage';
parent::__construct($publicKey, $secretKey, true);
parent::__construct($publicKey, $secretKey, $makeStart);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/ConvertImageTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class ConvertImageTask extends ImageTask
* @param string $publicKey
* @param string $secretKey
*/
function __construct($publicKey, $secretKey)
function __construct($publicKey, $secretKey, $makeStart = true)
{
$this->tool = 'convertimage';
parent::__construct($publicKey, $secretKey, true);
parent::__construct($publicKey, $secretKey, $makeStart);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/CropImageTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class CropImageTask extends ImageTask
* @param string $publicKey
* @param string $secretKey
*/
function __construct($publicKey, $secretKey)
function __construct($publicKey, $secretKey, $makeStart = true)
{
$this->tool = 'cropimage';
parent::__construct($publicKey, $secretKey, true);
parent::__construct($publicKey, $secretKey, $makeStart);
}

/**
Expand Down
48 changes: 22 additions & 26 deletions src/Iloveimg.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getJWT()
$token['jti'] = $this->getPublicKey();

// Set encryptKey
if ($this->getFileEncryption()){
if ($this->getFileEncryption()) {
$token['file_encryption_key'] = $this->getEncrytKey();
}

Expand Down Expand Up @@ -164,7 +164,7 @@ public static function getTokenAlgorithm()
* @throws ProcessException
* @throws UploadException
*/
public function sendRequest($method, $endpoint, $body=null, $start=false)
public function sendRequest($method, $endpoint, $body = null, $start = false)
{
$to_server = self::$startServer;
if (!$start && !is_null($this->getWorkerServer())) {
Expand All @@ -188,18 +188,13 @@ public function sendRequest($method, $endpoint, $body=null, $start=false)
}
if ($endpoint == 'upload') {
throw new UploadException($response->body->error->message, $response->code, null, $response);
}
elseif ($endpoint == 'process') {
var_dump($response);
} elseif ($endpoint == 'process') {
throw new ProcessException($response->body->error->message, $response->code, null, $response);
}
elseif (strpos($endpoint, 'download')===0) {
var_dump($response);
} elseif (strpos($endpoint, 'download') === 0) {
throw new DownloadException($response->body->error->message, $response->code, null, $response);
}
else{
} else {
if ($response->code == 400) {
if(strpos($endpoint, 'task')!=-1){
if (strpos($endpoint, 'task') != -1) {
throw new TaskException('Invalid task id');
}
throw new \Exception('Bad Request');
Expand All @@ -217,21 +212,23 @@ public function sendRequest($method, $endpoint, $body=null, $start=false)
*
* @throws \Exception
*/
public function newTask($tool='')
public function newTask($tool = '', $makeStart = true)
{
$classname = '\\Iloveimg\\' . ucwords(strtolower($tool)) . 'ImageTask';
if (!class_exists($classname)) {
throw new \InvalidArgumentException();
}
return new $classname($this->getPublicKey(), $this->getSecretKey());
return new $classname($this->getPublicKey(), $this->getSecretKey(), $makeStart);
}

public static function setStartServer($server){
public static function setStartServer($server)
{
self::$startServer = $server;
}


public static function getStartServer(){
public static function getStartServer()
{
return self::$startServer;
}

Expand All @@ -252,17 +249,15 @@ public function setWorkerServer($workerServer)
}



/**
* @param boolean $value
*/
public function setFileEncryption($value, $encryptKey=null)
public function setFileEncryption($value, $encryptKey = null)
{
$this->encrypted = $value;
if($this->encrypted){
if ($this->encrypted) {
$this->setEncryptKey($encryptKey);
}
else{
} else {
$this->encryptKey = null;
}
}
Expand All @@ -287,9 +282,9 @@ public function getEncrytKey()
/**
* @param mixed $encrytKey
*/
public function setEncryptKey($encryptKey=null)
public function setEncryptKey($encryptKey = null)
{
if($encryptKey==null){
if ($encryptKey == null) {
$encryptKey = IloveimgTool::rand_sha1(32);
}
$len = strlen($encryptKey);
Expand All @@ -306,7 +301,7 @@ public function getStatus($server, $taskId)
{
$workerServer = $this->getWorkerServer();
$this->setWorkerServer($server);
$response = $this->sendRequest('get', 'task/'.$taskId);
$response = $this->sendRequest('get', 'task/' . $taskId);
$this->setWorkerServer($workerServer);

return $response->body;
Expand All @@ -315,12 +310,14 @@ public function getStatus($server, $taskId)
/**
* @param $verify
*/
public function verifySsl($verify){
public function verifySsl($verify)
{
Request::verifyPeer($verify);
Request::verifyHost($verify);
}

private function getUpdatedInfo(){
private function getUpdatedInfo()
{
$data = array('v' => self::VERSION);
$body = Body::Form($data);
$response = self::sendRequest('get', 'info', $body);
Expand All @@ -329,7 +326,6 @@ private function getUpdatedInfo(){
}



/**
* @return object
*/
Expand Down
Loading

0 comments on commit 9eb15dc

Please sign in to comment.