/**
 * fetch today's news regarding objects monitored by the user
 *
 * @param string $user - OppUser object
 * @return void
 * @author Guglielmo Celata
 */
function opp_sync_politician_image($pol)
{
    $start_time = microtime(true);
    $success = true;
    echo pakeColor::colorize(sprintf('Processing politician %s...', $pol), array('fg' => 'red', 'bold' => true));
    // invoke the remote getPolImage function to grab the images from op_openpolis
    $remote_img_url = sfConfig::get('app_remote_politicians_images_service_url') . '/' . sfConfig::get('app_remote_openpolis_api_key') . '/' . $pol->getId();
    /* debug
      echo pakeColor::colorize(sprintf('Url:  %s...', $remote_img_url), 
                              array('fg' => 'red', 'bold' => true));
      */
    $file = fopen($remote_img_url, "r");
    if (!$file) {
        $err = "unable to open remote file.";
        $success = false;
    }
    $remote_img_str = '';
    while (!feof($file)) {
        $remote_img_str .= fgets($file, 1024);
    }
    fclose($file);
    $images_root = SF_ROOT_DIR . DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'parlamentari' . DIRECTORY_SEPARATOR;
    // resizes images and stores them in the FS
    $picture = new sfImage();
    $picture->setMimeType('image/jpeg');
    $picture->loadString($remote_img_str);
    $picture->resize(91, null);
    $picture->saveAs($images_root . 'picture/' . $pol->getId() . '.jpeg', 'image/jpeg');
    $thumb = new sfImage();
    $thumb->setMimeType('image/jpeg');
    $thumb->loadString($remote_img_str);
    $thumb->resize(40, null);
    $thumb->saveAs($images_root . 'thumb/' . $pol->getId() . '.jpeg', 'image/jpeg');
    $execution_time = microtime(true) - $start_time;
    if ($success) {
        echo " ok (";
    } else {
        echo " {$err} (";
    }
    echo pakeColor::colorize(sprintf("%f", $execution_time), array('fg' => 'cyan'));
    echo ")\n";
}
 protected function updateRemoteImage($image, Swift_Message $message, $templateName)
 {
     $fileName = dmOs::join(sfConfig::get('sf_web_dir'), 'cache/mail_templates', md5($image->src . $image->width . $image->height) . pathinfo($image->src, PATHINFO_EXTENSION));
     if (!file_exists($fileName)) {
         $imageData = file_get_contents($image->src);
         if (!$imageData) {
             $this->logError($templateName, 'remote', 'image', $image->src);
             $image->src = '';
             return $image;
         } else {
             $sfImage = new sfImage();
             $sfImage->loadString($imageData);
             $width = $sfImage->getWidth();
             $height = $sfImage->getHeight();
             if (isset($image->width) && strpos($image->width, '%') === false) {
                 $width = $image->width;
             }
             if (isset($image->height) && strpos($image->height, '%') === false) {
                 $width = $image->height;
             }
             $sfImage->setQuality(dmConfig::get('image_resize_quality'));
             $sfImage->thumbnail($width, $height, dmConfig::get('image_resize_method'), null);
             $fileSystem = $this->serviceContainer->getService('filesystem');
             if (!file_exists(dirname($fileName))) {
                 $fileSystem->mkdir(dirname($fileName));
             }
             $sfImage->saveAs($fileName);
             chmod($fileName, 0777);
         }
     }
     if (dmConfig::get('mail_template_embed_remote_images_as_attachments', true)) {
         $image->src = $message->embed(Swift_Image::fromPath($fileName));
     } else {
         $image->src = $this->basePath . str_replace(sfConfig::get('sf_web_dir'), '', $fileName);
     }
     return $image;
 }