PHP code example of ycdev / paper-size

1. Go to this page and download the library: Download ycdev/paper-size 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/ */

    

ycdev / paper-size example snippets


use Ycdev\PaperSize;

// Get A4 dimensions in pixels (default 300 DPI)
$dimensions = PaperSize::px(PaperSize::A4);
// Result: [2480, 3508]

// Get A4 dimensions in centimeters
$dimensions = PaperSize::cm(PaperSize::A4);
// Result: [21.0, 29.7]

// Get A4 dimensions in inches
$dimensions = PaperSize::in(PaperSize::A4);
// Result: [8.27, 11.69]

use Ycdev\PaperSize;

// Create a GD image with A4 dimensions
$image = PaperSize::gdImage(PaperSize::A4);

use Ycdev\PaperSize;

// Get the aspect ratio of A4 format
$ratio = PaperSize::ratio(PaperSize::A4);
// Result: 1.4142

use Ycdev\PaperSize;

// Convert A4 format to landscape
$landscape = PaperSize::landscape(PaperSize::A4);
// Result: [297, 210]

use Ycdev\PaperSize;

// Get all available formats
$formats = PaperSize::allFormats();
// Returns an associative array:
// [
//     'A4' => ['width' => 210, 'height' => 297, 'name' => 'A4'],
//     'FR_RAISIN' => ['width' => 500, 'height' => 650, 'name' => 'Fr raisin'],
//     ...
// ]

// Example: populate a <select>
foreach (PaperSize::allFormats() as $key => $format) {
    echo "<option value=\"$key\">{$format['name']} ({$format['width']}x{$format['height']}mm)</option>";
}

use Ycdev\PaperSize;

// Detect paper format from pixel dimensions
$format = PaperSize::format(2480, 3507);
// Result: 'A4'

// Works in landscape too
$format = PaperSize::format(3507, 2480);
// Result: 'A4'

// Returns false if no match
$format = PaperSize::format(1, 1);
// Result: false

use Ycdev\PaperSize;

$image = PaperSize::gdImage(PaperSize::A4);
$cropped = PaperSize::crop($image, PaperSize::A5);

use Ycdev\PaperSize;

// Convert to pixels with 600 DPI resolution
$dimensions = PaperSize::px(PaperSize::A4, 600);