Example #1
0
function ewww_image_optimizer_count_optimized($gallery, $return_ids = false)
{
    global $ewww_debug;
    global $wpdb;
    $ewww_debug .= "<b>ewww_image_optimizer_count_optmized()</b><br>";
    $full_count = 0;
    $unoptimized_full = 0;
    $unoptimized_re = 0;
    $resize_count = 0;
    $attachment_query = '';
    $ewww_debug .= "scanning for {$gallery}<br>";
    // retrieve the time when the optimizer starts
    $started = microtime(true);
    if (ewww_image_optimizer_stl_check()) {
        set_time_limit(0);
    }
    $max_query = 3000;
    $attachment_query_count = 0;
    switch ($gallery) {
        case 'media':
            $ids = array();
            // see if we were given attachment IDs to work with via GET/POST
            if (!empty($_REQUEST['ids']) || get_option('ewww_image_optimizer_bulk_resume')) {
                $ewww_debug .= 'we have preloaded attachment ids<br>';
                // retrieve the attachment IDs that were pre-loaded in the database
                $attachment_ids = get_option('ewww_image_optimizer_bulk_attachments');
                while ($attachment_ids && $attachment_query_count < $max_query) {
                    $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                    $attachment_query_count++;
                }
                $attachment_query = 'AND metas.post_id IN (' . substr($attachment_query, 0, -1) . ')';
            }
            $offset = 0;
            // retrieve all the image attachment metadata from the database
            while ($attachments = $wpdb->get_results("SELECT metas.meta_value,post_id FROM {$wpdb->postmeta} metas INNER JOIN {$wpdb->posts} posts ON posts.ID = metas.post_id WHERE posts.post_mime_type LIKE '%image%' AND metas.meta_key = '_wp_attachment_metadata' {$attachment_query} LIMIT {$offset}, {$max_query}", ARRAY_N)) {
                $ewww_debug .= "fetched " . count($attachments) . " attachments starting at {$offset}<br>";
                $disabled_sizes = ewww_image_optimizer_get_option('ewww_image_optimizer_disable_resizes');
                foreach ($attachments as $attachment) {
                    $meta = unserialize($attachment[0]);
                    if (empty($meta)) {
                        continue;
                    }
                    if (empty($meta['ewww_image_optimizer'])) {
                        $unoptimized_full++;
                        $ids[] = $attachment[1];
                    }
                    // resized versions, so we can continue
                    if (isset($meta['sizes'])) {
                        foreach ($meta['sizes'] as $size => $data) {
                            if (!empty($disabled_sizes[$size])) {
                                continue;
                            }
                            $resize_count++;
                            if (empty($meta['sizes'][$size]['ewww_image_optimizer'])) {
                                $unoptimized_re++;
                            }
                        }
                    }
                }
                $full_count += count($attachments);
                $offset += $max_query;
                if (!empty($attachment_ids)) {
                    $attachment_query = '';
                    $attachment_query_count = 0;
                    $offset = 0;
                    while ($attachment_ids && $attachment_query_count < $max_query) {
                        $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                        $attachment_query_count++;
                    }
                    $attachment_query = 'AND metas.post_id IN (' . substr($attachment_query, 0, -1) . ')';
                }
            }
            break;
        case 'ngg':
            // see if we were given attachment IDs to work with via GET/POST
            if (!empty($_REQUEST['ewww_inline']) || get_option('ewww_image_optimizer_bulk_ngg_resume')) {
                // retrieve the attachment IDs that were pre-loaded in the database
                $attachment_ids = get_option('ewww_image_optimizer_bulk_ngg_attachments');
                while ($attachment_ids && $attachment_query_count < $max_query) {
                    $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                    $attachment_query_count++;
                }
                $attachment_query = 'WHERE pid IN (' . substr($attachment_query, 0, -1) . ')';
            }
            // creating the 'registry' object for working with nextgen
            $registry = C_Component_Registry::get_instance();
            // creating a database storage object from the 'registry' object
            $storage = $registry->get_utility('I_Gallery_Storage');
            // get an array of sizes available for the $image
            $sizes = $storage->get_image_sizes();
            $offset = 0;
            while ($attachments = $wpdb->get_col("SELECT meta_data FROM {$wpdb->nggpictures} {$attachment_query} LIMIT {$offset}, {$max_query}")) {
                foreach ($attachments as $attachment) {
                    if (class_exists('Ngg_Serializable')) {
                        $serializer = new Ngg_Serializable();
                        $meta = $serializer->unserialize($attachment);
                    } else {
                        $meta = unserialize($attachment);
                    }
                    if (!is_array($meta)) {
                        continue;
                    }
                    if (empty($meta['ewww_image_optimizer'])) {
                        $unoptimized_full++;
                    }
                    foreach ($sizes as $size) {
                        if ($size !== 'full') {
                            $resize_count++;
                            if (empty($meta[$size]['ewww_image_optimizer'])) {
                                $unoptimized_re++;
                            }
                        }
                    }
                }
                $full_count += count($attachments);
                $offset += $max_query;
                if (!empty($attachment_ids)) {
                    $attachment_query = '';
                    $attachment_query_count = 0;
                    $offset = 0;
                    while ($attachment_ids && $attachment_query_count < $max_query) {
                        $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                        $attachment_query_count++;
                    }
                    $attachment_query = 'WHERE pid IN (' . substr($attachment_query, 0, -1) . ')';
                }
            }
            break;
        case 'flag':
            if (!empty($_REQUEST['doaction']) || get_option('ewww_image_optimizer_bulk_flag_resume')) {
                // retrieve the attachment IDs that were pre-loaded in the database
                $attachment_ids = get_option('ewww_image_optimizer_bulk_flag_attachments');
                while ($attachment_ids && $attachment_query_count < $max_query) {
                    $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                    $attachment_query_count++;
                }
                $attachment_query = 'WHERE pid IN (' . substr($attachment_query, 0, -1) . ')';
            }
            $offset = 0;
            while ($attachments = $wpdb->get_col("SELECT meta_data FROM {$wpdb->flagpictures} {$attachment_query} LIMIT {$offset}, {$max_query}")) {
                foreach ($attachments as $attachment) {
                    $meta = unserialize($attachment);
                    if (!is_array($meta)) {
                        continue;
                    }
                    if (empty($meta['ewww_image_optimizer'])) {
                        $unoptimized_full++;
                    }
                    if (!empty($meta['webview'])) {
                        $resize_count++;
                        if (empty($meta['webview']['ewww_image_optimizer'])) {
                            $unoptimized_re++;
                        }
                    }
                    if (!empty($meta['thumbnail'])) {
                        $resize_count++;
                        if (empty($meta['thumbnail']['ewww_image_optimizer'])) {
                            $unoptimized_re++;
                        }
                    }
                }
                $full_count += count($attachments);
                $offset += $max_query;
                if (!empty($attachment_ids)) {
                    $attachment_query = '';
                    $attachment_query_count = 0;
                    $offset = 0;
                    while ($attachment_ids && $attachment_query_count < $max_query) {
                        $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                        $attachment_query_count++;
                    }
                    $attachment_query = 'WHERE pid IN (' . substr($attachment_query, 0, -1) . ')';
                }
            }
            break;
    }
    if (empty($full_count) && !empty($attachment_ids)) {
        //		return array( count( $attachment_ids ), '', '', '');
        $ewww_debug .= "query appears to have failed, just counting total images instead<br>";
        $full_count = count($attachment_ids);
    }
    $elapsed = microtime(true) - $started;
    $ewww_debug .= "counting images took {$elapsed} seconds<br>";
    $ewww_debug .= "found {$full_count} fullsize ({$unoptimized_full} unoptimized), and {$resize_count} resizes ({$unoptimized_re} unoptimized)<br>";
    ewwwio_memory(__FUNCTION__);
    if ($return_ids) {
        return $ids;
    } else {
        return array($full_count, $unoptimized_full, $resize_count, $unoptimized_re);
    }
}
 /**
  * Returns the ngg images list(id and meta ) or count
  *
  * @param string $type Whether to return smushed images or unsmushed images
  * @param bool|false $count Return count only
  * @param bool|false $force_update true/false to update the cache or not
  *
  * @return bool|mixed Returns assoc array of image ids and meta or Image count
  */
 function get_ngg_images($type = 'smushed', $count = false, $force_update = false)
 {
     global $wpdb;
     /**
      * Allows to set a limit of mysql query
      * Default value is 2000
      */
     $limit = apply_filters('wp_smush_nextgen_query_limit', 2000);
     $limit = intval($limit);
     $offset = 0;
     //Check type of images being queried
     if (!in_array($type, array('smushed', 'unsmushed'))) {
         return false;
     }
     // Check for the  wp_smush_images_smushed in the 'nextgen' group.
     $images = wp_cache_get('wp_smush_images_' . $type, 'nextgen');
     // If nothing is found, build the object.
     if (!$images || $force_update) {
         // Query Attachments for meta key
         while ($attachments = $wpdb->get_results("SELECT pid, meta_data FROM {$wpdb->nggpictures} LIMIT {$offset}, {$limit}")) {
             foreach ($attachments as $attachment) {
                 //Check if it has `wp_smush` key
                 if (class_exists('Ngg_Serializable')) {
                     $serializer = new Ngg_Serializable();
                     $meta = $serializer->unserialize($attachment->meta_data);
                 } else {
                     $meta = unserialize($attachment->meta_data);
                 }
                 //Check meta for wp_smush
                 if (!is_array($meta) || empty($meta['wp_smush'])) {
                     $unsmushed_images[$attachment->pid] = $meta;
                     continue;
                 }
                 $smushed_images[$attachment->pid] = $meta;
             }
             //Set the offset
             $offset += $limit;
         }
         if (!empty($smushed_images)) {
             wp_cache_set('wp_smush_images_smushed', $smushed_images, 'nextgen', 300);
         }
         if (!empty($unsmushed_images)) {
             wp_cache_set('wp_smush_images_unsmushed', $unsmushed_images, 'nextgen', 300);
         }
     }
     if ($type == 'smushed') {
         $smushed_images = !empty($smushed_images) ? $smushed_images : $images;
         if (!$smushed_images) {
             return 0;
         } else {
             return $count ? count($smushed_images) : $smushed_images;
         }
     } else {
         $unsmushed_images = !empty($unsmushed_images) ? $unsmushed_images : $images;
         if (!$unsmushed_images) {
             return 0;
         } else {
             return $count ? count($unsmushed_images) : $unsmushed_images;
         }
     }
 }
Example #3
0
function ewww_image_optimizer_count_optimized($gallery, $return_ids = false)
{
    ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>');
    global $wpdb;
    $full_count = 0;
    $unoptimized_full = 0;
    $unoptimized_re = 0;
    $resize_count = 0;
    $attachment_query = '';
    ewwwio_debug_message("scanning for {$gallery}");
    // retrieve the time when the optimizer starts
    $started = microtime(true);
    if (ewww_image_optimizer_stl_check()) {
        set_time_limit(0);
    }
    $max_query = apply_filters('ewww_image_optimizer_count_optimized_queries', 3000);
    $max_query = (int) $max_query;
    $attachment_query_count = 0;
    switch ($gallery) {
        case 'media':
            $ids = array();
            // see if we were given attachment IDs to work with via GET/POST
            if (!empty($_REQUEST['ids']) || get_option('ewww_image_optimizer_bulk_resume')) {
                ewwwio_debug_message('we have received attachment ids via $_REQUEST');
                // retrieve the attachment IDs that were pre-loaded in the database
                $attachment_ids = get_option('ewww_image_optimizer_bulk_attachments');
                if (!empty($attachment_ids)) {
                    $full_count = count($attachment_ids);
                    while ($attachment_ids && $attachment_query_count < $max_query) {
                        $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                        $attachment_query_count++;
                    }
                    $attachment_query = 'AND metas.post_id IN (' . substr($attachment_query, 0, -1) . ')';
                }
            } else {
                $full_count = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->posts} WHERE (post_type = 'attachment' OR post_type = 'ims_image') AND (post_mime_type LIKE '%%image%%' OR post_mime_type LIKE '%%pdf%%')");
            }
            $offset = 0;
            // retrieve all the image attachment metadata from the database
            while ($attachments = $wpdb->get_results("SELECT metas.meta_value,post_id FROM {$wpdb->postmeta} metas INNER JOIN {$wpdb->posts} posts ON posts.ID = metas.post_id WHERE (posts.post_mime_type LIKE '%%image%%' OR posts.post_mime_type LIKE '%%pdf%%') AND metas.meta_key = '_wp_attachment_metadata' {$attachment_query} LIMIT {$offset},{$max_query}", ARRAY_N)) {
                ewwwio_debug_message("fetched " . count($attachments) . " attachments starting at {$offset}");
                $disabled_sizes = ewww_image_optimizer_get_option('ewww_image_optimizer_disable_resizes_opt');
                foreach ($attachments as $attachment) {
                    //					ewwwio_debug_message( 'checking attachment ' . $attachment[1] );
                    $meta = maybe_unserialize($attachment[0]);
                    if (empty($meta)) {
                        ewwwio_debug_message('empty meta');
                        continue;
                    }
                    $mime = '';
                    if (!empty($meta['file'])) {
                        $mime = ewww_image_optimizer_quick_mimetype($meta['file']);
                    } elseif (!empty($attachment[1])) {
                        $mime = get_post_mime_type($attachment[1]);
                        ewwwio_debug_message('checking mime via get_post...');
                    }
                    if ($mime == 'image/jpeg' && ewww_image_optimizer_get_option('ewww_image_optimizer_jpg_level') == 0) {
                        //						ewwwio_debug_message( 'optimization for this type disabled, skipping' );
                        continue;
                    }
                    if ($mime == 'image/png' && ewww_image_optimizer_get_option('ewww_image_optimizer_png_level') == 0) {
                        //						ewwwio_debug_message( 'optimization for this type disabled, skipping' );
                        continue;
                    }
                    if ($mime == 'image/gif' && ewww_image_optimizer_get_option('ewww_image_optimizer_gif_level') == 0) {
                        //						ewwwio_debug_message( 'optimization for this type disabled, skipping' );
                        continue;
                    }
                    if ($mime == 'application/pdf' && ewww_image_optimizer_get_option('ewww_image_optimizer_pdf_level') == 0) {
                        //						ewwwio_debug_message( 'optimization for this type disabled, skipping' );
                        continue;
                    }
                    if (empty($meta['ewww_image_optimizer'])) {
                        //						ewwwio_debug_message( 'no optimization status, counting as unoptimized full: ' . $attachment[1] );
                        //						ewwwio_debug_message( print_r( $meta, true ) );
                        $unoptimized_full++;
                        $ids[] = $attachment[1];
                    }
                    if (!empty($meta['ewww_image_optimizer']) && preg_match('/' . __('License exceeded', EWWW_IMAGE_OPTIMIZER_DOMAIN) . '/', $meta['ewww_image_optimizer'])) {
                        //						ewwwio_debug_message( 'optimization status license exceeded, counting as unoptimized full: ' . $attachment[1] );
                        //						ewwwio_debug_message( print_r( $meta, true ) );
                        $unoptimized_full++;
                        $ids[] = $attachment[1];
                    }
                    // resized versions, so we can continue
                    if (isset($meta['sizes']) && ewww_image_optimizer_iterable($meta['sizes'])) {
                        foreach ($meta['sizes'] as $size => $data) {
                            if (!empty($disabled_sizes[$size])) {
                                continue;
                            }
                            if (strpos($size, 'webp') === 0) {
                                continue;
                            }
                            $resize_count++;
                            if (empty($meta['sizes'][$size]['ewww_image_optimizer'])) {
                                $unoptimized_re++;
                            }
                        }
                    }
                }
                //		$full_count += count( $attachments );
                $offset += $max_query;
                if (!empty($attachment_ids)) {
                    $attachment_query = '';
                    $attachment_query_count = 0;
                    $offset = 0;
                    while ($attachment_ids && $attachment_query_count < $max_query) {
                        $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                        $attachment_query_count++;
                    }
                    $attachment_query = 'AND metas.post_id IN (' . substr($attachment_query, 0, -1) . ')';
                }
            }
            break;
        case 'ngg':
            // see if we were given attachment IDs to work with via GET/POST
            if (!empty($_REQUEST['ewww_inline']) || get_option('ewww_image_optimizer_bulk_ngg_resume')) {
                // retrieve the attachment IDs that were pre-loaded in the database
                $attachment_ids = get_option('ewww_image_optimizer_bulk_ngg_attachments');
                while ($attachment_ids && $attachment_query_count < $max_query) {
                    $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                    $attachment_query_count++;
                }
                $attachment_query = 'WHERE pid IN (' . substr($attachment_query, 0, -1) . ')';
            }
            // creating the 'registry' object for working with nextgen
            $registry = C_Component_Registry::get_instance();
            // creating a database storage object from the 'registry' object
            $storage = $registry->get_utility('I_Gallery_Storage');
            // get an array of sizes available for the $image
            $sizes = $storage->get_image_sizes();
            $offset = 0;
            while ($attachments = $wpdb->get_col("SELECT meta_data FROM {$wpdb->nggpictures} {$attachment_query} LIMIT {$offset}, {$max_query}")) {
                foreach ($attachments as $attachment) {
                    if (class_exists('Ngg_Serializable')) {
                        $serializer = new Ngg_Serializable();
                        $meta = $serializer->unserialize($attachment);
                    } else {
                        $meta = unserialize($attachment);
                    }
                    if (!is_array($meta)) {
                        continue;
                    }
                    if (empty($meta['ewww_image_optimizer'])) {
                        $unoptimized_full++;
                    }
                    if (ewww_image_optimizer_iterable($sizes)) {
                        foreach ($sizes as $size) {
                            if ($size !== 'full') {
                                $resize_count++;
                                if (empty($meta[$size]['ewww_image_optimizer'])) {
                                    $unoptimized_re++;
                                }
                            }
                        }
                    }
                }
                $full_count += count($attachments);
                $offset += $max_query;
                if (!empty($attachment_ids)) {
                    $attachment_query = '';
                    $attachment_query_count = 0;
                    $offset = 0;
                    while ($attachment_ids && $attachment_query_count < $max_query) {
                        $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                        $attachment_query_count++;
                    }
                    $attachment_query = 'WHERE pid IN (' . substr($attachment_query, 0, -1) . ')';
                }
            }
            break;
        case 'flag':
            if (!empty($_REQUEST['doaction']) || get_option('ewww_image_optimizer_bulk_flag_resume')) {
                // retrieve the attachment IDs that were pre-loaded in the database
                $attachment_ids = get_option('ewww_image_optimizer_bulk_flag_attachments');
                while ($attachment_ids && $attachment_query_count < $max_query) {
                    $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                    $attachment_query_count++;
                }
                $attachment_query = 'WHERE pid IN (' . substr($attachment_query, 0, -1) . ')';
            }
            $offset = 0;
            while ($attachments = $wpdb->get_col("SELECT meta_data FROM {$wpdb->flagpictures} {$attachment_query} LIMIT {$offset}, {$max_query}")) {
                foreach ($attachments as $attachment) {
                    $meta = unserialize($attachment);
                    if (!is_array($meta)) {
                        continue;
                    }
                    if (empty($meta['ewww_image_optimizer'])) {
                        $unoptimized_full++;
                    }
                    if (!empty($meta['webview'])) {
                        $resize_count++;
                        if (empty($meta['webview']['ewww_image_optimizer'])) {
                            $unoptimized_re++;
                        }
                    }
                    if (!empty($meta['thumbnail'])) {
                        $resize_count++;
                        if (empty($meta['thumbnail']['ewww_image_optimizer'])) {
                            $unoptimized_re++;
                        }
                    }
                }
                $full_count += count($attachments);
                $offset += $max_query;
                if (!empty($attachment_ids)) {
                    $attachment_query = '';
                    $attachment_query_count = 0;
                    $offset = 0;
                    while ($attachment_ids && $attachment_query_count < $max_query) {
                        $attachment_query .= "'" . array_pop($attachment_ids) . "',";
                        $attachment_query_count++;
                    }
                    $attachment_query = 'WHERE pid IN (' . substr($attachment_query, 0, -1) . ')';
                }
            }
            break;
    }
    if (empty($full_count) && !empty($attachment_ids)) {
        ewwwio_debug_message('query appears to have failed, just counting total images instead');
        $full_count = count($attachment_ids);
    }
    $elapsed = microtime(true) - $started;
    ewwwio_debug_message("counting images took {$elapsed} seconds");
    ewwwio_debug_message("found {$full_count} fullsize ({$unoptimized_full} unoptimized), and {$resize_count} resizes ({$unoptimized_re} unoptimized)");
    ewwwio_memory(__FUNCTION__);
    if ($return_ids) {
        return $ids;
    } else {
        return array($full_count, $unoptimized_full, $resize_count, $unoptimized_re);
    }
}
Example #4
0
 /**
  * Update or add meta data for an image
  *
  * @since 1.4.0
  * @param int $id The image ID
  * @param array $values An array with existing or new values
  * @return bool result of query
  */
 static function update_image_meta($id, $new_values)
 {
     global $wpdb;
     // XXX nggdb is used statically, cannot inherit from Ngg_Serializable
     $serializer = new Ngg_Serializable();
     // Query database for existing values
     // Use cache object
     $old_values = $wpdb->get_var($wpdb->prepare("SELECT meta_data FROM {$wpdb->nggpictures} WHERE pid = %d ", $id));
     $old_values = $serializer->unserialize($old_values);
     $meta = array_merge((array) $old_values, (array) $new_values);
     $meta = $serializer->serialize($meta);
     $result = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->nggpictures} SET meta_data = %s WHERE pid = %d", $meta, $id));
     wp_cache_delete($id, 'ngg_image');
     return $result;
 }
Example #5
0
function ewww_image_optimizer_import_loop()
{
    // verify that an authorized user has started the optimizer
    if (!wp_verify_nonce($_REQUEST['ewww_wpnonce'], 'ewww-image-optimizer-bulk')) {
        wp_die(__('Cheatin&#8217; eh?', EWWW_IMAGE_OPTIMIZER_DOMAIN));
    }
    global $wpdb;
    global $ewww_debug;
    // retrieve the time when the optimizer starts
    //	$started = microtime(true);
    $import_finished = false;
    $import_status = get_option('ewww_image_optimizer_import_status');
    $attachments = $wpdb->get_results("SELECT posts.ID,metas.meta_value FROM {$wpdb->postmeta} metas INNER JOIN {$wpdb->posts} posts ON posts.ID = metas.post_id WHERE posts.post_mime_type LIKE '%image%' AND metas.meta_key = '_wp_attachment_metadata' AND metas.meta_value LIKE '%ewww_image_optimizer%' LIMIT {$import_status['media']}, 100", ARRAY_N);
    if (count($attachments) === 0) {
        $import_finished = true;
    } else {
        $import_status['media'] += count($attachments);
    }
    $already_optimized = array();
    $ewww_debug .= "importing " . count($attachments) . " attachments<br>";
    $insert_query = "INSERT INTO {$wpdb->ewwwio_images} (path, image_size, orig_size, results, temp) VALUES ";
    $rows = array();
    foreach ($attachments as $attachment) {
        $record = array();
        $gallery_type = 0;
        $id = $attachment[0];
        $meta = unserialize($attachment[1]);
        if (empty($attachment) || empty($attachment[1])) {
            continue;
        }
        list($attachment, $upload_path) = ewww_image_optimizer_attachment_path($meta, $id);
        if ('ims_image' == get_post_type($id)) {
            $gallery_type = 6;
        }
        // make sure the meta actually contains data for ewww_image_optimizer
        if (empty($meta['ewww_image_optimizer'])) {
            $prev_results = '';
        } else {
            $prev_results = $meta['ewww_image_optimizer'];
        }
        $record = ewww_image_optimizer_import_file($attachment, $prev_results, $already_optimized);
        if (!empty($record)) {
            $rows[] = "('{$record['0']}', '{$record['1']}', '{$record['2']}', '{$record['3']}', true)";
        }
        // resized versions, so we can continue
        if (isset($meta['sizes'])) {
            $record = array();
            $ewww_debug .= "processing resizes<br>";
            // meta sizes don't contain a path, so we calculate one
            if ($gallery_type === 6) {
                $base_dir = dirname($attachment) . '/_resized/';
            } else {
                $base_dir = dirname($attachment) . '/';
            }
            foreach ($meta['sizes'] as $size => $data) {
                $resize_path = $base_dir . $data['file'];
                $ewww_debug .= "current resize: {$resize_path}<br>";
                // make sure the meta actually contains data for ewww_image_optimizer
                if (empty($data['ewww_image_optimizer'])) {
                    $prev_results = '';
                } else {
                    $prev_results = $data['ewww_image_optimizer'];
                }
                $record = ewww_image_optimizer_import_file($resize_path, $prev_results, $already_optimized);
                if (!empty($record)) {
                    $rows[] = "('{$record['0']}', '{$record['1']}', '{$record['2']}', '{$record['3']}', true)";
                }
            }
        }
        ewww_image_optimizer_debug_log();
    }
    $import_count = $import_status['media'];
    //nextgen import
    if ($import_finished && isset($import_status['nextgen']) && class_exists('C_Component_Registry')) {
        $import_finished = false;
        $images = $wpdb->get_results("SELECT pid,meta_data,filename,galleryid FROM {$wpdb->nggpictures} WHERE meta_data LIKE '%ewww_image_optimizer%' LIMIT {$import_status['nextgen']}, 100", ARRAY_N);
        if (count($images) === 0) {
            $import_finished = true;
        } else {
            $import_status['nextgen'] += count($images);
        }
        $galleries = $wpdb->get_results("SELECT gid,path FROM {$wpdb->nggallery}", ARRAY_N);
        // creating the 'registry' object for working with nextgen
        $registry = C_Component_Registry::get_instance();
        // creating a database storage object from the 'registry' object
        $storage = $registry->get_utility('I_Gallery_Storage');
        $sizes = $storage->get_image_sizes();
        foreach ($images as $image) {
            $record = array();
            $gallery_path = '';
            foreach ($galleries as $gallery) {
                if ($gallery[0] == $image[3]) {
                    $gallery_path = trailingslashit($gallery[1]);
                }
            }
            if (class_exists('Ngg_Serializable')) {
                $serializer = new Ngg_Serializable();
                $meta = $serializer->unserialize($image[1]);
            } else {
                $meta = unserialize($image[1]);
            }
            // get an array of sizes available for the $image
            foreach ($sizes as $size) {
                // get the absolute path
                if ($size === 'full' || $size === 'original' || $size === 'image') {
                    if (!empty($meta['ewww_image_optimizer'])) {
                        $file_path = ABSPATH . $gallery_path . $image[2];
                        $ewww_debug .= "nextgen path generated: {$file_path}<br>";
                        $record = ewww_image_optimizer_import_file($file_path, $meta['ewww_image_optimizer'], $already_optimized);
                    }
                } elseif (!empty($meta[$size]['ewww_image_optimizer'])) {
                    if (isset($meta[$size]['filename'])) {
                        $file_path = ABSPATH . $gallery_path . trailingslashit('thumbs') . $meta[$size]['filename'];
                    } else {
                        $file_path = ABSPATH . $gallery_path . trailingslashit('thumbs') . 'thumbs_' . $image[2];
                    }
                    $ewww_debug .= "nextgen path generated: {$file_path}<br>";
                    $record = ewww_image_optimizer_import_file($file_path, $meta[$size]['ewww_image_optimizer'], $already_optimized);
                } elseif (!empty($meta['ewww_image_optimizer'])) {
                    if (isset($meta[$size]['filename'])) {
                        $file_path = ABSPATH . $gallery_path . trailingslashit('thumbs') . $meta[$size]['filename'];
                    } else {
                        $file_path = ABSPATH . $gallery_path . trailingslashit('thumbs') . 'thumbs_' . $image[2];
                    }
                    $ewww_debug .= "nextgen path generated: {$file_path}<br>";
                    $meta[$size]['ewww_image_optimizer'] = __('Unknown Savings', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    nggdb::update_image_meta($image[0], $meta);
                    $record = ewww_image_optimizer_import_file($file_path, $meta[$size]['ewww_image_optimizer'], $already_optimized);
                }
                if (!empty($record)) {
                    $rows[] = "('{$record['0']}', '{$record['1']}', '{$record['2']}', '{$record['3']}', true)";
                }
            }
            ewww_image_optimizer_debug_log();
        }
        $import_count += $import_status['nextgen'];
    }
    // fla gallery import
    if ($import_finished && isset($import_status['flag'])) {
        $import_finished = false;
        $images = $wpdb->get_results("SELECT pid,meta_data,filename,galleryid FROM {$wpdb->flagpictures} WHERE meta_data LIKE '%ewww_image_optimizer%' LIMIT {$import_status['flag']}, 100", ARRAY_N);
        $galleries = $wpdb->get_results("SELECT gid,path FROM {$wpdb->flaggallery}", ARRAY_N);
        if (count($images) === 0) {
            $import_finished = true;
        } else {
            $import_status['flag'] += count($images);
        }
        // need this file to work with flag meta
        foreach ($images as $image) {
            $record = array();
            $gallery_path = '';
            foreach ($galleries as $gallery) {
                if ($gallery[0] == $image[3]) {
                    $gallery_path = trailingslashit($gallery[1]);
                }
            }
            // get the image meta for the current ID
            $meta = unserialize($image[1]);
            $file_path = ABSPATH . $gallery_path . $image[2];
            $ewww_debug .= "flagallery path generated: {$file_path}<br>";
            if (!empty($meta['ewww_image_optimizer'])) {
                $record = ewww_image_optimizer_import_file($file_path, $meta['ewww_image_optimizer']);
                if (!empty($record)) {
                    $rows[] = "('{$record['0']}', '{$record['1']}', '{$record['2']}', '{$record['3']}', true)";
                }
                $thumb_path = ABSPATH . $gallery_path . 'thumbs/thumbs_' . $image[2];
                if (empty($meta['thumbnail']['ewww_image_optimizer'])) {
                    $meta['thumbnail']['ewww_image_optimizer'] = __('Unknown Savings', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    // update the image metadata in the db
                    flagdb::update_image_meta($id, $meta);
                    $record = ewww_image_optimizer_import_file($thumb_path, __('Unknown Savings', EWWW_IMAGE_OPTIMIZER_DOMAIN));
                } else {
                    $record = ewww_image_optimizer_import_file($thumb_path, $meta['thumbnail']['ewww_image_optimizer']);
                }
                if (!empty($record)) {
                    $rows[] = "('{$record['0']}', '{$record['1']}', '{$record['2']}', '{$record['3']}', true)";
                }
            }
            ewww_image_optimizer_debug_log();
        }
        $import_count += $import_status['flag'];
    }
    if (!empty($rows)) {
        $wpdb->query($insert_query . implode(', ', $rows));
        $rows = array();
    }
    if ($import_finished) {
        update_option('ewww_image_optimizer_imported', true);
        update_option('ewww_image_optimizer_import_status', '');
        $wpdb->query("ALTER TABLE {$wpdb->ewwwio_images} DROP temp");
        echo "<b>" . __('Finished importing', EWWW_IMAGE_OPTIMIZER_DOMAIN) . "</b>";
    } else {
        update_option('ewww_image_optimizer_import_status', $import_status);
        echo $import_count;
    }
    //	$elapsed = microtime(true) - $started;
    //	echo "<br>importing images took $elapsed seconds<br>";
    die;
}
 function unserialize_image_metadata($image)
 {
     if (class_exists('Mixin_DataMapper_Driver_Base')) {
         return Mixin_DataMapper_Driver_Base::unserialize($image->meta_data);
     }
     if (class_exists('Ngg_Serializable')) {
         return Ngg_Serializable::unserialize($image->meta_data);
     }
     return maybe_unserialize($image->meta_data);
 }