Skip to content

Commit

Permalink
refs #514
Browse files Browse the repository at this point in the history
  * synchronize with 1.8 branch
  • Loading branch information
inureyes committed Sep 15, 2010
1 parent 31a274a commit 2ecb7f9
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 128 deletions.
2 changes: 1 addition & 1 deletion framework/data/DBModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function doesExistTable($tablename) {
class DBModel extends Singleton implements IModel {
protected $_attributes, $_qualifiers, $_query;
protected $_relations, $_filters, $_order, $_limitation, $table, $id, $_reservedFields, $_isReserved, $param;

function __construct($table = null) {
$this->context = Model_Context::getInstance();
$this->reset($table);
Expand Down
119 changes: 119 additions & 0 deletions framework/utils/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,5 +520,124 @@ function hexRGB($hexstr) {
$int = hexdec($hexstr);
return array('R' => 0xFF & ($int >> 0x10), 'G' => 0xFF & ($int >> 0x8), 'B' => 0xFF & $int);
}

// 본문에 있는 첨부파일이미지 리스트를 뽑아냄
function getAttachmentExtracts($content, $blogid = null){
if (is_null($blogid)) $blogid = getBlogId();

$result = $temp = array();
if (preg_match_all('/\[##_(1R|1L|1C|2C|3C|iMazing|Gallery)\|[^|]*\.(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG)\|(.[^\[]*)_##\]/mi', $content, $matches)) {
foreach ($matches[0] as $image) {
$split = explode("|", $image);
if (!in_array($split[1], $temp)) $temp[] = $split[1];
}
}

if (preg_match_all('/<img[^>]+?src=("|\')?([^\'">]*?)("|\')/mi', $content, $matches)) {
foreach ($matches[2] as $image)
if (!in_array(basename($image), $temp)) $temp[] = basename($image);
}

foreach($temp as $filename) {
if (preg_match('/(.+)\.w(\d{1,})\-h(\d{1,})\.(.+)/', $filename, $matches))
$filename = $matches[1].'.'.$matches[4];

if (file_exists(ROOT."/attach/{$blogid}/{$filename}") && !in_array($filename, $result))
$result[] = $filename;
}

return $result;
}

function getImageResizer($filename, $options = null, $blogid = null) {
// version 1.2.5.1
// usages :
// $options = array('size'=>100) // resize & crop to square
// $options = array('width'=>100) // resize by width
// $options = array('width'=>100, 'height'=>50) // resize & crop by width and height
// $options = array('force'=>true) // refresh image
// result : $url, $width, $height, $path

$context = Model_Context::getInstance();

if (is_null($blogid)) $blogid = getBlogId();
$force = isset($options['force']) ? $options['force'] : false;
$absolute = isset($options['absolute']) ? $options['absolute'] : true;

$originSrc = ROOT."/attach/{$blogid}/{$filename}";
$originURL = ($absolute ? $context->getProperty('uri.service'):$context->getProperty('uri.path'))."/attach/{$blogid}/{$filename}";

if (!file_exists($originSrc)) return false;

$imageInfo = getimagesize($originSrc);
if ($imageInfo === false || count($imageInfo) < 1) return false;
$originWidth = $imageInfo[0];
$originHeight = $imageInfo[1];

if (!extension_loaded('gd')) return array($originURL, $originWidth, $originHeight, $originSrc);

if (!is_dir(ROOT."/cache/thumbnail")) {
@mkdir(ROOT."/cache/thumbnail");
@chmod(ROOT."/cache/thumbnail", 0777);
}
if (!is_dir(ROOT."/cache/thumbnail/" . $blogid)) {
@mkdir(ROOT."/cache/thumbnail/" .$blogid);
@chmod(ROOT."/cache/thumbnail/" . $blogid, 0777);
}

$this->imageFile = $originSrc;
if (isset($options['size']) && is_numeric($options['size'])) {
if ($imageInfo[0] > $imageInfo[1])
list($tempWidth, $tempHeight) = $this->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], NULL, $options['size']);
else
list($tempWidth, $tempHeight) = $this->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], $options['size'], null);

$resizeWidth = $resizeHeight = $options['size'];
} else if (isset($options['width']) && is_numeric($options['width']) && isset($options['height']) && is_numeric($options['height'])) {
if ($options['width'] / $options['height'] > intval($imageInfo[0]) / intval($imageInfo[1]))
list($tempWidth, $tempHeight) = $this->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], $options['width'], NULL);
else
list($tempWidth, $tempHeight) = $this->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], NULL, $options['height']);

$resizeWidth = $options['width'];
$resizeHeight = $options['height'];
} else {
if (isset($options['width']) && is_numeric($options['width'])) {
list($tempWidth, $tempHeight) = $this->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], $options['width'], NULL);
} elseif (isset($options['height']) && is_numeric($options['height'])) {
list($tempWidth, $tempHeight) = $this->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], NULL, $options['height']);
} else {
$this->reset();
return array($originURL, $originWidth, $originHeight, $originSrc);
}

$resizeWidth = $tempWidth;
$resizeHeight = $tempHeight;
}
$resizeFilename = preg_replace("/\\.([[:alnum:]]+)$/i", ".w{$resizeWidth}-h{$resizeHeight}.\\1", $filename);
$resizeSrc = ROOT . "/cache/thumbnail/{$blogid}/{$resizeFilename}";
$resizeURL = ($absolute ? $context->getProperty('uri.service'):$context->getProperty('uri.path')) . "/cache/thumbnail/{$blogid}/{$resizeFilename}";

if($force) @unlink($resizeSrc);

if (file_exists($resizeSrc)) {
$this->reset();
return array($resizeURL, $resizeWidth, $resizeHeight, $resizeSrc);
}
if ($this->resample($tempWidth, $tempHeight)) {
if (isset($options['width']) && is_numeric($options['width']) && isset($options['height']) && is_numeric($options['height'])) {
@$this->cropRectBySize($options['width'], $options['height']);
} else if (isset($options['size']) && is_numeric($options['size'])) {
@$this->cropRectBySize($options['size'], $options['size']);
}
if ($this->saveAsFile($resizeSrc)) {
@chmod($resizeSrc, 0666);
$this->reset();
return array($resizeURL, $resizeWidth, $resizeHeight, $resizeSrc);
}
}
$this->reset();
return array($originURL, $originWidth, $originHeight, $originSrc);
}
}
?>
122 changes: 1 addition & 121 deletions library/function/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// See the GNU General Public License for more details. (/documents/LICENSE, /documents/COPYRIGHT)

// img의 width/height에 맞춰 이미지를 리샘플링하는 함수. 썸네일 함수가 아님! 주의.
// resampleImage는 더 이상 사용되지 않습니다.
function resampleImage($imgString, $filename, $useAbsolutePath = true) {
$blogid = getBlogId();
$context = Model_Context::getInstance();
Expand Down Expand Up @@ -54,125 +55,4 @@ function resampleImage($imgString, $filename, $useAbsolutePath = true) {

return $imgString;
}

// 본문으로 부터 포함된 모든 첨부이미지 파일명 추출
function getAttachmentExtracts($content, $blogid = null){
if (is_null($blogid)) $blogid = getBlogId();

$result = $temp = array();
if (preg_match_all('/\[##_(1R|1L|1C|2C|3C|iMazing|Gallery)\|[^|]*\.(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG)\|(.[^\[]*)_##\]/mi', $content, $matches)) {
foreach ($matches[0] as $image) {
$split = explode("|", $image);
if (!in_array($split[1], $temp)) $temp[] = $split[1];
}
}

if (preg_match_all('/<img[^>]+?src=("|\')?([^\'">]*?)("|\')/mi', $content, $matches)) {
foreach ($matches[2] as $image)
if (!in_array(basename($image), $temp)) $temp[] = basename($image);
}

foreach($temp as $filename) {
if (preg_match('/(.+)\.w(\d{1,})\-h(\d{1,})\.(.+)/', $filename, $matches))
$filename = $matches[1].'.'.$matches[4];

if (file_exists(ROOT."/attach/{$blogid}/{$filename}") && !in_array($filename, $result))
$result[] = $filename;
}

return $result;
}

function getImageResizer($filename, $options = null, $blogid = null) {
// version 1.2.5
// usages :
// $options = array('size'=>100) // resize & crop to square
// $options = array('width'=>100) // resize by width
// $options = array('width'=>100, 'height'=>50) // resize & crop by width and height
// $options = array('force'=>true) // refresh image
// result : $url, $width, $height, $path

$context = Model_Context::getInstance();

if (is_null($blogid)) $blogid = getBlogId();
$force = isset($options['force']) ? $options['force'] : false;
$absolute = isset($options['absolute']) ? $options['absolute'] : true;

$originSrc = ROOT."/attach/{$blogid}/{$filename}";
$originURL = ($absolute ? $context->getProperty('uri.service'):$context->getProperty('uri.path'))."/attach/{$blogid}/{$filename}";

if (!file_exists($originSrc)) return false;

$imageInfo = getimagesize($originSrc);
if ($imageInfo === false || count($imageInfo) < 1) return false;
$originWidth = $imageInfo[0];
$originHeight = $imageInfo[1];

if (!extension_loaded('gd')) return array($originURL, $originWidth, $originHeight, $originSrc);

if (!is_dir(ROOT."/cache/thumbnail")) {
@mkdir(ROOT."/cache/thumbnail");
@chmod(ROOT."/cache/thumbnail", 0777);
}
if (!is_dir(ROOT."/cache/thumbnail/" . $blogid)) {
@mkdir(ROOT."/cache/thumbnail/" .$blogid);
@chmod(ROOT."/cache/thumbnail/" . $blogid, 0777);
}

requireComponent('Textcube.Function.Image');
$objResize = new Image();
$objResize->imageFile = $originSrc;
if (isset($options['size']) && is_numeric($options['size'])) {
if ($imageInfo[0] > $imageInfo[1])
list($tempWidth, $tempHeight) = $objResize->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], NULL, $options['size']);
else
list($tempWidth, $tempHeight) = $objResize->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], $options['size'], null);

$resizeWidth = $resizeHeight = $options['size'];
} else if (isset($options['width']) && is_numeric($options['width']) && isset($options['height']) && is_numeric($options['height'])) {
if ($options['width'] / $options['height'] > intval($imageInfo[0]) / intval($imageInfo[1]))
list($tempWidth, $tempHeight) = $objResize->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], $options['width'], NULL);
else
list($tempWidth, $tempHeight) = $objResize->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], NULL, $options['height']);

$resizeWidth = $options['width'];
$resizeHeight = $options['height'];
} else {
if (isset($options['width']) && is_numeric($options['width'])) {
list($tempWidth, $tempHeight) = $objResize->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], $options['width'], NULL);
} elseif (isset($options['height']) && is_numeric($options['height'])) {
list($tempWidth, $tempHeight) = $objResize->calcOptimizedImageSize($imageInfo[0], $imageInfo[1], NULL, $options['height']);
} else {
unset($objResize);
return array($originURL, $originWidth, $originHeight, $originSrc);
}

$resizeWidth = $tempWidth;
$resizeHeight = $tempHeight;
}
$resizeFilename = preg_replace("/\\.([[:alnum:]]+)$/i", ".w{$resizeWidth}-h{$resizeHeight}.\\1", $filename);
$resizeSrc = ROOT . "/cache/thumbnail/{$blogid}/{$resizeFilename}";
$resizeURL = ($absolute ? $context->getProperty('uri.service'):$context->getProperty('uri.path')) . "/cache/thumbnail/{$blogid}/{$resizeFilename}";

if($force) @unlink($resizeSrc);

if (file_exists($resizeSrc)) {
unset($objResize);
return array($resizeURL, $resizeWidth, $resizeHeight, $resizeSrc);
}
if ($objResize->resample($tempWidth, $tempHeight)) {
if (isset($options['width']) && is_numeric($options['width']) && isset($options['height']) && is_numeric($options['height'])) {
@$objResize->cropRectBySize($options['width'], $options['height']);
} else if (isset($options['size']) && is_numeric($options['size'])) {
@$objResize->cropRectBySize($options['size'], $options['size']);
}
if ($objResize->saveAsFile($resizeSrc)) {
@chmod($resizeSrc, 0666);
unset($objResize);
return array($resizeURL, $resizeWidth, $resizeHeight, $resizeSrc);
}
}
unset($objResize);
return array($originURL, $originWidth, $originHeight, $originSrc);
}
?>
14 changes: 8 additions & 6 deletions library/view/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -1239,16 +1239,18 @@ function getEntryContentView($blogid, $id, $content, $formatter, $keywords = arr
}

$tempFileName = array_pop(explode('/', $images[$i][1]));
if (preg_match('/(.+)\.w(\d{1,})\-h(\d{1,})\.(.+)/', $tempFileName, $matches))
$tempFileName = $matches[1].'.'.$matches[4];

$newImage = $images[$i][0];
if (file_exists(ROOT . "/attach/{$blogid}/{$tempFileName}")) {
$tempAttributes = Misc::getAttributesFromString($images[$i][2]);
$tempOriginInfo = getimagesize(ROOT . "/attach/{$blogid}/{$tempFileName}");
if (isset($tempAttributes['width']) && ($tempOriginInfo[0] > $tempAttributes['width']))
$newImage = resampleImage($images[$i][0], $tempFileName, $useAbsolutePath);
else
$newImage = $images[$i][0];
} else {
$newImage = $images[$i][0];
if (isset($tempAttributes['width']) && ($tempOriginInfo[0] > $tempAttributes['width'])) {
$image = Utils_Image::getInstance();
list($tempImageURL, $tempImageWidth, $tempImageHeight, $tempImageSrc) = $image->getImageResizer($tempFileName, array('width' => $tempAttributes['width']));
$newImage = "<img src=\"{$tempImageURL}\" width=\"{$tempImageWidth}\" height=\"{$tempImageHeight}\" alt=\"resize_image\" />";
}
}
$view = preg_replace('@\[#####_#####_#####_image_#####_#####_#####\]@', $newImage, $view, 1);
}
Expand Down

0 comments on commit 2ecb7f9

Please sign in to comment.