Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vertonim committed Jul 26, 2023
0 parents commit 973573e
Show file tree
Hide file tree
Showing 10 changed files with 794 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflow will release new versions of Vertopal PHP Library

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Release New Version

on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Release
uses: softprops/action-gh-release@v1
with:
name: Vertopal PHP Library ${{ github.ref_name }}
prerelease: false

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
vendor/
composer.lock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Vertopal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Vertopal-PHP

Vertopal-PHP is a PHP library for easy access to
[Vertopal file conversion API](https://www.vertopal.com/en/developer/api).

Using Vertopal-PHP, you can get started quickly and easily implement
the support of converting +350 file formats into your project.

## Installing Vertopal-PHP

Vertopal-PHP is available on
[Packagist](https://packagist.org/packages/vertopal/vertopal-php) and
can be installed using [Composer](https://getcomposer.org/):

```bash
composer require vertopal/vertopal-php
```

If you're not using Composer, you can also download the most recent version of
Vertopal-PHP source code as a ZIP file from the
[release page](https://github.com/vertopal/vertopal-php/releases/latest)
and load each class file manually.

### Requirements

- **PHP 7.4.0** or higher
- **cURL** extension enabled

## Using Vertopal-PHP

To use Vertopal-PHP you need to
[obtain an App-ID and a Security Token](http://www.vertopal.com/en/account/api/app/new)
as client credentials for API authentication.

The following code illustrates
[GIF to APNG](https://www.vertopal.com/en/convert/gif-to-apng) conversion using
the Vertopal PHP library.

```php
<?php
// Import Vertopal classes into the global namespace
use Vertopal\API\Credential;
use Vertopal\API\V1;

// Load Composer Autoloader
require "vendor/autoload.php";

// Create a client credential instance using your app ID and security token
$app = "your-app-id";
$token = "your-security-token";
$credential = new Credential($app, $token);

// Set HTTP request timeout high enough for Synchronous Conversion
V1::$timeout = 120;

// Set input and output details
$inputFile = "MickeyMouse.gif";
$inputPath = "/path/to/" . $inputFile;
$outputFormat = "apng";
$outputFile = "MyConvertedFile.apng";

// Call the minimum tasks required for a file conversion
$response = V1::upload($inputFile, $inputPath, $credential);
$connector = $response->result->output->connector;

$response = V1::convert($outputFormat, $credential, $connector, null, V1::SYNC);
$connector = $response->result->output->connector;

$response = V1::downloadURL($credential, $connector);
$connector = $response->result->output->connector;

V1::downloadFile($credential, $connector, $outputFile);
```
28 changes: 28 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* VertopalPHPLib - PHP library for Vertopal file conversion API.
* PHP Version 5.5.
*
* @see https://github.com/vertopal/vertopal-php/ The Vertopal-PHP GitHub repo
*
* @author Vertopal <contact@vertopal.com>
* @copyright 2023 Vertopal - https://www.vertopal.com
* @license MIT, see LICENSE for more details
* @note This program is distributed in the hope that it will be useful -
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*/


spl_autoload_register(
function ($className) {
$classPath = str_replace("\\", "/", $className);
list($namespace, $classPath) = explode("/", $classPath, 2);

$filePath = dirname(__FILE__) . "/src/" . $classPath . ".php";
if (file_exists($filePath)) {
require_once($filePath);
}
}
);
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "vertopal/vertopal-php",
"description": "PHP library for Vertopal file conversion API",
"homepage": "https://github.com/vertopal/vertopal-php",
"keywords": [
"vertopal",
"file-conversion-api",
"api",
"sdk",
"rest",
"converter",
"file-converter"
],
"type": "library",
"authors": [
{
"name": "Vertopal",
"email": "contact@vertopal.com",
"homepage": "https://www.vertopal.com"
}
],
"funding": [
{
"type": "other",
"url": "https://www.vertopal.com/en/donate"
}
],
"support": {
"name": "Vertopal",
"email": "contact@vertopal.com",
"docs": "https://www.vertopal.com/en/developer/api/introduction",
"issues": "https://github.com/vertopal/vertopal-php/issues"
},
"require": {
"php": ">=7.4.0",
"ext-curl": "*",
"ext-json": "*"
},
"autoload": {
"psr-4": {"Vertopal\\": "src/"}
},
"license": "MIT"
}
138 changes: 138 additions & 0 deletions src/API/ConnectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

/**
* VertopalPHPLib - PHP library for Vertopal file conversion API.
* PHP Version 5.5.
*
* @see https://github.com/vertopal/vertopal-php/ The Vertopal-PHP GitHub repo.
*
* @author Vertopal <contact@vertopal.com>
* @copyright 2023 Vertopal - https://www.vertopal.com
* @license MIT, see LICENSE for more details
* @note This program is distributed in the hope that it will be useful -
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*/

namespace Vertopal\API;

use Vertopal\Vertopal;


/**
* Interface class used as parent of different API versions.
* Provides different methods for generating User-Agent string and HTTP headers.
*
* @access public
*/
class ConnectionInterface
{

/**
* HTTP request timeout. in seconds.
* The maximum number of seconds to allow cURL functions to execute.
* @access public
* @staticvar int
*/
public static $timeout = 30;

/**
* Async (asynchronous) mode strategy.
* @access public
* @var string
*/
public const ASYNC = "async";
/**
* Sync (synchronous) mode strategy.
* @access public
* @var string
*/
public const SYNC = "sync";

/**
* Flag for decoding HTTP response.
* @access protected
* @var int
*/
protected const RESPONSE_DECODE = 1;

/**
* Flag for writing HTTP response to disk.
* @access protected
* @var int
*/
protected const RESPONSE_TO_FILE = 2;

/**
* Flag for raw HTTP response.
* @access protected
* @var int
*/
protected const RESPONSE_RAW = 3;

/**
* Generate User-Agent string for HTTP request header.
*
* @static
* @access protected
*
* @return string Returns User-Agent string.
*/
protected static function getUserAgent(): string
{
$product = Vertopal::LIBNAME;
$productVersion = Vertopal::VERSION;
$platformRelease = php_uname("r");
$platformMachine = php_uname("m");
$platformSystem = php_uname("s");
// Rename Windows platform
if ($platformSystem === "Windows NT") {
$platformSystem = "Windows";
}

$platformFull = $platformSystem;

if ($platformRelease) {
// Shorten release info if contains hyphen
if (str_contains($platformRelease, "-")) {
$hyphenPosition = strpos($platformRelease, "-");
$shortRelease = substr($platformRelease, 0, $hyphenPosition);
$platformFull .= " {$shortRelease}";
} else {
$platformFull .= " {$platformRelease}";
}
}
if ($platformMachine) {
if ($platformMachine === "AMD64") {
if (str_starts_with(strtolower($platformSystem), "windows")) {
$platformFull .= "; Win64";
}
$platformFull .= "; x64";
} else {
$platformFull .= "; {$platformMachine}";
}
}

$userAgent = "{$product}/{$productVersion} ({$platformFull})";
return $userAgent;
}

/**
* Concatenate the token provided to build and return an HTTP header.
*
* @static
* @access public
*
* @param string $token Your Security-Token.
* @return array Returns the HTTP header needed for API requests.
*/
protected static function getHeaders(string $token): array
{
$userAgent = self::getUserAgent();
return [
"Authorization: Bearer {$token}",
"User-Agent: {$userAgent}",
];
}

}

0 comments on commit 973573e

Please sign in to comment.