PHP code example of baaaaane / image-optimizer

1. Go to this page and download the library: Download baaaaane/image-optimizer 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/ */

    

baaaaane / image-optimizer example snippets


use Baaane\ImageOptimizer\Action\ImageOptimizer;

$imageOptimizer = new ImageOptimizer($defaultPath);

$imageOptimizer->upload($_FILES['filename']);

git clone https://github.com/baaane/image-optimizer.git

composer 

composer 

$imageOptimizer->uploadRequestFile($request->file('filename'));

$imageOptimizer->setPath($optionalPath)->upload();

$new_name = [
    'new_name' => 'new_name1'
];

$size = [
    'size' => $imageOptimizer->setThumbnailSize(width,height)
                            ->setMobileSize(width,height)
                            ->setDesktopSize(width,height)
                            ->get(), 
];

$data = $imageOptimizer->reArray($data_merge);

$imageOptimizer->deleteOriginalFile()->upload();

[
    'name' => 'uploaded-image.jpg',
    'type' => 'image/jpeg',
    'size' => 542,
    'tmp_name' => __DIR__. '/_files/test.jpg',
    'error' => 0,
    'new_name' => 'new_name1',
    'new_size' => [
        'thumbnail' => [
            'width' => 200,
            'height' => 200
        ]
        'mobile' => [
            'width' => 691,
            'height' => 961
        ]
        'desktop' => [
            'width' => 1920,
            'height' => 1080
        ]
    ]
]

// Before merge
$_FILES = [
    'filename' => [
        'name' => 'uploaded-image.jpg',
        'type' => 'image/jpeg',
        'size' => 542,
        'tmp_name' =>	__DIR__. '/_files/test.jpg',
        'error' => 0,
    ]
];

// OPTIONAL PARAMETER: If the input post is string, convert it to array
$new_name = [
	'new_name' => 'new_name1'
];

// OPTIONAL PARAMETER: If the input post is string, convert it to array
$new_size = [
    'new_size' => $imageOptimizer->setThumbnailSize(200,200)
                                ->setMobileSize(691,961)
                                ->setDesktopSize(1920,1080)
                                ->get(), 
];

// Then merge
$data = array_merge($_FILES['filename'], $new_name, $new_size);

// OPTIONAL PARAMETER: desired path of uploaded files
$path = __DIR__. '/_files';

$imageOptimizer = new ImageOptimizer($defaultPath);
$imageOptimizer->setPath($optionalPath)->upload($data);

$_FILES = [
    'filename' => [
        'name' => [
            0 => 'uploaded-image.jpg',
            1 => 'uploaded-image1.jpg',
        ],
        'type' => [
            0 => 'image/jpeg',
            1 => 'image/jpeg',
        ],
        'size' => [
            0 => 542,
            1 => 542,
        ],
        'tmp_name' => [
            0 => __DIR__. '/_files/test.jpg',
            1 => __DIR__. '/_files/test1.jpg',
        ],
        'error' => [
            0 => 0,
            1 => 0,
        ]
    ]
];

// OPTIONAL PARAMETER: If the input post is string, convert it to array
$new_name = [
    'new_name' => ['new_name1', 'new_name2']
];

// OPTIONAL PARAMETER: If the input post is string, convert it to array
$size = [ 
    'new_size' => [
        $imageOptimizer->setThumbnailSize(200,200)
                        ->setMobileSize(691,961)
                        ->setDesktopSize(800,750)
                        ->get(),
        $imageOptimizer->setThumbnailSize()
                        ->setMobileSize(345,789)
                        ->setDesktopSize(0,0)
                        ->get(),
    ]
];

// Then merge
$data_merge = array_merge($_FILES['filename'], $new_name, $new_size);

// Re-arrange the merge data array
$data = $imageOptimizer->reArray($data_merge);

// OPTIONAL PARAMETER: desired path of uploaded files
$path = __DIR__. '/_files';

$imageOptimizer = new ImageOptimizer(defaultPath);
$imageOptimizer->setPath($optionalPath)->upload($data);

// single upload file
Array 
(
  0 => Array (
    "thumbnail" => "/_files/thumbnail_new_name1.jpg"
    "mobile" => "/_files/mobile_new_name1.jpg"
    "desktop" => "/_files/desktop_new_name1.jpg"
  )
)

// multiple upload file
Array 
(
  0 => Array (
    "thumbnail" => "/_files/thumbnail_new_name1.jpg"
    "mobile" => "/_files/mobile_new_name1.jpg"
    "desktop" => "/_files/desktop_new_name1.jpg"
  )
  1 => Array (
    "thumbnail" => "/_files/thumbnail_new_name2.jpg"
    "mobile" => "/_files/mobile_new_name2.jpg"
    "desktop" => "/_files/desktop_new_name2.jpg"
  )
)