public function posterous_process_images($post, $Template)
 {
     $html = $post['postDescHTML'];
     // find posterous URLs
     // <img alt="Img_8371" height="333" src="http://getfile0.posterous.com/getfile/files.posterous.com/temp-2012-02-04/ybzoAslvztsefCumHsmxEuFjiEutyFpnhGanxcfyunylvDaoAhgpAxChyrnp/IMG_8371.jpg.scaled500.jpg" width="500"/>
     // <a href="http://getfile0.posterous.com/getfile/files.posterous.com/temp-2012-02-04/ybzoAslvztsefCumHsmxEuFjiEutyFpnhGanxcfyunylvDaoAhgpAxChyrnp/IMG_8371.jpg.scaled1000.jpg">
     $s = '/<img[^>]*src="[^"]*posterous\\.com[^"]*"[^>]*>/';
     $count = preg_match_all($s, $html, $matches);
     $PerchImage = $this->api->get('Image');
     $image_folder = $this->import_folder . '/image/';
     $Perch = Perch::fetch();
     $bucket = $Perch->get_resource_bucket($this->resource_bucket);
     if ($count) {
         foreach ($matches as $match) {
             $Tag = new PerchXMLTag($match[0]);
             // Find the file name
             $parts = explode('/', $Tag->src());
             $filename = array_pop($parts);
             $linkpath = str_replace($filename, '', $Tag->src());
             $fileparts = explode('.scaled', $filename);
             $filename = array_shift($fileparts);
             $linkpath .= $filename;
             // Find the temp-YYYY-MM-DD part of the path to find the image folder
             $s = '/\\/temp-([0-9]{4})-([0-9]{2})-[0-9]{2}\\//';
             $count = preg_match($s, $Tag->src(), $path_matches);
             if ($count) {
                 $folder = PerchUtil::file_path($image_folder . $path_matches[1] . '/' . $path_matches[2] . '/');
                 $files = PerchUtil::get_dir_contents($folder, false);
                 if (PerchUtil::count($files)) {
                     $l = strlen($filename);
                     $image_file = false;
                     foreach ($files as $file) {
                         PerchUtil::debug(substr($file, -$l));
                         if (substr($file, -$l) == $filename) {
                             $image_file = PerchUtil::file_path($folder . $file);
                             break;
                         }
                     }
                     if ($image_file) {
                         $new_image_file = PerchUtil::file_path($bucket['file_path'] . '/' . $file);
                         copy($image_file, $new_image_file);
                         $new_image = $PerchImage->resize_image($new_image_file, (int) $Tag->width(), (int) $Tag->height());
                         $img_html = '<img src="' . $new_image['web_path'] . '" width="' . $new_image['w'] . '" height="' . $new_image['h'] . '" alt="' . PerchUtil::html($Tag->alt()) . '" />';
                         if (defined('PERCH_XHTML_MARKUP') && PERCH_XHTML_MARKUP == false) {
                             $img_html = str_replace(' />', '>', $img_html);
                         }
                         $html = str_replace($match[0], $img_html, $html);
                         // find links to the bigger version
                         $s = '/<a[^>]*href="' . preg_quote($linkpath, '/') . '[^"]*"[^>]*>/';
                         $s = preg_replace('#getfile[0-9]{1,2}#', 'getfile[0-9]{1,2}', $s);
                         $count = preg_match_all($s, $html, $link_matches);
                         if ($count) {
                             $big_image = $PerchImage->resize_image($new_image_file, (int) $Tag->width() * 2, (int) $Tag->height() * 2);
                             $link_html = '<a href="' . $big_image['web_path'] . '">';
                             foreach ($link_matches as $link_match) {
                                 $html = str_replace($link_match[0], $link_html, $html);
                             }
                         } else {
                             PerchUtil::debug('FAIL', 'error');
                             PerchUtil::debug($new_image);
                             PerchUtil::debug($s);
                             PerchUtil::debug($link_matches);
                         }
                     }
                 }
             }
         }
     }
     $post['postDescHTML'] = $html;
     $post['postDescRaw'] = $html;
     return $post;
 }