PHP code example of pinimize / laravel-compression-and-archive

1. Go to this page and download the library: Download pinimize/laravel-compression-and-archive 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/ */

    

pinimize / laravel-compression-and-archive example snippets


'default' => env('COMPRESSION_DRIVER', 'gzip'),

'gzip' => [
    'level' => env('GZIP_LEVEL', -1),
    'encoding' => FORCE_GZIP,
    'disk' => env('COMPRESSION_DISK', null),
],

'zlib' => [
    'level' => env('ZLIB_LEVEL', -1),
    'encoding' => ZLIB_ENCODING_DEFLATE,
    'disk' => env('COMPRESSION_DISK', null),
],

Str::compress($data)

// and

Str::decompress($compressedData)

use Illuminate\Support\Str;

// Using the default driver
$originalString = "This is a long string that will be compressed.";
$compressedString = Str::compress($originalString);

// Specifying a driver
$compressedStringGzip = Str::compress($originalString, 'gzip');
$compressedStringZlib = Str::compress($originalString, 'zlib');

use Illuminate\Support\Str;

$decompressedString = Str::decompress($compressedString);

// Specifying a driver
$decompressedStringGzip = Str::decompress($compressedStringGzip, 'gzip');
$decompressedStringZlib = Str::decompress($compressedStringZlib, 'zlib');

use Illuminate\Support\Facades\Storage;

Storage::compress(
    string $source,
    ?string $destination = null,
    bool $deleteSource = false,
    ?string $driver = null
): bool|string;

use Illuminate\Support\Facades\Storage;

Storage::decompress(
    string $source,
    ?string $destination = null,
    bool $deleteSource = false,
    ?string $driver = null
): string|bool;

use Illuminate\Support\Facades\Storage;

// Compress a file with default settings
$compressResult = Storage::compress('large_file.txt');
if ($compressResult !== false) {
    echo "File compressed successfully to: $compressResult\n";
}

// Compress a file with custom destination and driver
$compressResult = Storage::compress('document.pdf', 'compressed_doc.pdf.gz', false, 'zlib');
if ($compressResult === true) {
    echo "File compressed successfully\n";
}

// Decompress a file with default settings
$decompressResult = Storage::decompress('large_file.txt.gz');
if ($decompressResult !== false) {
    echo "File decompressed successfully to: $decompressResult\n";
}

// Decompress a file, delete the source, and use a specific driver
$decompressResult = Storage::decompress('archive.tar.gz', 'extracted_archive.tar', true, 'gzip');
if ($decompressResult === true) {
    echo "File decompressed and compressed version deleted\n";
}

use Illuminate\Http\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Compression;

// With string input
$compressed = Compression::string('Hello, World!');

// With file input
$compressed = Compression::string(new File($path));

// With uploaded input
$compressed = Compression::string(new UploadedFile($path));
// etc.

use Illuminate\Support\Facades\Decompression;

$data = gzencode('Hello, World!');
$compressed = Decompression::string($data); // Hello, World!

use Illuminate\Support\Facades\Compression;

// string input
$compressedResource = Compression::resource("I'm too big, make me smaller please!");

// resource input
$resource = fopen('path/to/input.txt', 'r');
$compressedResource = Compression::resource($resource);

use Illuminate\Support\Facades\Decompression;

// resource input
$resource = fopen('path/to/compressed_data.txt.gz', 'r');

$decompressedResource = Decompression::resource($resource);

use Illuminate\Http\File;

// Storing on a local disk
Compression::put('local_output.gz', 'Content');

// Compressing a file and storing on a local disk
Compression::put('ftp_output.gz', new File($path));

// Storing on Google Cloud Storage
Compression::put('gcs_output.gz', 'Content');

use Illuminate\Support\Facades\Decompression;

// resource input
$resource = fopen('path/to/compressed.txt.gz', 'r');

$decompressedResource = Decompression::put('local_output.txt', $resource);
$decompressedResource = Decompression::put('local_output.txt', 'path/to/compressed.txt.gz');

use Illuminate\Support\Facades\Compression;

// Storing on an S3 bucket, the path is relative to the bucket's root
Compression::put('compressed/output.gz', 'Content to compress', [
    'disk' => 's3'
]);

// Storing on a local disk
Compression::put('local_output.gz', 'Content', ['disk' => 'local']);

// Storing on an FTP server
Compression::put('ftp_output.gz', 'Content', ['disk' => 'ftp']);

// Storing on Google Cloud Storage
Compression::put('gcs_output.gz', 'Content', ['disk' => 'gcs']);

   Compression::put('output.gz', 'String content to compress');
   

   use GuzzleHttp\Psr7\Stream;
   
   $stream = new Stream(fopen('path/to/file.txt', 'r'));
   Compression::put('output.gz', $stream);
   

   use Illuminate\Http\File;
   
   $file = new File('path/to/file.txt');
   Compression::put('output.gz', $file);
   

   // In a controller method handling file upload
   public function handleUpload(Request $request)
   {
       $uploadedFile = $request->file('document');
       Compression::put('compressed_upload.gz', $uploadedFile);
   }
   

   $resource = fopen('path/to/file.txt', 'r');
   Compression::put('output.gz', $resource);
   

return Compression::download('path/to/file.txt', 'downloaded_file.gz');

return Decompression::download('path/to/file.txt.gz', 'file.txt');

$ratio = Compression::getRatio('original_content', 'compressed_content');

$algorithms = Compression::getSupportedAlgorithms();

use Illuminate\Support\Facades\Compression;

public function boot()
{
    Compression::extend('custom', function ($app) {
        return new CustomCompressionDriver($app['config']['compression.custom']);
    });
}
bash
php artisan vendor:publish --provider="Pinimize\PinimizeServiceProvider" --tag="config"