Browse Source

- Rewrite the picture thumbnail generation for the Media widget

- Clean old functions
- Fix Media viewer
pull/16/head
Jaussoin Timothée 12 years ago
parent
commit
c8f78c730a
  1. 14
      app/widgets/Media/Media.php
  2. 5
      app/widgets/Media/media.css
  3. 11
      bootstrap.php
  4. 11
      system/User.php
  5. 366
      system/Utils.php
  6. 57
      system/UtilsPicture.php
  7. 19
      upload.php

14
app/widgets/Media/Media.php

@ -41,8 +41,6 @@ class Media extends WidgetBase {
function ajaxDeleteItem($name)
{
unlink($this->user->userdir.'thumb_'.$name);
unlink($this->user->userdir.'medium_'.$name);
unlink($this->user->userdir.$name);
$this->ajaxRefreshMedia();
@ -52,17 +50,18 @@ class Media extends WidgetBase {
{
$html = '<ul class="thumb">';
foreach($this->user->getDir() as $key => $value) {
foreach($this->user->getDir() as $file) {
$p = new \Picture;
$html .=
'<li style="background-image: url('.$value['thumb'].');">
<a href="'.Route::urlize('media', $key).'">
'<li style="background-image: url('.$p->get($this->user->userdir.$file, 300).');">
<a href="'.Route::urlize('media', $file).'">
</a>
<div
class="remove"
onclick="'.
$this->genCallAjax(
'ajaxDeleteItem',
"'".$key."'"
"'".$file."'"
).'">
x
</div>
@ -95,7 +94,6 @@ class Media extends WidgetBase {
if(file_exists($this->user->userdir.$f) && getimagesize($this->user->userdir.$f) != 0) {
$er = @exif_read_data($this->user->userdir.$f);
$exif = '';
@ -120,7 +118,7 @@ class Media extends WidgetBase {
$html = '
<div class="viewer">
<img src="'.$this->user->useruri.'medium_'.$f.'"/>
<img src="'.$this->user->useruri.$f.'" style="max-width: '.$er['COMPUTED']['Width'].'px"/>
<div class="exif">
<ul>

5
app/widgets/Media/media.css

@ -74,6 +74,7 @@
#viewer .viewer img {
width: 100%;
margin: 0 auto;
height: auto;
display: block;
}
@ -96,15 +97,13 @@
float: left;
overflow: hidden;
white-space: nowrap;
line-height: 1.3em;
text-overflow: ellipsis;
}
#viewer .viewer .exif span {
font-weight: bold;
display: inline-block;
text-align: right;
margin-right: 1em;
width: 5em;
}
#media .size {

11
bootstrap.php

@ -47,13 +47,13 @@ class Bootstrap {
$this->setContants();
//Check if vital system need is OK
$this->checkSystem();
$this->setBrowserSupport();
$this->loadSystem();
$this->loadCommonLibraries();
$this->loadDispatcher();
$this->loadHelpers();
$this->setTimezone();
@ -126,6 +126,7 @@ class Bootstrap {
define('CACHE_PATH', DOCUMENT_ROOT . '/cache/');
define('VIEWS_PATH', DOCUMENT_ROOT . '/app/views/');
define('HELPERS_PATH', DOCUMENT_ROOT . '/app/helpers/');
if (!defined('DOCTYPE')) {
define('DOCTYPE','text/html');
@ -191,6 +192,12 @@ class Bootstrap {
// The template lib
require_once(LIB_PATH . 'RainTPL.php');
}
private function loadHelpers() {
foreach(glob(HELPERS_PATH."*Helper.php") as $file) {
require $file;
}
}
private function loadDispatcher() {
require_once(SYSTEM_PATH . "template/TplPageBuilder.php");
@ -201,8 +208,6 @@ class Bootstrap {
require_once(SYSTEM_PATH . "controllers/FrontController.php");
require_once(SYSTEM_PATH . "widget/WidgetBase.php");
require_once(SYSTEM_PATH . "widget/WidgetWrapper.php");

11
system/User.php

@ -56,7 +56,7 @@ class User {
$sum = 0;
foreach($this->getDir() as $s)
$sum = $sum + filesize($s['dir']);
$sum = $sum + filesize($this->userdir.$s);
return $sum;
}
@ -72,16 +72,17 @@ class User {
if(
$s != '.' &&
$s != '..' &&
substr($s, 0, 6) != 'thumb_' &&
substr($s, 0, 7) != 'medium_' &&
//substr($s, 0, 6) != 'thumb_' &&
//substr($s, 0, 7) != 'medium_' &&
$s != 'index.html') {
$file = array(
/*$file = array(
'uri' => $this->useruri.$s,
'dir' => $this->userdir.$s,
'thumb' => $this->useruri.'thumb_'.$s,
'medium' => $this->useruri.'medium_'.$s);
$dir[$s] = $file;
$dir[$s] = $file;*/
array_push($dir, $s);
}
}

366
system/Utils.php

@ -16,250 +16,6 @@
* All rights reserved.
*/
// Handy.
function println($string)
{
$args = func_get_args();
echo call_user_func_array('sprintf', $args) . PHP_EOL;
}
function sprintln($string)
{
$args = func_get_args();
return call_user_func_array('sprintf', $args) . PHP_EOL;
}
/**
* Prepare the string (add the a to the links and show the smileys)
*
* @param string $string
* @return string
*/
function prepareString($string) {
$smileys =
array(
// HFR icons
":'\(" => 'cry.gif',
':love:'=> 'love.gif',
'O:\)' => 'ange.gif',
'O:-\)' => 'ange.gif',
':redface:' => 'redface.gif',
':petrus:' => 'petrus75.gif',
// famfamfam icons
':\)\)' => 'grin.png',
':\)' => 'smiley.png',
':-\)' => 'smiley.png',
':\(' => 'sad.png',
':o' => 'shocked.png',
':O' => 'shocked.png',
':D' => 'grin.png',
':d' => 'grin.png',
':p' => 'tongue.png',
':P' => 'tongue.png',
':-P' => 'tongue.png',
';D' => 'wink.png',
';d' => 'wink.png',
';\)' => 'wink.png',
'\^\^' => 'happy.png',
'\(k\)' => 'heart.png',
'B\)' => 'cool.png',
':s' => 'confused.png',
':S' => 'confused.png',
':\/' => 'wondering.png',
':evil:'=> 'evil.png',
":\|" => 'neutral.png',
// Meme icons
':okay:' => 'okay.gif',
':trolldad:' => 'trolldad.png',
':epic:' => 'epic.png',
':aloneyeah:' => 'aloneyeah.png',
':fapfap:' => 'fapfap.png',
':megusta:' => 'gusta.png',
':trollface:' => 'trollface.png',
':troll:' => 'trollface.png',
':lol:' => 'trollol.png',
':genius:' => 'genius.png',
);
//replace begin by www
$string = preg_replace_callback(
'/(^|\s|>)(www.[^<> \n\r]+)/ix', function ($match) {
//print '<br />preg[1]';\system\Debug::dump($match);
if (strlen($match[2])>0) {
return stripslashes($match[1].'<a href=\"http://'.$match[2].'\" target=\"_blank\">'.$match[2].'</a>');
} else {
return $match[2];
}
}, ' ' . $string
);
//replace begin by http - https (before www)
$string = preg_replace_callback(
'/(?(?=<a[^>]*>.+<\/a>)(?:<a[^>]*>.+<\/a>)|([^="\'])((?:https?):\/\/([^<> \n\r]+)))/ix', function ($match) {
if (isset($match[2]) && strlen($match[2])>0) {
return stripslashes($match[1].'<a href=\"'.$match[2].'\" target=\"_blank\">'.$match[3].'</a>');
} else {
return $match[0];
}
}, ' ' . $string
);
// Twitter hashtags
$string = preg_replace_callback(
"/ #[a-zA-Z0-9_-]*/", function ($match) {
return
' <a class="twitter hastag" href="http://twitter.com/search?q='.
urlencode(trim($match[0])).
'&src=hash" target="_blank">'.
trim($match[0]).
'</a>';
}, ' ' . $string
);
$string = preg_replace_callback(
"/ @[a-zA-Z0-9_-]*/", function ($match) {
return
' <a class="twitter at" href="http://twitter.com/'.
trim($match[0]).
'" target="_blank">'.
trim($match[0]).
'</a>';
}, ' ' . $string
);
//remove all scripts
$string = preg_replace_callback(
'#<[/]?script[^>]*>#is', function ($match) {
return '';
}, ' ' . $string
);
//remove all iframe
$string = preg_replace_callback(
'#<[/]?iframe[^>]*>#is', function ($match) {
return '';
}, ' ' . $string
);
// We add some smileys...
$conf = new \system\Conf();
$theme = $conf->getServerConfElement('theme');
$path = BASE_URI . 'themes/' . $theme . '/img/smileys/';
foreach($smileys as $key => $value) {
$replace = ' <img class="smiley" alt="smiley" src="'.$path.$value.'">';
$string = preg_replace('/(^|[ ])('.$key.')/', $replace, $string);
}
return trim($string);
}
/**
* Fix self-closing tags
*/
function fixSelfClosing($string) {
return preg_replace_callback('/<([^\s<]+)\/>/',
function($match) {
return '<'.$match[1].'></'.$match[1].'>';
}
, $string);
}
/**
* Remove the content, body and html tags
*/
function cleanHTMLTags($string) {
return str_replace(
array(
'<content type="html">',
'<html xmlns="http://jabber.org/protocol/xhtml-im">',
'<body xmlns="http://www.w3.org/1999/xhtml">',
'</body>',
'</html>',
'</content>'),
'',
$string);
}
/**
* Return a human-readable week
* @return string
*/
function getDays() {
return array(
1 => t('Monday'),
2 => t('Tuesday'),
3 => t('Wednesday'),
4 => t('Thursday'),
5 => t('Friday'),
6 => t('Saturday'),
7 => t('Friday'));
}
/**
* Return a human-readable year
* @return string
*/
function getMonths() {
return array(
1 => t('January'),
2 => t('February'),
3 => t('March'),
4 => t('April'),
5 => t('May'),
6 => t('June'),
7 => t('July'),
8 => t('August'),
9 => t('September'),
10 => t('October'),
11 => t('November'),
12 => t('December'));
}
/**
* Return a human-readable date
*
* @param timestamp $string
* @return string
*/
function prepareDate($time, $hours = true) {
$dotw = getDays();
$moty = getMonths();
$today = strtotime(date('M j, Y'));
$reldays = ($time - $today)/86400;
if ($reldays >= 0 && $reldays < 1) {
$date = t('Today');
} else if ($reldays >= 1 && $reldays < 2) {
$date = t('Tomorrow');
} else if ($reldays >= -1 && $reldays < 0) {
$date = t('Yesterday');
} else {
if (abs($reldays) < 7) {
if ($reldays > 0) {
$reldays = floor($reldays);
$date = 'In ' . $reldays . ' '.t('day') . ($reldays != 1 ? 's' : '');
} else {
$reldays = abs(floor($reldays));
$date = t(' %d days ago', $reldays);
}
} else {
$date = $dotw[date('N',$time ? $time : time())] .', '.date('j',$time ? $time : time()).' '.$moty[date('n',$time ? $time : time())] ;
if (abs($reldays) > 182)
$date .= date(', Y',$time ? $time : time());
}
}
if($hours)
$date .= ' - '. date('H:i', $time);
if($time)
return $date;
}
/**
* Return the list of gender
*/
@ -271,31 +27,6 @@ function getGender() {
);
}
/**
* Return an array of informations from a XMPP uri
*/
function explodeURI($uri) {
$arr = parse_url(urldecode($uri));
$result = array();
if(isset($arr['query'])) {
$query = explode(';', $arr['query']);
foreach($query as $elt) {
if($elt != '') {
list($key, $val) = explode('=', $elt);
$result[$key] = $val;
}
}
$arr = array_merge($arr, $result);
}
return $arr;
}
/**
* Return a list of all the country
*/
@ -622,103 +353,6 @@ function getMood() {
);
}
/*
* Get the timezone list
*/
function getTimezoneList()
{
return array(
'Etc/GMT+12' => -12.00,
'Etc/GMT+11' => -11.00,
'Etc/GMT+10' => -10.00,
'Etc/GMT+9' => -9.00,
'Etc/GMT+8' => -8.00,
'Etc/GMT+7' => -7.00,
'Etc/GMT+6' => -6.00,
'Etc/GMT+5' => -5.00,
'America/Caracas' => -4.30,
'Etc/GMT+4' => -4.00,
'America/St_Johns' => -3.30,
'Etc/GMT+3' => -3.00,
'Etc/GMT+2' => -2.00,
'Etc/GMT+1' => -1.00,
'Etc/GMT' => 0,
'Etc/GMT-1' => 1.00,
'Etc/GMT-2' => 2.00,
'Etc/GMT-3' => 3.00,
'Asia/Tehran' => 3.30,
'Etc/GMT-4' => 4.00,
'Etc/GMT-5' => 5.00,
'Asia/Kolkata' => 5.30,
'Asia/Katmandu' => 5.45,
'Etc/GMT-6' => 6.00,
'Asia/Rangoon' => 6.30,
'Etc/GMT-7' => 7.00,
'Etc/GMT-8' => 8.00,
'Etc/GMT-9' => 9.00,
'Australia/Darwin' => 9.30,
'Etc/GMT-10' => 10.00,
'Etc/GMT-11' => 11.00,
'Etc/GMT-12' => 12.00,
'Etc/GMT-13' => 13.00);
}
/*
* Get the user local timezone
*/
function getLocalTimezone()
{
date_default_timezone_set('UTC');
$iTime = time();
$arr = localtime($iTime);
$arr[5] += 1900;
$arr[4]++;
$iTztime = gmmktime($arr[2], $arr[1], $arr[0], $arr[4], $arr[3], $arr[5]);
$offset = doubleval(($iTztime-$iTime)/(60*60));
$zonelist = getTimezoneList();
$index = array_keys($zonelist, $offset);
if(sizeof($index)!=1)
return false;
return $index[0];
}
/*
* Echap the JID
*/
function echapJid($jid)
{
return str_replace(' ', '\40', $jid);
}
/**
* Return a URIfied string
* @param string
* @return string
*/
function stringToUri($url) {
$url = utf8_decode($url);
$url = strtolower(strtr($url, utf8_decode('ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ()[]\'"~$&%*@ç!?;,:/\^¨€{}<>|+- '), 'aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn -- c --- e --'));
$url = str_replace(' ', '', $url);
$url = str_replace('---', '-', $url);
$url = str_replace('--', '-', $url);
$url = trim($url,'-');
return $url;
}
/**
* Return a human readable filesize
* @param string size in bytes
* @return string
*/
function sizeToCleanSize($size)
{
$units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$power = $size > 0 ? floor(log($size, 1024)) : 0;
return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
}
/*
* Generate a standard UUID
*/

57
system/UtilsPicture.php

@ -1,34 +1,5 @@
<?php
function createThumbnailPicture($path, $filename) {
if(file_exists($path.'thumb_'.$filename))
unlink($path.'thumb_'.$filename);
if(file_exists($path.'medium_'.$filename))
unlink($path.'medium_'.$filename);
$handle = fopen($path.$filename, "r");
$file = fread($handle, filesize($path.$filename));
fclose($handle);
createThumbnailSize($file, 200, $path.'thumb_'.$filename);
createThumbnailSize($file, 600, $path.'medium_'.$filename);
}
function createThumbnails($jid, $photobin) {
if(file_exists(DOCUMENT_ROOT.'/cache/'.$jid.'_l.jpg'))
unlink(DOCUMENT_ROOT.'/cache/'.$jid.'_l.jpg');
if(file_exists(DOCUMENT_ROOT.'/cache/'.$jid.'_m.jpg'))
unlink(DOCUMENT_ROOT.'/cache/'.$jid.'_m.jpg');
if(file_exists(DOCUMENT_ROOT.'/cache/'.$jid.'_s.jpg'))
unlink(DOCUMENT_ROOT.'/cache/'.$jid.'_s.jpg');
if(file_exists(DOCUMENT_ROOT.'/cache/'.$jid.'_xs.jpg'))
unlink(DOCUMENT_ROOT.'/cache/'.$jid.'_xs.jpg');
createThumbnailSize(base64_decode($photobin), 200, DOCUMENT_ROOT.'/cache/'.$jid.'_l.jpg');
createThumbnailSize(base64_decode($photobin), 120, DOCUMENT_ROOT.'/cache/'.$jid.'_m.jpg');
createThumbnailSize(base64_decode($photobin), 50, DOCUMENT_ROOT.'/cache/'.$jid.'_s.jpg');
createThumbnailSize(base64_decode($photobin), 28, DOCUMENT_ROOT.'/cache/'.$jid.'_xs.jpg');
createThumbnailSize(base64_decode($photobin), 24, DOCUMENT_ROOT.'/cache/'.$jid.'_xxs.jpg');
}
function createEmailPic($jid, $email) {
if(file_exists(DOCUMENT_ROOT.'/cache/'.$jid.'_email.jpg'))
@ -43,31 +14,3 @@ function createEmailPic($jid, $email) {
imagejpeg($thumb, DOCUMENT_ROOT.'/cache/'.$jid.'_email.jpg', 95);
}
function createThumbnailSize($photobin, $size, $path) {
$thumb = imagecreatetruecolor($size, $size);
$white = imagecolorallocate($thumb, 255, 255, 255);
imagefill($thumb, 0, 0, $white);
$source = imagecreatefromstring($photobin);
$width = imagesx($source);
$height = imagesy($source);
if($width >= $height) {
// For landscape images
$x_offset = ($width - $height) / 2;
$y_offset = 0;
$square_size = $width - ($x_offset * 2);
} else {
// For portrait and square images
$x_offset = 0;
$y_offset = ($height - $width) / 2;
$square_size = $height - ($y_offset * 2);
}
if($source) {
imagecopyresampled($thumb, $source, 0, 0, $x_offset, $y_offset, $size, $size, $square_size, $square_size);
imagejpeg($thumb, $path, 95);
}
}

19
upload.php

@ -5,28 +5,21 @@ require_once(DOCUMENT_ROOT.'/bootstrap.php');
$bootstrap = new Bootstrap();
$booted = $bootstrap->boot();
function bytesToSize1024($bytes, $precision = 2) {
$unit = array('B','KB','MB');
return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), $precision).' '.$unit[$i];
}
$sFileName = $_FILES['image_file']['name'];
$sFileType = $_FILES['image_file']['type'];
$sFileSize = bytesToSize1024($_FILES['image_file']['size'], 1);
$error = $_FILES['image_file']['error'];
$user = new User();
define('USER_PATH', DOCUMENT_ROOT . '/users/'.$user->getLogin().'/');
$error = $_FILES['image_file']['error'];
if ($error == UPLOAD_ERR_OK && $user->dirSize() < $user->sizelimit) {
$tmp_name = $_FILES["image_file"]["tmp_name"];
if(getimagesize($tmp_name) != 0) {
$name = stringToUri($_FILES["image_file"]["name"]);
move_uploaded_file($tmp_name, USER_PATH.$name);
createThumbnailPicture(USER_PATH, $name);
move_uploaded_file($tmp_name, $user->userdir.$name);
$p = new \Picture;
$p->fromPath($user->userdir.$name);
$p->set($user->userdir.$name);
} else {
unlink($tmp_name);
echo '<div class="message error">'.t('Not a picture').'</div>';

Loading…
Cancel
Save