scale external images

This commit is contained in:
friendica 2012-02-25 14:22:51 -08:00
parent 579738aff7
commit 2c7da5d0de
4 changed files with 49 additions and 37 deletions

View file

@ -55,42 +55,12 @@ function diaspora2bb($s) {
$s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s); $s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s);
// Don't show link to full picture (until it is fixed) // Don't show link to full picture (until it is fixed)
$s = scale_diaspora_images($s, false); $s = scale_external_images($s, false);
return $s; return $s;
} }
function scale_diaspora_images($s,$include_link = true) {
$matches = null;
$c = preg_match_all('/\[img\](.*?)\[\/img\]/ism',$s,$matches,PREG_SET_ORDER);
if($c) {
require_once('include/Photo.php');
foreach($matches as $mtch) {
logger('scale_diaspora_image: ' . $mtch[1]);
$i = fetch_url($mtch[1]);
if($i) {
$ph = new Photo($i);
if($ph->is_valid()) {
if($ph->getWidth() > 600 || $ph->getHeight() > 600) {
$ph->scaleImage(600);
$new_width = $ph->getWidth();
$new_height = $ph->getHeight();
logger('scale_diaspora_image: ' . $new_width . 'w ' . $new_height . 'h' . 'match: ' . $mtch[0], LOGGER_DEBUG);
$s = str_replace($mtch[0],'[img=' . $new_width . 'x' . $new_height. ']' . $mtch[1] . '[/img]'
. "\n" . (($include_link)
? '[url=' . $mtch[1] . ']' . t('view full size') . '[/url]' . "\n"
: ''),$s);
logger('scale_diaspora_image: new string: ' . $s, LOGGER_DEBUG);
}
}
}
}
}
return $s;
}
function stripdcode_br_cb($s) { function stripdcode_br_cb($s) {
return '[code]' . str_replace('<br />', "\n\t", $s[1]) . '[/code]'; return '[code]' . str_replace('<br />', "\n\t", $s[1]) . '[/code]';
} }

View file

@ -794,15 +794,15 @@ function diaspora_reshare($importer,$xml) {
if(strlen($source_xml->post->asphoto->objectId) && ($source_xml->post->asphoto->objectId != 0) && ($source_xml->post->asphoto->image_url)) { if(strlen($source_xml->post->asphoto->objectId) && ($source_xml->post->asphoto->objectId != 0) && ($source_xml->post->asphoto->image_url)) {
$body = '[url=' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->post->asphoto->objectId)) . '[/img][/url]' . "\n"; $body = '[url=' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->post->asphoto->objectId)) . '[/img][/url]' . "\n";
$body = scale_diaspora_images($body,false); $body = scale_external_images($body,false);
} }
elseif($source_xml->post->asphoto->image_url) { elseif($source_xml->post->asphoto->image_url) {
$body = '[img]' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '[/img]' . "\n"; $body = '[img]' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '[/img]' . "\n";
$body = scale_diaspora_images($body); $body = scale_external_images($body);
} }
elseif($source_xml->post->status_message) { elseif($source_xml->post->status_message) {
$body = diaspora2bb($source_xml->post->status_message->raw_message); $body = diaspora2bb($source_xml->post->status_message->raw_message);
$body = scale_diaspora_images($body); $body = scale_external_images($body);
} }
else { else {
@ -945,11 +945,11 @@ function diaspora_asphoto($importer,$xml) {
if(strlen($xml->objectId) && ($xml->objectId != 0) && ($xml->image_url)) { if(strlen($xml->objectId) && ($xml->objectId != 0) && ($xml->image_url)) {
$body = '[url=' . notags(unxmlify($xml->image_url)) . '][img]' . notags(unxmlify($xml->objectId)) . '[/img][/url]' . "\n"; $body = '[url=' . notags(unxmlify($xml->image_url)) . '][img]' . notags(unxmlify($xml->objectId)) . '[/img][/url]' . "\n";
$body = scale_diaspora_images($body,false); $body = scale_external_images($body,false);
} }
elseif($xml->image_url) { elseif($xml->image_url) {
$body = '[img]' . notags(unxmlify($xml->image_url)) . '[/img]' . "\n"; $body = '[img]' . notags(unxmlify($xml->image_url)) . '[/img]' . "\n";
$body = scale_diaspora_images($body); $body = scale_external_images($body);
} }
else { else {
logger('diaspora_asphoto: no photo url found.'); logger('diaspora_asphoto: no photo url found.');
@ -1476,7 +1476,7 @@ function diaspora_photo($importer,$xml,$msg) {
$link_text = '[img]' . $remote_photo_path . $remote_photo_name . '[/img]' . "\n"; $link_text = '[img]' . $remote_photo_path . $remote_photo_name . '[/img]' . "\n";
$link_text = scale_diaspora_images($link_text); $link_text = scale_external_images($link_text);
if(strpos($parent_item['body'],$link_text) === false) { if(strpos($parent_item['body'],$link_text) === false) {
$r = q("update item set `body` = '%s', `visible` = 1 where `id` = %d and `uid` = %d limit 1", $r = q("update item set `body` = '%s', `visible` = 1 where `id` = %d and `uid` = %d limit 1",

View file

@ -776,3 +776,43 @@ function add_fcontact($arr,$update = false) {
return $r; return $r;
} }
function scale_external_images($s,$include_link = true) {
$a = get_app();
$matches = null;
$c = preg_match_all('/\[img\](.*?)\[\/img\]/ism',$s,$matches,PREG_SET_ORDER);
if($c) {
require_once('include/Photo.php');
foreach($matches as $mtch) {
logger('scale_external_image: ' . $mtch[1]);
$hostname = str_replace('www.','',substr($a->get_baseurl(),strpos($a->get_baseurl(),'://')+3));
if(stristr($mtch[1],$hostname))
continue;
$i = fetch_url($mtch[1]);
if($i) {
$ph = new Photo($i);
if($ph->is_valid()) {
$orig_width = $ph->getWidth();
$orig_height = $ph->getHeight();
if($orig_width > 640 || $orig_height > 640) {
$ph->scaleImage(640);
$new_width = $ph->getWidth();
$new_height = $ph->getHeight();
logger('scale_external_images: ' . $orig_width . '->' . $new_width . 'w ' . $orig_height . '->' . $new_height . 'h' . ' match: ' . $mtch[0], LOGGER_DEBUG);
$s = str_replace($mtch[0],'[img=' . $new_width . 'x' . $new_height. ']' . $mtch[1] . '[/img]'
. "\n" . (($include_link)
? '[url=' . $mtch[1] . ']' . t('view full size') . '[/url]' . "\n"
: ''),$s);
logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG);
}
}
}
}
}
return $s;
}

View file

@ -400,6 +400,8 @@ function item_post(&$a) {
$body = preg_replace('/\[\/code\]\s*\[code\]/ism',"\n",$body); $body = preg_replace('/\[\/code\]\s*\[code\]/ism',"\n",$body);
$body = scale_external_images($body,false);
/** /**
* Look for any tags and linkify them * Look for any tags and linkify them
*/ */