PHP code example of generoi / wp-image-resizer
1. Go to this page and download the library: Download generoi/wp-image-resizer 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/ */
generoi / wp-image-resizer example snippets
wp_get_attachment_image($profileImage, 'full', false, [
'data-resizer-args' => 'crop=1:1&quality=50',
'sizes' => '(min-width: 40em) 48px, (min-width: 68.75em) 56px, (min-width: 87.5em) 64px, 40px'
])
use GeneroWP\ImageResizer\Image;
$image = new Image($url, [
'blur' => 50,
]);
return sprintf(
'<img loading="eager" class="wp-block-cover__image-background" src="%s" srcset="%s" sizes="%s">',
$image->src(),
$image->srcset(),
'100vw'
);
use GeneroWP\ImageResizer\Image;
$image = Image::fromAttachment($bannerImageId, 'full', [
'fit' => 'cover',
'height' => 350 * 2,
]);
return sprintf(
'<img loading="eager" class="wp-block-cover__image-background" src="%s" srcset="%s" sizes="%s">',
$image->src(),
$image->srcset(),
'100vw'
);
use GeneroWP\ImageResizer\Rewriters\Preload;
add_action('wp_head', function () {
if (is_singular('recipe')) {
$composer = new ContentRecipe();
$featured = $composer->featuredImageHtml(get_post());
echo Preload::buildLink($featured);
}
}, 2);
// Alter rewriters
add_filter('wp-image-resizer/rewriters', function (array $rewriters) {
$rewriters[] = MyCustomRewriter::class;
return $rewriters;
});
// Alter resizer URLs
add_filter('wp-image-resizer/image/url', function (string $url) {
if (defined('WP_ENV') && WP_ENV === 'development') {
$url = str_replace('development-domain.ddev.site', 'production-domain.com', $url);
}
return $url;
});
// Alter blocks that should be considered for preloading
add_filter('wp-image-resizer/preload/blocks', function (array $blockTypes) {
$blockTypes[] = 'my-theme/hero-banner';
return $blockTypes;
});
// Alter if preload should stop iterating blocks
add_filter('wp-image-resizer/preload/should_stop', function (bool $stop, array $block, array $blocks) {
if ($block['blockType'] === 'my-theme/breadcrumb') {
return false;
}
return $stop;
}, 10, 3);
// Alter srcset breakpoints
add_filter('wp-image-resizer/config/breakpoints', function (array $breakpoints) {
return [
...range(50, 500, 50),
...range(600, 2000, 100),
];
});
// Alter default resizing settings
add_filter('wp-image-resizer/config/default', function (array $breakpoints) {
return [
'quality' => 90
];
});