| Server IP : 172.67.216.113 / Your IP : 104.23.243.32 [ Web Server : Apache System : Linux cpanel01wh.bkk1.cloud.z.com 2.6.32-954.3.5.lve1.4.59.el6.x86_64 #1 SMP Thu Dec 6 05:11:00 EST 2018 x86_64 User : cp648411 ( 1354) PHP Version : 7.2.34 Disable Function : NONE Domains : 0 Domains MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home2/cp648411/public_html/kainumber.com/convert/ |
Upload File : |
<?php
$folder = isset($_GET['folder']) ? rtrim($_GET['folder'], '/') . '/' : '';
if ($folder && is_dir($folder)) {
$files = scandir($folder);
$converted = 0;
/*
foreach ($files as $file) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (is_file($folder . $file) && in_array($ext, ['jpg', 'jpeg', 'png'])) {
$source = $folder . $file;
$destination = $folder . pathinfo($file, PATHINFO_FILENAME) . '.webp';
if (convertToWebp($source, $destination)) {
//unlink($source); // ✅ ลบไฟล์ต้นฉบับ
$converted++;
}
}
}
*/
foreach ($files as $file) {
$filepath = $folder . $file;
if (is_file($filepath)) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($ext, ['jpg', 'jpeg', 'png'])) {
if (convertToWebp($filepath, $folder . pathinfo($file, PATHINFO_FILENAME) . '.webp')) {
unlink($filepath);
$converted++;
}
}
}
}
echo "✅ แปลงและลบไฟล์ต้นฉบับทั้งหมดสำเร็จ $converted ไฟล์ → <a href='index.php?folder=" . urlencode($folder) . "'>กลับ</a>";
} else {
echo "❌ ไม่พบโฟลเดอร์";
}
function convertToWebp($source, $destination, $quality = 80) {
$info = getimagesize($source);
$mime = $info['mime'];
switch ($mime) {
case 'image/jpeg':
$image = imagecreatefromjpeg($source);
break;
case 'image/png':
$image = @imagecreatefrompng($source);
imagepalettetotruecolor($image);
imagealphablending($image, true);
imagesavealpha($image, true);
break;
default:
return false;
}
$result = imagewebp($image, $destination, $quality);
imagedestroy($image);
return $result;
}
?>