[add] now can generate thumbshot of entire page

This commit is contained in:
Knah Tsaeb 2015-07-09 17:21:32 +02:00
parent d742fe8b3e
commit 9ae1fbbb06
4 changed files with 131 additions and 83 deletions

View file

@ -1,5 +1,5 @@
var page = new WebPage(),
address, output, size;
var page = require('webpage').create(),
address, output;
if (phantom.args.length < 2) {
//console.log('Usage: rasterize.js URL filename');

View file

@ -7,7 +7,7 @@ site=$1 # url for thumbshot
hashUrl=$2 # hash(url) is the name of final image
thumbSize=$3 # size of thumb widthxheight ex : 190x90
sizeNameDir=$4 # name of size dir
onlyThumb=$5 # make only thumbshot no full size image
renderType=$5 # make only thumbshot / thumbshot + realSize / thumbshot + complete / thumbshot + complete + realSize
waitForResult=$6 # if true we try to make soon as possible or add to queue
##########################################
@ -26,7 +26,7 @@ firstLevel=${hashUrl:0:2}
secondLevel=${hashUrl:2:2}
startPath=$sizeNameDir/$firstLevel/$secondLevel/
getArch=$(getconf LONG_BIT)
echo $getArch
#echo $getArch
if [ $site == "manual" ]
then
site=$(cat "../cache/tmp/manual.txt")
@ -62,7 +62,6 @@ else
timeout $timeOut ./phantomjs_x86 --disk-cache=false --local-storage-path=bin/ --ignore-ssl-errors=true rasterize.js "$site" "../cache/tmp/$hashUrl.png"
fi
if [ ! -f "../cache/tmp/$hashUrl.png" ]
then
echo 0
@ -90,39 +89,73 @@ then
fi
exit
fi
if [ $onlyThumb == 1 ]
then
case $renderType in
# thumbnail only
t)
convert "../cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 -filter Lanczos -thumbnail "$thumbSize" "../cache/img/$startPath""$hashUrl""_thumb.png"
if [ $optimizeTool ]
then
eval $optimizeTool "../cache/img/$startPath""$hashUrl""_thumb.png"
fi
else
convert "../cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 "../cache/img/$startPath""$hashUrl.png"
convert "../cache/img/$startPath""$hashUrl.png" -filter Lanczos -thumbnail "$thumbSize" "../cache/img/$startPath""$hashUrl""_thumb.png"
if [ $optimizeTool ]
then
eval $optimizeTool "../cache/img/$startPath""$hashUrl""_thumb.png" "../cache/img/$startPath""$hashUrl.png"
fi
fi
rm "../cache/tmp/$hashUrl.png"
if [ $onlyThumb ]
then
if [[ -f "../cache/img/$startPath""$hashUrl""_thumb.png" ]]
then
echo 1
else
echo 0
fi
;;
# thumbnail + complete
tc)
convert "../cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 -filter Lanczos -thumbnail "$thumbSize" "../cache/img/$startPath""$hashUrl""_thumb.png"
if [ $optimizeTool ]
then
eval $optimizeTool "../cache/img/$startPath""$hashUrl""_thumb.png" "../cache/tmp/$hashUrl.png"
fi
mv "../cache/tmp/$hashUrl.png" "../cache/img/$startPath""$hashUrl""_complete.png"
if [[ -f "../cache/img/$startPath""$hashUrl""_thumb.png" && -f "../cache/img/$startPath""$hashUrl""_complete.png" ]]
then
echo 1
else
echo 0
fi
;;
# thumbnail + realsize
tr)
convert "../cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 "../cache/img/$startPath""$hashUrl.png"
convert "../cache/img/$startPath""$hashUrl.png" -filter Lanczos -thumbnail "$thumbSize" "../cache/img/$startPath""$hashUrl""_thumb.png"
if [ $optimizeTool ]
then
eval $optimizeTool "../cache/img/$startPath""$hashUrl""_thumb.png" "../cache/img/$startPath""$hashUrl.png"
fi
rm "../cache/tmp/$hashUrl.png"
if [[ -f "../cache/img/$startPath""$hashUrl""_thumb.png" && -f "../cache/img/$startPath""$hashUrl.png" ]]
then
echo 1
else
echo 0
fi
;;
# thumbnail + realsize + complete
trc)
convert "../cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 "../cache/img/$startPath""$hashUrl.png"
convert "../cache/img/$startPath""$hashUrl.png" -filter Lanczos -thumbnail "$thumbSize" "../cache/img/$startPath""$hashUrl""_thumb.png"
if [ $optimizeTool ]
then
eval $optimizeTool "../cache/img/$startPath""$hashUrl""_thumb.png" "../cache/img/$startPath""$hashUrl.png" "../cache/tmp/$hashUrl.png"
fi
mv "../cache/tmp/$hashUrl.png" "../cache/img/$startPath""$hashUrl""_complete.png"
if [[ -f "../cache/img/$startPath""$hashUrl""_thumb.png" && -f "../cache/img/$startPath""$hashUrl.png" && -f "../cache/img/$startPath""$hashUrl""_complete.png" ]]
then
echo 1
else
echo 0
fi
;;
esac
if $log
then
end_time=`date +%s`

View file

@ -34,8 +34,11 @@ if (!empty($success)) {
echo '<h3>This image will be removed in ', $GLOBALS['config']['expireCache'], 'h</h3>';
echo '<img src="data:image/png;base64,',base64_encode(file_get_contents($success['thumb'])), '"/>';
echo '<p><label>Thumbshot</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['thumb'], '" onclick="this.select()"/>';
if ($GLOBALS['config']['onlyThumb'] === false) {
echo '<p><label>1280x1024</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['thumb'], '" onclick="this.select()"/></p>';
if ($GLOBALS['config']['activeFullSize'] === true) {
echo '<p><label>1280x1024</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['normal'], '" onclick="this.select()"/></p>';
}
if ($GLOBALS['config']['activeComplete'] === true) {
echo '<p><label>Complete</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['complete'], '" onclick="this.select()"/></p>';
}
echo '</div>';
}

114
index.php
View file

@ -1,6 +1,5 @@
<?php
session_start();
if (empty($_SESSION['login'])) {
$_SESSION['login'] = false;
}
@ -18,8 +17,6 @@ $GLOBALS['config']['banTime'] = 60;
$GLOBALS['config']['defaultUrl'] = 'https://duckduckgo.com/';
// default size for thumbnail
$GLOBALS['config']['defaultThumbSize'] = '120x90';
// generate only thumbnail or generate thumbnail + 1280x1024 image
$GLOBALS['config']['onlyThumb'] = false;
// list of available size for thumb
$GLOBALS['config']['thumbSize'] = array(
's' => '120x90',
@ -28,17 +25,19 @@ $GLOBALS['config']['thumbSize'] = array(
'xl' => '400x320',
'xxl' => '500x400'
);
// create thumshot 1280x1024
$GLOBALS['config']['activeFullSize'] = false;
// create thumbshot of complete page
$GLOBALS['config']['activeComplete'] = false;
// Remove image older than 12 hours
$GLOBALS['config']['expireCache'] = 12;
// Disable exec command and use cron task
$GLOBALS['config']['disableExec'] = true;
// Enable log for success, suspect, error
$GLOBALS['config']['log'] = false;
$GLOBALS['config']['log'] = true;
if (file_exists('cache/config/options.php')) {
require 'cache/config/options.php';
}
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value) {
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
@ -49,7 +48,6 @@ if (get_magic_quotes_gpc()) {
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}
if (!empty($_POST)) {
$ui = $_POST;
$ui['request'] = 'form';
@ -61,60 +59,65 @@ if (!empty($_GET)) {
$ui['request'] = 'api';
unset($_GET);
}
if (empty($ui['request'])) {
$ui['request'] = '';
}
if (empty($ui['p'])) {
$ui['p'] = 'index';
}
if (empty($ui['fr'])) {
$ui['fr'] = '';
}
if (empty($ui['t'])) {
$ui['t'] = 't';
}
if (isset($ui['logout']) && (int)$ui['logout'] === 1) {
session_destroy();
header("Location:?");
exit();
}
if (!file_exists('cache/config/genConf.php')) {
$serverKey = install();
$ui['p'] = 'install';
}
require 'cache/config/genConf.php';
if ($GLOBALS['config']['pwd'] === 'install' && $ui['p'] !== 'install') {
reloadInstall();
header("Location:?");
}
if (($GLOBALS['config']['onlyLocalServer'] === true && $_SERVER['REMOTE_ADDR'] !== '127.0.0.1') || checkIfBan() === true) {
header("HTTP/1.0 404 Not Found");
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
exit();
}
if ($GLOBALS['config']['NoWebPage'] === true && empty($_GET)) {
header("HTTP/1.0 404 Not Found");
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
exit();
}
function testExistImg($file) {
if ($GLOBALS['config']['onlyThumb'] === true) {
if (file_exists($file.'_thumb.png')) {
if ($GLOBALS['config']['activeFullSize'] && $GLOBALS['config']['activeComplete']) {
if (file_exists($file.'_thumb.png') && file_exists($file.'.png') && file_exists($file.'_complete.png')) {
return true;
} else {
return false;
}
} elseif ($GLOBALS['config']['activeFullSize']) {
if (file_exists($file.'_thumb.png') && file_exists($file.'.png')) {
return true;
} else {
return false;
}
} elseif ($GLOBALS['config']['activeComplete']) {
if (file_exists($file.'_thumb.png') && file_exists($file.'_complete.png')) {
return true;
} else {
return false;
}
} else {
if (file_exists($file.'_thumb.png') && file_exists($file.'.png')) {
if (file_exists($file.'_thumb.png')) {
return true;
} else {
return false;
@ -134,37 +137,40 @@ function testExistImg($file) {
* @param $waitForResult (bool)
* @return
*/
function launchScript($url, $hashUrl, $width, $size, $onlyThumb, $waitForResult = false) {
function launchScript($url, $hashUrl, $width, $size, $waitForResult = false) {
$hashUrl = escapeshellarg($hashUrl);
$url = escapeshellarg($url);
$width = escapeshellarg($width);
if ($GLOBALS['config']['onlyThumb'] === true) {
$onlyThumb = 1;
if ($GLOBALS['config']['activeFullSize'] && $GLOBALS['config']['activeComplete']) {
$renderType = 'trc';
} elseif ($GLOBALS['config']['activeFullSize']) {
$renderType = 'tr';
} elseif ($GLOBALS['config']['activeComplete']) {
$renderType = 'tc';
} else {
$onlyThumb = 0;
$renderType = 't';
}
if ($GLOBALS['config']['disableExec'] === false) {
chdir('bin/');
if ($waitForResult === false) {
exec('bash thumb_server.sh '.$url.' '.$hashUrl.' '.$width.' '.$size.' '.$onlyThumb.' > /dev/null &', $result);
exec('bash thumb_server.sh '.$url.' '.$hashUrl.' '.$width.' '.$size.' '.$renderType.' > /dev/null &', $result);
} else {
exec('bash thumb_server.sh '.$url.' '.$hashUrl.' '.$width.' '.$size.' '.$onlyThumb.' 1', $result);
exec('bash thumb_server.sh '.$url.' '.$hashUrl.' '.$width.' '.$size.' '.$renderType.' 1', $result);
}
chdir('../');
} else {
makeQueueFile($url, $hashUrl, $width, $size, $onlyThumb);
makeQueueFile($url, $hashUrl, $width, $size, $renderType);
$result = 0;
}
return $result;
}
function makeQueueFile($url, $hashUrl, $width, $size, $onlyThumb) {
function makeQueueFile($url, $hashUrl, $width, $size, $renderType) {
$url = str_replace('\'', '', $url);
$hashUrl = str_replace('\'', '', $hashUrl);
$width = str_replace('\'', '', $width);
if (!file_exists('cache/cronTask/'.$hashUrl.'.hash')) {
$data = $url.' '.$hashUrl.' '.$width.' '.$size.' '.$onlyThumb. "\n";
$data = $url.' '.$hashUrl.' '.$width.' '.$size.' '.$renderType."\n";
file_put_contents('cache/cronTask/'.$hashUrl.'.hash', $data);
}
}
@ -263,7 +269,6 @@ function install() {
$salt = sha1(uniqid(rand(), true).'_'.mt_rand());
$serverKey = substr(sha1(uniqid(rand(), true).'_'.mt_rand().$salt), 0, 12);
$encryptServerKey = sha1($serverKey.$salt);
if (!is_file('cache/config/genConf.php')) {
file_put_contents('cache/config/genConf.php', "<?php\n\$GLOBALS['config']['serverKey'] = '$encryptServerKey';\n\$GLOBALS['config']['salt'] = '$salt';\n\$GLOBALS['config']['pwd'] = 'install';\n?>");
}
@ -395,11 +400,21 @@ function n_print($data, $name = '') {
';
}
function printThumbShot($file) {
function printThumbShot($file, $renderType) {
ob_end_clean();
header("Content-type: image/png");
header('Expires: ', gmdate('D, d M Y H:i:s', time()).' GMT');
switch ($renderType) {
case 'c' :
echo file_get_contents($file.'_complete.png');
break;
case 'r' :
echo file_get_contents($file.'.png');
break;
default :
echo file_get_contents($file.'_thumb.png');
break;
}
exit();
}
@ -430,11 +445,10 @@ function testIfImg($url) {
}
}
function makeImgThumb($url, $ext, $hashUrl, $width, $path, $onlyThumb) {
function makeImgThumb($url, $ext, $hashUrl, $width, $path, $renderType) {
if (!function_exists('imagecreatefromjpeg')) {
return false;
}
$image = file_get_contents($url);
$fullSize = file_put_contents('cache/tmp/'.$hashUrl.'.'.$ext, $image);
if ($ext === 'jpg' || $ext === 'jpeg') {
@ -450,7 +464,6 @@ function makeImgThumb($url, $ext, $hashUrl, $width, $path, $onlyThumb) {
unlink('cache/tmp/'.$hashUrl.'.'.$ext);
return false;
}
$w = imagesx($tmpImg);
$h = imagesy($tmpImg);
$ystart = 0;
@ -485,17 +498,14 @@ function makeImgThumb($url, $ext, $hashUrl, $width, $path, $onlyThumb) {
*/
checkInstall();
removeOlderThan();
if (empty($defUrl)) {
$defUrl = $GLOBALS['config']['defaultUrl'];
}
if (isset($ui['s']) && array_key_exists($ui['s'], $GLOBALS['config']['thumbSize'])) {
$width = $GLOBALS['config']['thumbSize'][$ui['s']];
} else {
$width = $GLOBALS['config']['defaultThumbSize'];
}
// Generate or return img
if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' && $ui['p'] !== 'admin') {
if (empty($ui['url'])) {
@ -512,15 +522,15 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
$file = 'cache/img/shortLive/'.$startPath.$hashUrl;
$file = 'cache/img/shortLive/'.$startPath.$hashUrl;
}
if (testExistImg($file) === true && (int)$ui['fr'] !== 1) {
if ($ui['request'] === 'api') {
printThumbShot($file);
printThumbShot($file, $ui['t']);
}
if ($ui['request'] === 'form') {
$success = array(
'normal' => $file.'.png',
'thumb' => $file.'_thumb.png'
'thumb' => $file.'_thumb.png',
'complete' => $file.'_complete.png'
);
}
} else {
@ -537,14 +547,15 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
}
if ($testUrl !== true) {
if (!file_exists('cache/logs/'.$hashUrl.'log')) {
file_put_contents('cache/logs/suspect/'.$hashUrl.'.log', $_SERVER['REMOTE_ADDR'].' --- '.$ui['url'].' --- '.$hashUrl.' --- '.$width.' --- '.$ui['s'].' --- '.$GLOBALS['config']['onlyThumb'].' --- true'."\n");
file_put_contents('cache/logs/suspect/'.$hashUrl.'.log', $_SERVER['REMOTE_ADDR'].' --- '.$ui['url'].' --- '.$hashUrl.' --- '.$width.' --- '.$ui['s'].' --- '.$renderType.' --- true'."\n");
}
if ($ui['request'] === 'api') {
printThumbShot('bin/error');
} else {
$success = array(
'normal' => 'bin/error.png',
'thumb' => 'bin/error_thumb.png'
'thumb' => 'bin/error_thumb.png',
'complete' => 'bin/error.png'
);
}
} else {
@ -552,11 +563,10 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
if ($ext !== false) {
$genWidth = explode("x", $width);
if ($ui['request'] === 'api') {
$makeImg = makeImgThumb($ui['url'], $ext, $hashUrl, $genWidth[0], 'cache/img/'.$ui['s'].'/'.$startPath, $GLOBALS['config']['onlyThumb']);
$makeImg = makeImgThumb($ui['url'], $ext, $hashUrl, $genWidth[0], 'cache/img/'.$ui['s'].'/'.$startPath, $ui['t']);
} else {
$makeImg = makeImgThumb($ui['url'], $ext, $hashUrl, $genWidth[0], 'cache/img/shortLive/'.$startPath, $GLOBALS['config']['onlyThumb']);
$makeImg = makeImgThumb($ui['url'], $ext, $hashUrl, $genWidth[0], 'cache/img/shortLive/'.$startPath, $ui['t']);
}
if ($makeImg === true) {
$GLOBALS['config']['disableExec'] = true;
if ($ui['request'] === 'api') {
@ -564,17 +574,18 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
} else {
$success = array(
'normal' => $file.'.png',
'thumb' => $file.'_thumb.png'
'thumb' => $file.'_thumb.png',
'complete' => $file.'_complete.png'
);
}
}
}
if ((isset($ui['iw']) && (int)$ui['iw'] === 1 && $ui['request'] === 'api')) {
$res = launchScript($defUrl, $hashUrl, $width, $ui['s'], $GLOBALS['config']['onlyThumb'], true);
$res = launchScript($defUrl, $hashUrl, $width, $ui['s'], true);
} elseif (isset($ui['iw']) && (int)$ui['iw'] === 1 && $ui['request'] === 'form') {
$res = launchScript($defUrl, $hashUrl, $width, 'shortLive', $GLOBALS['config']['onlyThumb'], true);
$res = launchScript($defUrl, $hashUrl, $width, 'shortLive', true);
} else {
$res = launchScript($defUrl, $hashUrl, $width, $ui['s'], $GLOBALS['config']['onlyThumb'], false);
$res = launchScript($defUrl, $hashUrl, $width, $ui['s'], false);
}
if ($ui['request'] === 'api') {
$file = 'bin/loadingGen';
@ -585,7 +596,8 @@ if (!empty($ui['request']) && $ui['p'] !== 'install' && $ui['p'] !== 'login' &&
}
$success = array(
'normal' => $file.'.png',
'thumb' => $file.'_thumb.png'
'thumb' => $file.'_thumb.png',
'complete' => $file.'_complete.png'
);
}
}