
Extensão de fácil uso, verificar se o navegador da suporte ao formado de imagem e disponibiliza a imagem já em formato webp.
Não precisa subir imagens novas, ele cria imagens webp de acordo com as imagens existentes.
Possibilidade de configurar a qualidade.
Desativa a qualquer momento.
Vai dar um Up no Page Speed.
Por favor verificar se usa a versão 7.2 do PHP.
Somente essa versão ou superior que vai funcionar.
Bónus:
Transformar todas as imagens em webp se o navegador suportar: (Todas mesmo, até os do CSSs):
Requisitos:
Extensão imagick;
Precisa alterar o .htaccess e adicionar o arquivo na raiz do projeto como abaixo:
ADD in .htacesss
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/$1.webp !-f
RewriteRule ^(.*)\.(jpe?g|png)$ webp-on-demand.php?source=%{SCRIPT_FILENAME} [NC,L,QSA,T=image/webp]
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
RewriteRule ^(.*)\.(jpe?g|png)$ $1.webp [NC,T=image/webp,QSD,L]
Criar arquivo no diretório: webp-on-demand.php
e add o código abaixo:
<?php
$name_file = $_GET['source'];
if (!file_exists($name_file)) {
http_response_code(404);
exit();
}
if (!isBrowserSupportsWebp()) {
try {
$image = new Imagick($name_file);
header('Content-Type: image/' . $image->getImageFormat());
echo $image;
} catch (Exception $ex) {
http_response_code(404);
exit();
}
}
$image_new_webp = $name_file . '.webp';
if (!file_exists($image_new_webp) || filemtime($name_file) > filemtime($image_new_webp)) {
try {
$image = new Imagick($name_file);
$image->setImageFormat('webp');
$image->writeImage($image_new_webp);
} catch (Exception $ex) {
http_response_code(404);
exit();
}
}
header("Content-type: image/webp");
$image = imagecreatefromwebp($image_new_webp);
imagewebp($image, NULL, 100);
imagedestroy($image);
function isBrowserSupportsWebp() {
return isset($_SERVER['HTTP_ACCEPT']) && strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) !== false || strpos( $_SERVER['HTTP_USER_AGENT'], ' Chrome/' ) !== false;
}
function utf8_substr($string, $offset, $length = null) {
if ($length === null) {
return iconv_substr($string, $offset, utf8_strlen($string), 'UTF-8');
} else {
return iconv_substr($string, $offset, $length, 'UTF-8');
}
}
function utf8_strrpos($string, $needle) {
return iconv_strrpos($string, $needle, 'UTF-8');
}
Na primeira Requisição, pode ser que demore alguns milésimos de segundos, mas nas demais o Apache irá trazer os arquivos criados.
Login and write down your comment.
Login my OpenCart Account