Skip to content

Commit

Permalink
Quick cleanup and inject File class into Optimization::dataUri() method
Browse files Browse the repository at this point in the history
  • Loading branch information
pH-7 committed Jun 15, 2017
1 parent 3a2e4de commit 35850d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
20 changes: 12 additions & 8 deletions _protected/framework/Layout/Optimization.class.php
Expand Up @@ -10,24 +10,26 @@
*/

namespace PH7\Framework\Layout;

defined('PH7') or exit('Restricted access');

use PH7\Framework\File\File;

class Optimization
{

/**
* Data URI scheme - base64 encoding.
*
* @param string $sFile
* @param File $oFile
*
* @return string Returns format: data:[<MIME-type>][;base64],<data>
*/
public static function dataUri($sFile)
public static function dataUri($sFile, File $oFile)
{
$oFile = new \PH7\Framework\File\File();
// Switch to right MIME-type
$sExt = $oFile->getFileExt($sFile);
$sMimeType = $oFile->getMimeType($sExt);
unset($oFile);

$sBase64 = base64_encode(file_get_contents($sFile));
return "data:$sMimeType;base64,$sBase64";
Expand All @@ -40,6 +42,7 @@ public static function dataUri($sFile)
*
* @param string $sFile Contents to scan.
* @param string $sDir Folder name to prepend.
*
* @return string Content with adjusted paths.
*/
public static function cssDataUriCleanup($sFile, $sDir)
Expand All @@ -49,8 +52,7 @@ public static function cssDataUriCleanup($sFile, $sDir)

preg_match_all($sRegexUrl, $sFile, $aHit, PREG_PATTERN_ORDER);

for ($i=0, $iCountHit = count($aHit[0]); $i < $iCountHit; $i++)
{
for ($i=0, $iCountHit = count($aHit[0]); $i < $iCountHit; $i++) {
$sSearch = $aHit[1][$i] . $aHit[2][$i] . $aHit[3][$i];

$sReplace = $sDir . $aHit[1][$i];
Expand All @@ -63,9 +65,11 @@ public static function cssDataUriCleanup($sFile, $sDir)
substr(str_replace(array('"', "'"), '', $aHit[2][$i]),0,6) != 'mhtml:' &&
substr(str_replace(array('"', "'"), '', $aHit[2][$i]),0,1) != '/' &&
substr(str_replace(array('"', "'"), '', $aHit[2][$i]),strlen(str_replace(array('"',"'"),'', $aHit[2][$i])) - 4,4) != '.htc'
) $sFile = str_replace($sSearch, $sReplace, $sFile);
) {
$sFile = str_replace($sSearch, $sReplace, $sFile);
}
}

return $sFile;
}

}
11 changes: 7 additions & 4 deletions _protected/framework/Parse/Emoticon.class.php
Expand Up @@ -11,18 +11,21 @@
*/

namespace PH7\Framework\Parse;

defined('PH7') or exit('Restricted access');

use PH7\Framework\Layout\Optimization;
use PH7\Framework\File\File;
use PH7\Framework\Service\Emoticon as EmoticonService;

class Emoticon extends \PH7\Framework\Service\Emoticon
class Emoticon extends EmoticonService
{
/**
* Parse the contents.
*
* @static
* @param string $sContents
* @param boolean $bIsDataUri Default: TRUE
* @param boolean $bIsDataUri
*
* @return string Contents
*/
public static function init($sContents, $bIsDataUri = true)
Expand All @@ -31,7 +34,7 @@ public static function init($sContents, $bIsDataUri = true)

foreach ($aEmoticons as $sEmoticonKey => $aEmoticon) {
if ($bIsDataUri) {
$sSrcImg = Optimization::dataUri(static::getPath($sEmoticonKey));
$sSrcImg = Optimization::dataUri(static::getPath($sEmoticonKey), new File);
} else {
$sSrcImg = static::getUrl($sEmoticonKey);
}
Expand Down

0 comments on commit 35850d3

Please sign in to comment.