ProPeler
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
tmp
/
bsimport
/
Filename :
fix_images.php
back
Copy
<?php // Tukar semua attachment PNG-berat kepada JPEG sebenar + regenerate saiz. Run: wp eval-file fix_images.php 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; $skip = 0; $fail = 0; foreach ($atts as $att) { $file = get_attached_file($att->ID); if (!$file || !file_exists($file)) { $skip++; continue; } $info = @getimagesize($file); if (!$info) { $skip++; continue; } $isPng = ($info['mime'] === 'image/png'); $big = filesize($file) > 400 * 1024; if (!$isPng && !$big) { $skip++; continue; } // padam saiz lama (png bersaiz) $meta = wp_get_attachment_metadata($att->ID); if (!empty($meta['sizes'])) { $dir = dirname($file); foreach ($meta['sizes'] as $s) { if (!empty($s['file'])) @unlink($dir . '/' . $s['file']); } } $im = @imagecreatefromstring(file_get_contents($file)); if (!$im) { $fail++; continue; } $w = imagesx($im); if ($w > 1600) { $im2 = imagescale($im, 1600, -1); imagedestroy($im); $im = $im2; } imagejpeg($im, $file, 82); imagedestroy($im); wp_update_post(array('ID'=>$att->ID, 'post_mime_type'=>'image/jpeg')); $new = wp_generate_attachment_metadata($att->ID, $file); if ($new) wp_update_attachment_metadata($att->ID, $new); WP_CLI::log(basename($file) . ' -> ' . intval(filesize($file)/1024) . 'KB'); $done++; } WP_CLI::success("Dioptimum: $done, skip: $skip, gagal: $fail");