<?php



function parseImagesOntext(&$text){
	// dsm($vars, 'vars');

	# parse images : left only the first in teaser, then replace all with image_cache
	preg_match_all('/<img[^>]+>/i',$text, $match_result);
	$allimgs = $match_result[0];
	// dsm($allimgs, '$allimgs');

	if(count($allimgs)){

		// if($vars['teaser']){

		// 	$first_img = array_shift($allimgs);
		// 	// dsm($first_img, '$first_img');

		// 	$img_link = l($first_img, 'node/'.$vars['nid'], array(
		// 		'html' => true,
		// 		'attributes' => array(
		// 			'title' => htmlspecialchars($vars['title'])
		// 		),
		// 	));

		// 	// dsm($img_link, '$img_link');
		// 	$vars['content'] = str_replace($first_img, $img_link, $vars['content']);

		// 	if(count($allimgs)){
		// 		foreach ($allimgs as $key => $img)
		// 			$vars['content'] = str_replace($img, '', $vars['content']);
		// 	}

		// 	$allimgs = array($first_img);
		// 	$ic_preset = 'content_teaser';

		// }else{
		// 	$ic_preset = 'content_full';
		// }
		// $cached_img = array();

		$preset = 'content_full';

		foreach ($allimgs as $key => $img){
			preg_match('/src="([^"]*)"/', $img, $src);
			if(!preg_match('/^http:\/\//', $src[1])
				&& !preg_match('/\.gif$/', $src[1])
				){
				preg_match('/alt="([^"]*)"/', $img, $alt);
				preg_match('/title="([^"]*)"/', $img, $title);
				// dsm($src, '$src');
				// theme_imagecache($presetname, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE, $absolute = TRUE)


				// $cached_img = theme('imagecache', $ic_preset, preg_replace('/^\//', '', $src[1]), $alt[1], $title[1]);
				// $image_infos['attributes']['class'] = 'image '.$preset;
				// $image_infos['path'] = image_style_url($preset, preg_replace('/^\//', '', $src[1]));
				// // dsm($image_infos['path'], 'path');
    //   	$cached_img = theme('image', $image_infos);

				$options['style_name'] = $preset;
				$options['path'] = preg_replace('/^\//', '', $src[1]);
				$options['alt'] = isset($alt[1]) ? $alt[1]:"";
				$options['title'] = isset($title[1]) ? $title[1]:"";

				$cached_img = theme('image_style', $options);

				// dsm($img, 'img');
				// dsm($cached_img, 'cached_img');


				$text = str_replace($img, $cached_img, $text);

			}
		}
	}
}