From 2c7da5d0de29cd7010bd6a34c0b6cfb79fe24466 Mon Sep 17 00:00:00 2001 From: friendica Date: Sat, 25 Feb 2012 14:22:51 -0800 Subject: [PATCH] scale external images --- include/bb2diaspora.php | 32 +------------------------------- include/diaspora.php | 12 ++++++------ include/network.php | 40 ++++++++++++++++++++++++++++++++++++++++ mod/item.php | 2 ++ 4 files changed, 49 insertions(+), 37 deletions(-) diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index bcef86616b..8487f845a6 100755 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -55,42 +55,12 @@ function diaspora2bb($s) { $s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s); // 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; } -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) { return '[code]' . str_replace('
', "\n\t", $s[1]) . '[/code]'; } diff --git a/include/diaspora.php b/include/diaspora.php index 5896f1bfd4..dca857a198 100755 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -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)) { $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) { $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) { $body = diaspora2bb($source_xml->post->status_message->raw_message); - $body = scale_diaspora_images($body); + $body = scale_external_images($body); } else { @@ -945,11 +945,11 @@ function diaspora_asphoto($importer,$xml) { 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 = scale_diaspora_images($body,false); + $body = scale_external_images($body,false); } elseif($xml->image_url) { $body = '[img]' . notags(unxmlify($xml->image_url)) . '[/img]' . "\n"; - $body = scale_diaspora_images($body); + $body = scale_external_images($body); } else { 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 = scale_diaspora_images($link_text); + $link_text = scale_external_images($link_text); 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", diff --git a/include/network.php b/include/network.php index 25db62d161..531c3ea4c8 100755 --- a/include/network.php +++ b/include/network.php @@ -776,3 +776,43 @@ function add_fcontact($arr,$update = false) { 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; +} diff --git a/mod/item.php b/mod/item.php index 3035989f36..9e0768a658 100755 --- a/mod/item.php +++ b/mod/item.php @@ -400,6 +400,8 @@ function item_post(&$a) { $body = preg_replace('/\[\/code\]\s*\[code\]/ism',"\n",$body); + $body = scale_external_images($body,false); + /** * Look for any tags and linkify them */