Files
ugc/inc/functions.php

126 lines
3.8 KiB
PHP
Executable File

<?php
require_once 'settings.php';
function getPage($sPath, $asVars, $sType)
{
$bSavePage = Settings::DEBUG;
if($bSavePage)
{
$asFileIds = array_filter($asVars);
unset($asFileIds['_']);
$sFileName = $sPath.'.'.implode('.', $asFileIds).'.log';
}
if(!$bSavePage || !file_exists($sFileName) || date('dmY', filemtime($sFileName))!=date('dmY'))
{
$sUrl = 'http://www.ugc.fr/'.$sPath.'?'.http_build_query($asVars);
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HEADER, false);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($oCurl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$sContent = curl_exec($oCurl);
curl_close($oCurl);
//Detect failed http request
if(strpos(str_replace(array("\r", "\n"), '', $sContent), '<title>Service Temporarily Unavailable</title>')) return getPage($sPath, $asVars, $sType);
if($bSavePage) file_put_contents($sFileName, $sContent);
}
else $sContent = file_get_contents($sFileName);
//Remove scripting & image source (avoid http request)
if($sType=='html')
{
$sContent = preg_replace('/<script(.*)<\/script>/sU', '', $sContent);
$sContent = preg_replace('/src=("|\')(.*)("|\')/sU', 'source="$2"', $sContent);
}
return $sContent;
}
function setPageMime($sType='html')
{
$asMainTypes = array('json'=>'application', 'jpeg'=>'image', 'png'=>'image', 'gif'=>'image');
if($sType!='html' && array_key_exists($sType, $asMainTypes)) header('Content-type: '.$asMainTypes[$sType].'/'.$sType);
}
function getTrimImage($iFilmId, $sSize, $sinPath, $sImageExt)
{
$sOutPath = 'images/'.$iFilmId.'_'.$sSize.'.'.$sImageExt;
if(!file_exists($sOutPath))
{
$oImage = call_user_func('imagecreatefrom'.$sImageExt, $sinPath);
$iImageX = imagesx($oImage);
$iImageY = imagesy($oImage);
$iTop = 0;
$iBottom = 0;
$iLeft = 0;
$iRight = 0;
$iWhiteLimit = 210;
//Top height to crop:
for(; $iTop < $iImageY; ++$iTop)
{
for($x = 0; $x < $iImageX; ++$x)
{
$hColor = imagecolorat($oImage, $x, $iTop);
$iRed = ($hColor >> 16) & 0xFF;
$iGreen = ($hColor >> 8) & 0xFF;
$iBlue = $hColor & 0xFF;
if($iRed<$iWhiteLimit || $iGreen<$iWhiteLimit || $iBlue<$iWhiteLimit) break 2; //out of the 'top' loop
}
}
//Bottom height to crop
for(; $iBottom < $iImageY; ++$iBottom)
{
for($x = 0; $x < $iImageX; ++$x)
{
$hColor = imagecolorat($oImage, $x, $iImageY - $iBottom - 1);
$iRed = ($hColor >> 16) & 0xFF;
$iGreen = ($hColor >> 8) & 0xFF;
$iBlue = $hColor & 0xFF;
if($iRed<$iWhiteLimit || $iGreen<$iWhiteLimit || $iBlue<$iWhiteLimit) break 2; //out of the 'bottom' loop
}
}
//Left width to crop
for(; $iLeft < $iImageX; ++$iLeft)
{
for($y = 0; $y < $iImageY; ++$y)
{
$hColor = imagecolorat($oImage, $iLeft, $y);
$iRed = ($hColor >> 16) & 0xFF;
$iGreen = ($hColor >> 8) & 0xFF;
$iBlue = $hColor & 0xFF;
if($iRed<$iWhiteLimit || $iGreen<$iWhiteLimit || $iBlue<$iWhiteLimit) break 2; //out of the 'left' loop
}
}
//Right width to crop
for(; $iRight < $iImageX; ++$iRight)
{
for($y = 0; $y < $iImageY; ++$y)
{
$hColor = imagecolorat($oImage, $iImageX - $iRight-1, $y);
$iRed = ($hColor >> 16) & 0xFF;
$iGreen = ($hColor >> 8) & 0xFF;
$iBlue = $hColor & 0xFF;
if($iRed<$iWhiteLimit || $iGreen<$iWhiteLimit || $iBlue<$iWhiteLimit) break 2; //out of the 'right' loop
}
}
//Creating a image with the new height
$oCropImage = imagecreatetruecolor($iImageX-($iLeft+$iRight), $iImageY-($iTop+$iBottom));
//Copy the content, excluding the border
imagecopy($oCropImage, $oImage, 0, 0, $iLeft, $iTop, imagesx($oCropImage), imagesy($oCropImage));
call_user_func_array('image'.$sImageExt, array($oCropImage, $sOutPath));
}
return file_get_contents($sOutPath);
}
?>