-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebp.php
94 lines (78 loc) · 2.68 KB
/
webp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
// Incluir o banco de dados
require_once($_SERVER['DOCUMENT_ROOT'].'/Database.php');
// Obtém a conexão PDO
$db = Database::getInstance();
$pdo = $db->getConnection();
// Define o diretório de origem das imagens
$directory = $_SERVER['DOCUMENT_ROOT'].'/imagens/';
$quality = 100; // Qualidade do WEBP
// Função para converter uma imagem para WEBP
function convertToWebP($filePath, $quality) {
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) {
echo "Formato não suportado: $filePath\n";
return false;
}
$outputFile = preg_replace('/\.\w+$/i', '.webp', $filePath);
if (file_exists($outputFile)) {
echo "Arquivo WEBP já existe, conversão ignorada: $outputFile\n";
return true;
}
try {
switch ($ext) {
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($filePath);
break;
case 'png':
$image = imagecreatefrompng($filePath);
break;
case 'gif':
$image = imagecreatefromgif($filePath);
break;
default:
echo "Formato não suportado para conversão: $filePath\n";
return false;
}
imagewebp($image, $outputFile, $quality);
imagedestroy($image);
echo "Convertido com sucesso: $filePath -> $outputFile\n";
unlink($filePath); // Deleta o arquivo original
return true;
} catch (Exception $e) {
echo "Erro ao converter $filePath para WEBP: " . $e->getMessage() . "\n";
return false;
}
}
// Lê todos os arquivos de imagem no diretório
$files = glob($directory . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
if (empty($files)) {
echo "Nenhuma imagem encontrada no diretório especificado: $directory\n";
return false;
}
foreach ($files as $file) {
try {
convertToWebP($file, $quality);
} catch (Exception $e) {
echo "Erro inesperado ao processar $file: " . $e->getMessage() . "\n";
return false;
}
}
// Atualizar URLs no banco de dados
$queryUpdate =
UPDATE imagens
SET
url_imagem = REGEXP_REPLACE(url_imagem, '\\.(jpg|jpeg|png|gif)$', '.webp')
WHERE
url_imagem REGEXP '\\.(jpg|jpeg|png|gif)$'
";
$stmtUpdate = $pdo->prepare($queryUpdate);
$stmtUpdate->execute();
// Verifica se as atualizações foram feitas com sucesso
if ($stmtUpdate->rowCount() > 0) {
echo "Todas as URLs foram atualizadas com sucesso!\n";
} else {
echo "Nenhuma URL foi atualizada.\n";
}
?>