File Manager
Editing: convert_to_webp.php
<?php // Database connection $host = "127.0.0.1:3306"; $dbUsername = "u404542307_dacotywebsites"; $password = "daCotywebs1te5"; $database = "u404542307_eizon"; try { $pdo = new PDO("mysql:host=$host;dbname=$database", $dbUsername, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die("Database connection error: " . $e->getMessage()); } // Function to convert an image to WebP format function convertToWebP($sourcePath, $destinationPath, $quality = 80) { if (!file_exists($sourcePath)) { return false; // Skip if the file does not exist } $mimeType = mime_content_type($sourcePath); // Load the image based on its MIME type switch ($mimeType) { case 'image/jpeg': case 'image/jpg': $image = imagecreatefromjpeg($sourcePath); break; case 'image/png': $image = imagecreatefrompng($sourcePath); break; case 'image/gif': $image = imagecreatefromgif($sourcePath); break; case 'image/webp': // If the image is already WebP, no conversion is needed return $sourcePath; default: return false; // Unsupported image type } // Convert and save as WebP if ($image && imagewebp($image, $destinationPath, $quality)) { imagedestroy($image); // Free up memory return $destinationPath; } else { return false; // Conversion failed } } // Fetch all image paths from the database $sql = "SELECT card_id, image1, image2, image3 FROM cards"; // Process only 10 cards for testing $stmt = $pdo->query($sql); $cards = $stmt->fetchAll(PDO::FETCH_ASSOC); // Process each card and convert its images foreach ($cards as $card) { $card_id = $card['card_id']; $images = [$card['image1'], $card['image2'], $card['image3']]; $newImagePaths = []; foreach ($images as $imagePath) { if (empty($imagePath)) { $newImagePaths[] = ''; // Skip empty paths continue; } $sourcePath = $imagePath; // Full path to the original image $webpPath = pathinfo($sourcePath, PATHINFO_DIRNAME) . '/' . pathinfo($sourcePath, PATHINFO_FILENAME) . '.webp'; // Check if the source file exists if (!file_exists($sourcePath)) { echo "Warning: File not found - $sourcePath (Card ID: $card_id)<br>"; $newImagePaths[] = $imagePath; // Keep the original path continue; } // Convert the image to WebP if (convertToWebP($sourcePath, $webpPath)) { $newImagePaths[] = $webpPath; // Save the new WebP path unlink($sourcePath); // Delete the original image to save space } else { echo "Warning: Conversion failed for $sourcePath (Card ID: $card_id)<br>"; $newImagePaths[] = $imagePath; // Keep the original path if conversion fails } } // Update the database with the new WebP paths $updateSql = "UPDATE cards SET image1 = :image1, image2 = :image2, image3 = :image3 WHERE card_id = :card_id"; $updateStmt = $pdo->prepare($updateSql); $updateStmt->execute([ ':image1' => $newImagePaths[0], ':image2' => $newImagePaths[1], ':image3' => $newImagePaths[2], ':card_id' => $card_id, ]); echo "Processed card ID: $card_id<br>"; } echo "All images have been processed successfully!"; ?>
💾 Save
⬅ Back