Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
adjust image sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Fiebig committed Feb 7, 2017
1 parent 6926d6a commit 52acb07
Showing 1 changed file with 136 additions and 91 deletions.
227 changes: 136 additions & 91 deletions faf-optim.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
add_action('admin_init', 'registerOptions');
}

function getSizes()
{
return get_intermediate_image_sizes();
}

/**
*
*/
Expand All @@ -36,9 +41,11 @@ function registerOptions()
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');

$sizes = getSizes();
foreach ($sizes AS $size) {
register_setting('faf-optim', $size);
}
}

/**
Expand All @@ -61,60 +68,74 @@ function optionsPage()
}
?>

<table width="100%">
<tr>
<td width="50%" style="vertical-align:top">

<h2>Einstellungen:</h2>
<form method="post" action="options.php">
<?php
settings_fields('faf-optim');
do_settings_sections('faf-optim');
?>
<dl>
<dt>Exif Daten behalten (kann nicht wieder hergestellt werden)</dt>
<dd><input type="checkbox" name="keepexif" value="yes" <?php if (get_option('keepexif') === 'yes') { ?>checked<?php } ?>></dd>

<dt>Autooptimierung (bei Mediaupload)</dt>
<dd><input type="checkbox" name="autoopt" value="yes" <?php if (get_option('autoopt') === 'yes') { ?>checked<?php } ?>></dd>

<dt>Qualität (JPGs)</dt>
<dd><input type="number" name="quality" value="<?php echo esc_attr(get_option('quality')); ?>" placeholder="Quality"></dd>

<?php
$sizes = getSizes();
foreach($sizes AS $size) {
?>
<dt><?php echo ucfirst($size); ?> optimieren</dt>
<dd><input type="checkbox" name="<?php echo $size; ?>" value="yes" <?php if (get_option($size) === 'yes') { ?>checked<?php } ?>></dd>
<?php
}
?>
</dl>

<?php submit_button('Einstellungen speichern', 'primary', 'settings'); ?>
</form>

</td>
<td width="50%" style="vertical-align:top">

<?php
$ids = [];
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
);
$images = new WP_Query($args);

foreach ($images->posts as $image) {
$ids[] = $image->ID;
}
?>

<h2><span id="counter"></span><?php echo count($images->posts); ?> Bilder in der Mediathek.</h2>
<button id="optimize" type="button" class="button button-primary">Optimieren jetzt alle Bilder</button>

<p>
<textarea id="report" style="width:80%; height:200px;"></textarea>
</p>

</td>
</tr>
</table>

<h2>Einstellungen:</h2>
<form method="post" action="options.php">
<?php
settings_fields('faf-optim');
do_settings_sections('faf-optim');
?>
<dl>
<dt>Exif Daten behalten (kann nicht wieder hergestellt werden)</dt>
<dd><input type="checkbox" name="keepexif" value="yes" <?php if (get_option('keepexif') === 'yes') { ?>checked<?php } ?>></dd>

<dt>Autooptimierung (bei Mediaupload)</dt>
<dd><input type="checkbox" name="autoopt" value="yes" <?php if (get_option('autoopt') === 'yes') { ?>checked<?php } ?>></dd>

<dt>Qualität (JPGs)</dt>
<dd><input type="number" name="quality" value="<?php echo esc_attr(get_option('quality')); ?>" placeholder="Quality"></dd>

<dt>Thumbnail optimieren</dt>
<dd><input type="checkbox" name="thumbnail" value="yes" <?php if (get_option('thumbnail') === 'yes') { ?>checked<?php } ?>></dd>

<dt>Medium optimieren</dt>
<dd><input type="checkbox" name="medium" value="yes" <?php if (get_option('medium') === 'yes') { ?>checked<?php } ?>></dd>

<dt>Large optimieren</dt>
<dd><input type="checkbox" name="large" value="yes" <?php if (get_option('large') === 'yes') { ?>checked<?php } ?>></dd>
</dl>

<?php submit_button('Einstellungen speichern', 'primary', 'settings'); ?>
</form>

<?php
$ids = [];
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
);
$images = new WP_Query($args);

foreach ($images->posts as $image) {
$ids[] = $image->ID;
}
?>

<h2><?php echo count($images->posts); ?> Bilder in der Mediathek.</h2>
<h4 id="counter"></h4>
<button id="optimize" type="button" class="button button-primary">Jetzt alle Bilder optimieren</button>

<p id="report"></p>
<script>
jQuery(document).ready(function ($) {
jQuery('#optimize').on('click', function(){
var images = [<?php echo implode(',', $ids); ?>];
jQuery('#report').html('');

jQuery.each(images, function(ix, val){

Expand All @@ -126,7 +147,7 @@ function optionsPage()

jQuery.post(ajaxurl, data, function (response) {
jQuery('#report').append(response);
jQuery('#counter').html((ix+1) + '/' + images.length + 'optimiert');
jQuery('#counter').html((ix+1) + ' / ');
})
}, 500);

Expand All @@ -140,46 +161,34 @@ function optionsPage()
}

/**
* @param $image
* @param string $size
* @param $path
*/
function optimizeImage($image)
function optimizeImage($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 '<br>';

switch ($ext) {
case 'jpeg';
case 'jpg';
system('jpegoptim -m' . $quality . ' ' . $strip . ' ' . $path);
echo '<br>';
break;
case 'png';
system('optipng ' . $path);
echo '<br>';
break;
default:
break;
}
if (!is_file($path)) {
return false;
}

echo '<br>';
}
$q = get_option('quality') * 1;
$quality = ($q >= 0 || $q <= 100) ? $q : 80;

//$size = getHumanFilesize(filesize($path));
$pinfo = pathinfo($path);
$ext = $pinfo['extension'];
$strip = (get_option('keepexif') === 'yes') ? '--strip-com --strip-icc --strip-iptc' : '--strip-all';

switch ($ext) {
case 'jpeg';
case 'jpg';
system('jpegoptim -m' . $quality . ' ' . $strip . ' ' . $path);
echo PHP_EOL;
break;
case 'png';
system('optipng ' . $path);
echo PHP_EOL;
break;
default:
break;
}
}

Expand All @@ -201,8 +210,44 @@ function getHumanFilesize($bytes, $decimals = 2)
function optimizeCallback()
{
$image = get_post($_POST['image']);
optimizeImage($image);
$sizes = getSizes();

foreach ($sizes AS $size) {
if (get_option($size) === 'yes') {

$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));

optimizeImage($path);
}
}

wp_die(); // this is required to terminate immediately and return a proper response
}
add_action('wp_ajax_optimize', 'optimizeCallback');

/**
* @param $postID
* @param $post
* @param $update
*/
function optimizeAfterUpload( $data )
{
if (get_option('autoopt') === 'yes') {
$path = wp_upload_dir();
$dir = $path['path'];
$sizes = getSizes();

foreach ($sizes AS $size) {
if (get_option($size) === 'yes' && isset($data['sizes'][$size]['file'])) {
$file = $dir.'/'.$data['sizes'][$size]['file'];

optimizeImage($file);
}
}
}

return $data;
}
//add_action( 'wp_generate_attachment_metadata', 'optimizeAfterUpload' );

0 comments on commit 52acb07

Please sign in to comment.