diff --git a/samples/chained_task.php b/samples/chained_task.php new file mode 100644 index 0000000..0501287 --- /dev/null +++ b/samples/chained_task.php @@ -0,0 +1,34 @@ +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(); \ No newline at end of file diff --git a/src/CompressImageTask.php b/src/CompressImageTask.php index 15f2623..85a92cb 100644 --- a/src/CompressImageTask.php +++ b/src/CompressImageTask.php @@ -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); } /** diff --git a/src/ConvertImageTask.php b/src/ConvertImageTask.php index 1bee266..679e905 100644 --- a/src/ConvertImageTask.php +++ b/src/ConvertImageTask.php @@ -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); } /** diff --git a/src/CropImageTask.php b/src/CropImageTask.php index 6b621f2..4d3662c 100644 --- a/src/CropImageTask.php +++ b/src/CropImageTask.php @@ -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); } /** diff --git a/src/Iloveimg.php b/src/Iloveimg.php index c2b7ee2..b5f06ba 100644 --- a/src/Iloveimg.php +++ b/src/Iloveimg.php @@ -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(); } @@ -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())) { @@ -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'); @@ -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; } @@ -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; } } @@ -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); @@ -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; @@ -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); @@ -329,7 +326,6 @@ private function getUpdatedInfo(){ } - /** * @return object */ diff --git a/src/ImageTask.php b/src/ImageTask.php index c79ea89..09a4d7a 100644 --- a/src/ImageTask.php +++ b/src/ImageTask.php @@ -56,16 +56,17 @@ class ImageTask extends Iloveimg * @param null $publicKey * @param null $secretKey */ - function __construct($publicKey, $secretKey, $makeStart=false) + function __construct($publicKey, $secretKey, $makeStart = false) { parent::__construct($publicKey, $secretKey); - if($makeStart) { + if ($makeStart == true) { $this->start(); } } - public function start(){ + public function start() + { $data = array('v' => self::VERSION); $body = Body::Form($data); $response = parent::sendRequest('get', 'start/' . $this->tool, $body); @@ -76,6 +77,37 @@ public function start(){ $this->setTask($response->body->task); } + public function next($nextTool): ImageTask + { + $data = [ + 'v' => self::VERSION, + 'task' => $this->getTaskId(), + 'tool' => $nextTool + ]; + $body = Body::Form($data); + + try { + $response = parent::sendRequest('post', 'task/next', $body); + + if (empty($response->body->task)) { + throw new StartException('No task assigned on chained start'); + }; + } catch (\Exception $e) { + throw new StartException('Error on start chained task'); + } + + $next = $this->newTask($nextTool); + $next->setWorkerServer($this->getWorkerServer()); + $next->setTask($response->body->task); + + //add files chained + foreach ($response->body->files as $serverFilename => $fileName) { + $next->files[] = new File($serverFilename, $fileName); + } + + return $next; + } + public function setTask($task) { $this->task = $task; @@ -100,12 +132,12 @@ public function getFilesArray() return $filesArray; } - public function getStatus($server=null, $taskId=null) + public function getStatus($server = null, $taskId = null) { $server = $server ? $server : $this->getWorkerServer(); $taskId = $taskId ? $taskId : $this->getTaskId(); - if($server==null || $taskId==null){ + if ($server == null || $taskId == null) { throw new \Exception('Cannot get status if no file is uploaded'); } return parent::getStatus($this->getWorkerServer(), $this->getTaskId()); @@ -145,7 +177,7 @@ public function addFileFromUrl($url) */ public function uploadFile($task, $filepath) { - $data = array('task' => $task, 'v'=> self::VERSION); + $data = array('task' => $task, 'v' => self::VERSION); $files = array('file' => $filepath); $body = Request\Body::multipart($data, $files); @@ -158,7 +190,7 @@ public function uploadFile($task, $filepath) */ public function delete() { - $response = $this->sendRequest('delete', 'task/'.$this->getTaskId()); + $response = $this->sendRequest('delete', 'task/' . $this->getTaskId()); return $this; } @@ -174,7 +206,7 @@ public function delete() */ public function uploadUrl($task, $url) { - $data = array('task' => $task, 'cloud_file' => $url, 'v'=> self::VERSION); + $data = array('task' => $task, 'cloud_file' => $url, 'v' => self::VERSION); $body = Request\Body::Form($data); $response = parent::sendRequest('post', 'upload', $body); return new File($response->body->server_filename, basename($url)); @@ -186,8 +218,8 @@ public function uploadUrl($task, $url) */ public function download($path = null) { - if($path!=null && !is_dir($path)){ - if(pathinfo($path, PATHINFO_EXTENSION) == ''){ + if ($path != null && !is_dir($path)) { + if (pathinfo($path, PATHINFO_EXTENSION) == '') { throw new PathException('Invalid download path. Use method setOutputFilename() to set the output file name.'); } throw new PathException('Invalid download path. Set a valid folder path to download the file.'); @@ -210,7 +242,7 @@ public function download($path = null) public function blob() { $this->downloadFile($this->task); - return $this->outputFile; + return $this->outputFile; } /** @@ -221,19 +253,18 @@ public function toBrowser() { $this->downloadFile($this->task); - if($this->outputFileType == 'pdf'){ + if ($this->outputFileType == 'pdf') { header("Content-type:application/pdf"); - header("Content-Disposition:attachment;filename=\"".$this->outputFileName."\""); - } - else{ + header("Content-Disposition:attachment;filename=\"" . $this->outputFileName . "\""); + } else { if (function_exists('mb_strlen')) { $size = mb_strlen($this->outputFile, '8bit'); } else { $size = strlen($this->outputFile); } header('Content-Type: application/zip'); - header("Content-Disposition: attachment; filename=\"".$this->outputFileName."\""); - header("Content-Length: ".$size); + header("Content-Disposition: attachment; filename=\"" . $this->outputFileName . "\""); + header("Content-Length: " . $size); } echo $this->outputFile; return; @@ -249,14 +280,13 @@ public function toBrowser() */ private function downloadFile($task) { - $data = array('v'=> self::VERSION); + $data = array('v' => self::VERSION); $body = Request\Body::Form($data); $response = parent::sendRequest('get', 'download/' . $task, $body); - if(preg_match("/filename\*\=utf-8\'\'([\W\w]+)/", $response->headers['Content-Disposition'], $matchesUtf)){ + if (preg_match("/filename\*\=utf-8\'\'([\W\w]+)/", $response->headers['Content-Disposition'], $matchesUtf)) { $filename = urldecode(str_replace('"', '', $matchesUtf[1])); - } - else { + } else { preg_match('/ .*filename=\"([\W\w]+)\"/', $response->headers['Content-Disposition'], $matches); $filename = str_replace('"', '', $matches[1]); } @@ -291,13 +321,13 @@ public function getEncrypted($value) */ public function execute() { - if($this->task===null){ + if ($this->task === null) { throw new \Exception('Current task not exists'); } $data = array_merge( $this->getPublicVars($this), - array('task' => $this->task, 'files' => $this->files, 'v'=> self::VERSION)); + array('task' => $this->task, 'files' => $this->files, 'v' => self::VERSION)); //clean unwanted vars to be sent unset($data['timeoutLarge']); @@ -313,7 +343,8 @@ public function execute() return $this; } - public function getPublicVars () { + public function getPublicVars() + { return call_user_func('get_object_vars', $this); } @@ -347,9 +378,10 @@ public function setOutputFilename($filename) * @throws Exceptions\UploadException * @throws \Exception */ - public function deleteFile($file){ + public function deleteFile($file) + { if (($key = array_search($file, $this->files)) !== false) { - $body = Request\Body::multipart(['task'=>$this->getTaskId(), 'server_filename'=>$file->server_filename, 'v'=> self::VERSION]); + $body = Request\Body::multipart(['task' => $this->getTaskId(), 'server_filename' => $file->server_filename, 'v' => self::VERSION]); $this->sendRequest('post', 'upload/delete', $body); unset($this->files[$key]); } @@ -362,9 +394,10 @@ public function deleteFile($file){ * * @return ImageTask */ - public function checkValues($value, $allowedValues){ - if(!in_array($value, $allowedValues)){ - throw new \InvalidArgumentException('Invalid '.$this->tool.' value "'.$value.'". Must be one of: '.implode(',', $allowedValues)); + public function checkValues($value, $allowedValues) + { + if (!in_array($value, $allowedValues)) { + throw new \InvalidArgumentException('Invalid ' . $this->tool . ' value "' . $value . '". Must be one of: ' . implode(',', $allowedValues)); } } @@ -409,9 +442,9 @@ public function ignoreErrors($value) * @param boolean $value * @return ImageTask */ - public function setFileEncryption($value, $encryptKey=null) + public function setFileEncryption($value, $encryptKey = null) { - if(count($this->files)>0){ + if (count($this->files) > 0) { throw new \Exception('Encrypth mode cannot be set after file upload'); } @@ -448,7 +481,8 @@ public function setCustomString($customString) * * @throws \Exception */ - public function listTasks($tool=null, $status=null, $customInt=null, $page=null){ + public function listTasks($tool = null, $status = null, $customInt = null, $page = null) + { $this->checkValues($status, $this->statusValues); @@ -457,8 +491,8 @@ public function listTasks($tool=null, $status=null, $customInt=null, $page=null) 'status' => $status, 'custom_int' => $customInt, 'page' => $page, - 'v'=> self::VERSION, - 'secret_key'=>$this->getSecretKey() + 'v' => self::VERSION, + 'secret_key' => $this->getSecretKey() ]; $body = Request\Body::multipart($data); diff --git a/src/RepairImageTask.php b/src/RepairImageTask.php index 1354535..878faf3 100644 --- a/src/RepairImageTask.php +++ b/src/RepairImageTask.php @@ -14,9 +14,9 @@ class RepairImageTask extends ImageTask * @param string $publicKey * @param string $secretKey */ - function __construct($publicKey, $secretKey) + function __construct($publicKey, $secretKey, $makeStart = true) { $this->tool = 'repairimage'; - parent::__construct($publicKey, $secretKey, true); + parent::__construct($publicKey, $secretKey, $makeStart); } } diff --git a/src/ResizeImageTask.php b/src/ResizeImageTask.php index 1873b4d..0a10474 100644 --- a/src/ResizeImageTask.php +++ b/src/ResizeImageTask.php @@ -48,10 +48,10 @@ class ResizeImageTask extends ImageTask * @param string $publicKey * @param string $secretKey */ - function __construct($publicKey, $secretKey) + function __construct($publicKey, $secretKey, $makeStart = true) { $this->tool = 'resizeimage'; - parent::__construct($publicKey, $secretKey, true); + parent::__construct($publicKey, $secretKey, $makeStart); } /** diff --git a/src/RotateImageTask.php b/src/RotateImageTask.php index d6756a2..f57a398 100644 --- a/src/RotateImageTask.php +++ b/src/RotateImageTask.php @@ -16,9 +16,9 @@ class RotateImageTask extends ImageTask * @param string $publicKey * @param string $secretKey */ - function __construct($publicKey, $secretKey) + function __construct($publicKey, $secretKey, $makeStart = true) { $this->tool = 'rotateimage'; - parent::__construct($publicKey, $secretKey, true); + parent::__construct($publicKey, $secretKey, $makeStart); } } diff --git a/src/WatermarkImageTask.php b/src/WatermarkImageTask.php index d545094..3e42d48 100644 --- a/src/WatermarkImageTask.php +++ b/src/WatermarkImageTask.php @@ -18,13 +18,15 @@ class WatermarkImageTask extends ImageTask /** * WatermarkTask constructor. - * @param null|string $publicKey - * @param null|string $secretKey + * + * @param null|string $publicKey Your public key + * @param null|string $secretKey Your secret key + * @param bool $makeStart Set to false for chained tasks, because we don't need the start */ - function __construct($publicKey, $secretKey) + function __construct($publicKey, $secretKey, $makeStart = true) { $this->tool = 'watermarkimage'; - parent::__construct($publicKey, $secretKey, true); + parent::__construct($publicKey, $secretKey, $makeStart); } /**