diff --git a/faf-optim.php b/faf-optim.php index efa7815..13ae832 100644 --- a/faf-optim.php +++ b/faf-optim.php @@ -16,27 +16,41 @@ * trigger only when admin is loggedin */ if (is_admin()) { - add_action('admin_menu', 'addSettingsPage'); + add_action('admin_menu', 'addOptionsPage'); + add_action('admin_init', 'registerOptions'); } /** * */ -function addSettingsPage() +function addOptionsPage() { - add_menu_page('FAF Optim', 'FAF Optim', 'administrator', __FILE__, 'settingsPage', 'dashicons-admin-generic'); + add_options_page('FAF Optim', 'FAF Optim', 'manage_options', 'faf-optim.php', 'optionsPage'); } /** * */ -function settingsPage() +function registerOptions() +{ // whitelist options + register_setting('faf-optim', 'keepexif'); + register_setting('faf-optim', 'autoopt'); + register_setting('faf-optim', 'quality'); + register_setting('faf-optim', 'thumbnail'); + register_setting('faf-optim', 'medium'); + register_setting('faf-optim', 'large'); +} + +/** + * + */ +function optionsPage() { ?>

FAF Image Optimierung

-

Optimiere deine generierten Bilder (JPG, PNG)

+

Optimiere alle generierten Bilder (JPG, PNG)

-
+ +

Einstellungen:

+ +
-
Quality
-
+
Exif Daten behalten (kann nicht wieder hergestellt werden)
+
checked>
+ +
Autooptimierung (bei Mediaupload)
+
checked>
-
Thumbnail
-
+
Qualität (JPGs)
+
-
Medium
-
+
Thumbnail optimieren
+
checked>
-
Large
-
+
Medium optimieren
+
checked>
+ +
Large optimieren
+
checked>
- +
'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => -1, ); $images = new WP_Query($args); - $sizes = array('thumbnail', 'medium', 'large'); foreach ($images->posts as $image) { - foreach ($sizes AS $size) { - if (isset($_POST[$size]) && $_POST[$size] === 'yes') { - optimizeImage($image, $size); - } - } - + $ids[] = $image->ID; } - } ?> + +

posts); ?> Bilder in der Mediathek.

+

+ + +

+ +
= 0 || $q <= 100) ? $q : 80; - $file = get_attached_file($image->ID, true); - $info = image_get_intermediate_size($image->ID, $size); - $path = realpath(str_replace(wp_basename($file), $info['file'], $file)); - $size = getHumanFilesize(filesize($path)); - $pinfo = pathinfo($path); - $ext = $pinfo['extension']; - - echo $path; - echo ' > '; - echo $size; - echo '
'; - - switch ($ext) { - case 'jpeg'; - case 'jpg'; - system('jpegoptim -m' . $quality . ' --strip-all ' . $path); + $sizes = array('thumbnail', 'medium', 'large'); + + foreach ($sizes AS $size) { + if (get_option($size) === 'yes') { + $q = get_option('quality') * 1; + $quality = ($q >= 0 || $q <= 100) ? $q : 80; + $file = get_attached_file($image->ID, true); + $info = image_get_intermediate_size($image->ID, $size); + $path = realpath(str_replace(wp_basename($file), $info['file'], $file)); + $size = getHumanFilesize(filesize($path)); + $pinfo = pathinfo($path); + $ext = $pinfo['extension']; + $strip = (get_option('keepexif') === 'yes') ? '--strip-com --strip-icc --strip-iptc' : '--strip-all'; + + echo $path; + echo ' > '; + echo $size; echo '
'; - break; - case 'png'; - system('optipng ' . $path); + + switch ($ext) { + case 'jpeg'; + case 'jpg'; + system('jpegoptim -m' . $quality . ' ' . $strip . ' ' . $path); + echo '
'; + break; + case 'png'; + system('optipng ' . $path); + echo '
'; + break; + default: + break; + } + echo '
'; - break; - default: - break; + } } - - echo '
'; } /** @@ -137,4 +189,16 @@ function getHumanFilesize($bytes, $decimals = 2) $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); $factor = floor((strlen($bytes) - 1) / 3); return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor]; -} \ No newline at end of file +} + +/** + * + */ +function optimizeCallback() +{ + $image = get_post($_POST['image']); + optimizeImage($image); + + wp_die(); // this is required to terminate immediately and return a proper response +} +add_action('wp_ajax_optimize', 'optimizeCallback');