PHP code example of pixelshelsinki / images

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

    

pixelshelsinki / images example snippets




use Pixels\Components\Images\Factory as ImageFactory;

/**
 * Image sizes array
 * Pattern:
 *
 * NAME, WIDTH, HEIGHT, CROP, RETINA TRUE/FALSE
 *
 * @var array
 */
$sizes = array(
    'page-hero'        => array( 1100, 500, true, true ),
    'page-hero-mobile' => array( 375, 500, true, true ),
);

// Register sizes to image factory.
ImageFactory::add_image_sizes( $sizes );

/**
 * You can also optionally register css-style breakpoint for mobile / desktop.
 * Default is 576px.
 */
ImageFactory::add_breakpoint( '600px' );



use Pixels\Components\Images\Factory as ImageFactory;

$image_html = ImageFactory::responsive_image( $image_id, 'my_mobile_size', 'my_desktop_size', 'my alt text' );

echo $image_html;




use Pixels\Components\Images\Factory as ImageFactory;

$image_html = ImageFactory::responsive_background( $image_id, 'my_mobile_size', 'my_desktop_size', '#my_html_element' );

echo $image_html;




add_filter( 'get_twig', array( 'add_image_functions' ) );

function add_image_functions( $twig ) {
    // Responsive mage helper functions.
    $twig->addFunction( new \Timber\Twig_Function( 'responsive_image', '\\Pixels\\Components\\Images\\Factory::responsive_image' ) );
    $twig->addFunction( new \Timber\Twig_Function( 'responsive_background', '\\Pixels\\Components\\Images\\Factory::responsive_background' ) );

    return $twig;
}