UZUN SÜREDİR KULLANDIĞIM BİR SCRİPT DOSYA YOLUNA KLASOR UZANTINIZI YAZIN VE TÜM RESİMLERİ OPTİMİZE ETSİN:


<?php
set_time_limit(0);
ob_implicit_flush(true);
ob_end_flush();
$baseDir = __DIR__ . '/admin/resimler';
$maxWidth = 1200;
$jpegQuality = 70;
$pngCompression = 7;
$doOptimize = isset($_POST['optimize']);
$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($baseDir, FilesystemIterator::SKIP_DOTS)
);
$totalSaved = 0;
$processed  = 0;
echo "<!doctype html><html><head><meta charset='utf-8'>";
echo "<title>Resim Optimizasyonu</title></head><body>";
echo "<h2>🖼️ Resim Optimizasyonu</h2>";
if (!$doOptimize) {
    echo '<form method="post">
            <button name="optimize" style="padding:12px 25px;font-size:16px;">
                🚀 Optimize Et (Anlık Göster)
            </button>
          </form><hr>';
    echo "<p>Butona basınca işlem başlayacak.</p>";
    exit;
}
echo "<pre style='font-size:13px;line-height:1.4;'>";
foreach ($iterator as $file) {
    if (!$file->isFile()) continue;
    $ext = strtolower($file->getExtension());
    if (!in_array($ext, ['jpg','jpeg','png'])) continue;
    $path = $file->getPathname();
    $info = @getimagesize($path);
    if (!$info) continue;
    $beforeSize = filesize($path);
    $srcW = $info[0];
    $srcH = $info[1];
    if ($srcW <= $maxWidth && $beforeSize < 1024*1024) {
        continue; // küçükse atla
    }
    $newW = $srcW > $maxWidth ? $maxWidth : $srcW;
    $newH = intval(($srcH / $srcW) * $newW);
    $srcImg = @imagecreatefromstring(file_get_contents($path));
    if (!$srcImg) continue;
    $dstImg = imagecreatetruecolor($newW, $newH);
    imagecopyresampled($dstImg, $srcImg, 0,0,0,0, $newW,$newH, $srcW,$srcH);
    if ($ext === 'png') {
        imagepng($dstImg, $path, $pngCompression);
    } else {
        imagejpeg($dstImg, $path, $jpegQuality);
    }
    imagedestroy($srcImg);
    imagedestroy($dstImg);
    clearstatcache(true, $path);
    $afterSize = filesize($path);
    $saved = $beforeSize - $afterSize;
    if ($saved < 0) $saved = 0;
    $totalSaved += $saved;
    $processed++;
    echo "✔ {$file->getFilename()} | "
       . round($beforeSize/1024/1024,2) . " MB → "
       . round($afterSize/1024/1024,2) . " MB | "
       . "Tasarruf: " . round($saved/1024/1024,2) . " MB\n";
    flush();
    usleep(200000); // sunucu yormasın
}
echo "\n-----------------------------------\n";
echo "✅ İşlenen resim: {$processed}\n";
echo "💾 Toplam tasarruf: "
   . round($totalSaved/1024/1024,2) . " MB ("
   . round($totalSaved/1024/1024/1024,2) . " GB)\n";
echo "</pre>";
echo "<h3>✔ İşlem tamamlandı</h3>";
echo "</body></html>";
)