Imagefield
Images are much like the filefield import, you'll actually see it used here in the code. Unlike attached files, images are also commonly just embedded into HTML (or in this Xoops migration case BB Code as well) and not recorded in any special field. To make sure that images were actually recorded and stored correctly the content was parsed:-
function _x_node_files($text) {
$img = array();
$up = array();
// BBcode
if (preg_match_all('#\[img[^\]]*\]([\w:;&,~%+!=@\/\.\-\#\?]+)\[/img(?::\w+)?\]#si', $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $url) {
// test if it's local - discard if not, discard http://.*nabuur.com/ if it is
$url[1] = preg_replace('#^(?:http://)?(?:www\.)?nabuur\.com#i','',$url[1]);
if (stripos($url[1],'http')===0 || stripos($url[1],'www.')===0) {
print ' - skipping non-local image '. $url[1];
continue;
}
if (strpos($url[1],'/')===0) {
$url[1] = drupal_substr($url[1],1);
}
$cur_img = _x_node_create_file($url[1]);
// @todo test mime-type to put in the correct container
if (drupal_substr($cur_img['filemime'], 0, 5) == 'image') {
$img[] = $cur_img;
}
else {
$up[] = $cur_img;
}
}
}
// HTML
if (preg_match_all('#<img[^>]*src="([^"]*)"[^>]*>#Usi', $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $url) {
// test if it's local - discard if not, discard http://.*nabuur.com/ if it is
$url[1] = preg_replace('#^(?:http://)?(?:www\.)?nabuur\.com#i','',$url[1]);
if (stripos($url[1],'http')===0 || stripos($url[1],'www.')===0) {
print ' - skipping non-local image '. $url[1];
continue;
}
if (strpos($url[1],'/')===0) {
$url[1] = drupal_substr($url[1],1);
}
$cur_img = _x_node_create_file($url[1]);
// @todo test mime-type to put in the correct container
if (drupal_substr($cur_img['filemime'], 0, 5) == 'image') {
$img[] = $cur_img;
}
else {
$up[] = $cur_img;
}
}
}
if (count($img)) {
print ' - retrieved images: ';
foreach ($img as $image) {
print $image['filepath'] .', ';
}
}
if (count($up)) {
print ' - retrieved uploads: ';
foreach ($up as $image) {
print $image['filepath'] .', ';
}
}
return array($img, $up);
}
$img = array();
$up = array();
// BBcode
if (preg_match_all('#\[img[^\]]*\]([\w:;&,~%+!=@\/\.\-\#\?]+)\[/img(?::\w+)?\]#si', $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $url) {
// test if it's local - discard if not, discard http://.*nabuur.com/ if it is
$url[1] = preg_replace('#^(?:http://)?(?:www\.)?nabuur\.com#i','',$url[1]);
if (stripos($url[1],'http')===0 || stripos($url[1],'www.')===0) {
print ' - skipping non-local image '. $url[1];
continue;
}
if (strpos($url[1],'/')===0) {
$url[1] = drupal_substr($url[1],1);
}
$cur_img = _x_node_create_file($url[1]);
// @todo test mime-type to put in the correct container
if (drupal_substr($cur_img['filemime'], 0, 5) == 'image') {
$img[] = $cur_img;
}
else {
$up[] = $cur_img;
}
}
}
// HTML
if (preg_match_all('#<img[^>]*src="([^"]*)"[^>]*>#Usi', $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $url) {
// test if it's local - discard if not, discard http://.*nabuur.com/ if it is
$url[1] = preg_replace('#^(?:http://)?(?:www\.)?nabuur\.com#i','',$url[1]);
if (stripos($url[1],'http')===0 || stripos($url[1],'www.')===0) {
print ' - skipping non-local image '. $url[1];
continue;
}
if (strpos($url[1],'/')===0) {
$url[1] = drupal_substr($url[1],1);
}
$cur_img = _x_node_create_file($url[1]);
// @todo test mime-type to put in the correct container
if (drupal_substr($cur_img['filemime'], 0, 5) == 'image') {
$img[] = $cur_img;
}
else {
$up[] = $cur_img;
}
}
}
if (count($img)) {
print ' - retrieved images: ';
foreach ($img as $image) {
print $image['filepath'] .', ';
}
}
if (count($up)) {
print ' - retrieved uploads: ';
foreach ($up as $image) {
print $image['filepath'] .', ';
}
}
return array($img, $up);
}
