ProPeler
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
thread-self
/
root
/
tmp
/
bsimport
/
Filename :
fix_images2.php
back
Copy
<?php // Fix extension mismatch: rename .png->.jpg, betulkan _wp_attached_file, regenerate saiz sebagai JPEG. if (!defined('WP_CLI')) { echo "Run via wp eval-file\n"; return; } require_once ABSPATH . 'wp-admin/includes/image.php'; $atts = get_posts(array('post_type'=>'attachment','numberposts'=>-1,'post_status'=>'any')); $done = 0; foreach ($atts as $att) { $file = get_attached_file($att->ID); if (!$file || !file_exists($file)) continue; $info = @getimagesize($file); if (!$info) continue; $dir = dirname($file); $base = pathinfo($file, PATHINFO_FILENAME); $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); // padam SEMUA fail saiz lama (png & jpg) untuk basename ini foreach (glob($dir . '/' . $base . '-*x*.{png,jpg,jpeg,webp}', GLOB_BRACE) as $old) { @unlink($old); } // pastikan kandungan = JPEG (dah ditukar pusingan lepas; jaga-jaga) if ($info['mime'] !== 'image/jpeg') { $im = @imagecreatefromstring(file_get_contents($file)); if ($im) { imagejpeg($im, $file, 82); imagedestroy($im); } } // rename ke .jpg jika perlu if ($ext !== 'jpg') { $newfile = $dir . '/' . $base . '.jpg'; if (@rename($file, $newfile)) { $rel = _wp_relative_upload_path($newfile); update_post_meta($att->ID, '_wp_attached_file', $rel); $file = $newfile; } } wp_update_post(array('ID'=>$att->ID, 'post_mime_type'=>'image/jpeg')); $meta = wp_generate_attachment_metadata($att->ID, $file); if ($meta) wp_update_attachment_metadata($att->ID, $meta); $large = !empty($meta['sizes']['large']['file']) ? $meta['sizes']['large']['file'] : '(tiada large)'; WP_CLI::log(basename($file) . ' | large: ' . $large); $done++; } WP_CLI::success("Dibaiki: $done attachment");