1. Go to this page and download the library: Download piggly/url-file-signer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
piggly / url-file-signer example snippets
use Piggly\UrlFileSigner\FileSigner;
// Starting with a base url and the secret key
$fileSigner = FileSigner::create( 'https://cdn.example.com', 'mysecretkey' );
// Change "Order of Parameters" parameter name
$fileSigner->changeOrderOfParametersParam('par');
// Change Expiration parameter name
$fileSigner->changeExpirationParam('exp');
// Change Signature parameter name
$fileSigner->changeSignatureParam('sig');
// Enable and set Domain parameter name
$fileSigner->enableDomainParam('dom');
use Piggly\UrlFileSigner\Collections\ParameterDict;
use Piggly\UrlFileSigner\Entities\File;
// Images allowed parameters
$imagesDict = ParameterDict::create()->add('version')->add('size')->add('compression');
// Create a file entity
$file = File::create( $imagesDict )->set('/path/to/file/image.jpg');
// Now, file separator will be '__'
$file->changeSeparator('__');
// [Tip] You can create a ParameterDict for each file type
$imageDict = ParameterDict::create()->add('version')->add('size')->add('compression');
$pdfDict = ParameterDict::create()->add('version');
// Files name will contain _c([a-z0-9]+)? parameter
$imageDict->add('compression');
// Files name will contain _xx([a-z0-9]+)? parameter (using xx as alias)
$imageDict->add('contrast', 'xx');
// Create a file entity including a ParameterDict class
$file = File::create( $imagesDict )->set('/path/to/file/image.jpg');
// It will show first the version parameter
$file->sortToDisplay(['version']);
// It will show first the version parameter, later size parameter
$file->sortToDisplay(['version','size']);
// In file name the version parameter cames first
$file->sortInFileName(['version']);
// In file name the size parameter cames first, then cames the version parameter
$file->sortInFileName(['size','version']);
// Setting all allowed parameters to image files
$imagesDict = ParameterDict::create()
->add('brightness')
->add('compression','x')
->add('contrast')
->add('version')
->add('size');
// The file name structure will be: file_b50_x82_c50_v1_s1080.jpg
// The generated URL paths will be: /b50/x82/c50/v1/s1080/...
// The File Entity can recognize these parameters
$file = File::create( $imagesDict );
// You changed the URL display order
$file->sortToDisplay( ['version','size','compression'] );
// You changed the file name order
$file->sortToDisplay( ['brightness','contrast','compression','size'] );
// The new file name structure will be: file_b50_c50_x82_s1080_v1.jpg
// The new generated URL Schema will be: /v1/s1080/x82/b50/c50/...
// The parameters attribute is a ParameterCollection class
$file->parameters->add( 'size', 1080 )->add( 'version', 1080 );
use Piggly\UrlFileSigner\Collections\ParameterDict;
use Piggly\UrlFileSigner\Entities\File;
use Piggly\UrlFileSigner\FileSigner;
// A parameter dictionary for images files
$imageDict = ParameterDict::create()->fill(['version', 'size', 'compression' => 'x']);
// A file with parameters size => 150
$file = File::create( $imagesDict )
->set('/path/to/file/image.jpg')
->parameters->fill(['size'=>150]);
// The generated URL will be encoded, signed and valid for 6 months
$signedUrl = FileSigner::create( 'https://cdn.example.com', 'mysecretkey' )->sign( $file, new DateInterval('P6M') );
// => https://cdn.example.com/s150/566a63774e6a45334e445934556a63304e6b5a554e6a59324f545a444e6a553d/image.jpg?op=cw&oe=5ECD709F&oh=8c569aa017a8b521afb7bf2c187d9089
// The generated URL will be encoded, signed and valid for 6 months. It also contains the query string `pid`. All sent query strings will be signed.
$signedUrl = FileSigner::create( 'https://cdn.example.com', 'mysecretkey' )->sign( $file, new DateInterval('P6M'), ['pid' => '309u5fj32958ikd' ] );