PHP code example of vvv3 / wp-responsive-images

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

    

vvv3 / wp-responsive-images example snippets


 <img src=" echo ImgUtils::resizeImg( $url, $width, $height, $crop ); 

use WPRI\ImgUtils;
...
<img src=" echo ImgUtils::resizeImg( get_the_post_thumbnail_url( get_the_ID(), 'full' ), 380, 250, true ); 

echo ImgUtils::imgForPost(
		get_the_ID(),
		[   // format: ['media-query' => width-in-px(int), ... , default-width-in-px(int) ]
			'(min-width: 1200px)' => 730,
			'(min-width: 768px)'  => 690,
			545, // dafault width, if no media query aplied
		],
		false,
		16/9,
		true,
		['class' => 'my-class']
	);

echo ImgUtils::pictureForPost(
		get_the_ID(),
		[
			'(max-width: 767px)'  => 545,
			'(max-width: 1199px)' => 690,
			730
		] );

if ( has_post_thumbnail( $postId ) ) {
    try {
        $originUrl = get_the_post_thumbnail_url( $postId, 'full' );
        $resizer   = Resizer::makeWithUrl( $originUrl );
        $picture =
            Picture::make(
                $originUrl, // original url for default img tag
                'customAlt', // alt for default img tag
                null, // width attribute, by default as at $originUrl image
                null, // height attribute, by default as at $originUrl image
                [     // sourses
                    Source::make(
                       [ // SrcsetItems
                        SrcsetItem::makeWithResize( $resizer->setWidth( 730 ), '1x' ),
                        SrcsetItem::makeWithResize( $resizer->setWidth( 730 * 2 ), '2x' ),
                    ],
                        [], // may add sizes
                        '(min-width: 1200px)', // may add media
                        'image/jpeg'  // may add image mime-type
                    ),

                    Source::make( [
                        SrcsetItem::makeWithResize( $resizer->setWidth( 690 ), '1x' ),
                        SrcsetItem::makeWithResize( $resizer->setWidth( 690 * 2 ), '2x' ),
                    ],
                        [],
                        '(min-width: 768px)'
                    ),

                    Source::make( [
                        SrcsetItem::makeWithResize( $resizer->setWidth( 545 ), '1x' ),
                        SrcsetItem::makeWithResize( $resizer->setWidth( 545 * 2 ), '2x' ),
                    ] ),
                ] )
                   ->setPictureAttr( 'class', 'custom-picture-class' )
                   ->setPictureAttr( 'data-some', 'custom-picture-attr' )
                   ->setImgAttr( 'class', 'custom-img-class' )
                   ->setImgAttr( 'data-some', 'custom-img-attr' )
                   ->render();
    } catch ( \Exception $ex ) {
        error_log( "\nFile: {$ex->getFile()}\nLine: {$ex->getLine()}\nMessage: {$ex->getMessage()}\n" );
        $picture = '';
    }
    echo $picture;
}