Пример #1
0
 public function CreateSprite(CssSprite $sprite)
 {
     if ($sprite->exists()) {
         return;
         //--- Спрайт существует
     }
     $this->LOGGER->info("Waiting to make {$sprite}");
     PsLock::lockMethod(__CLASS__, __FUNCTION__);
     try {
         if ($sprite->exists()) {
             $this->LOGGER->info('Sprite was created in another thread, skipping');
         } else {
             $this->PROFILER->start('Sprite creation');
             $this->CssSpriteGen->CreateSprite($sprite);
             $this->LOGGER->info('Sprite was successfully created, path: ' . $sprite->getCssDi()->getAbsPath());
             $this->PROFILER->stop();
         }
     } catch (Exception $ex) {
         PsLock::unlock();
         throw $ex;
     }
     PsLock::unlock();
 }
Пример #2
0
$tmpImgDir = realpath(dirname(__FILE__) . '/../cache/sprites');
$WRITE_FILES = 1;
// use 0 for testing only
//echo " $leoBase $cssFile $imgDir";
$leoSprites = array('fl' => array('selector-prefix' => 'img.fl', 'background' => '#00FF00', 'horizontal-offset' => 20, 'vertical-offset' => 20, 'use-transparency' => 'on', 'image-output' => 'PNG', 'image-num-colours' => '256', 'add-width-height-to-css' => '', 'filename' => 'flags'), 'icons1' => array('selector-prefix' => 'img.icons1', 'background' => '#FF7DF1', 'horizontal-offset' => 40, 'vertical-offset' => 25, 'file-regex' => '(.*).png', 'use-transparency' => 'on', 'image-output' => 'PNG', 'image-num-colours' => '256'), 'brands' => array('selector-prefix' => 'img.brands', 'background' => '#FF7DF1', 'horizontal-offset' => 85, 'vertical-offset' => 25, 'use-transparency' => 'on', 'image-output' => 'PNG', 'image-num-colours' => '256'));
$extraCss = array('fl' => '
width:18px;
height:11px;	
margin-top:1px;
margin-left:2px;');
$cssStr = '';
foreach ($leoSprites as $dirName => $dirOptions) {
    // create an intance of the sprite gen class
    // (this does all the work of creating sprites and CSS)
    $_POST = array('MAX_FILE_SIZE' => 524288, 'zip-folder' => '6d64c93a3f812f546092f64a911176de', 'zip-folder-hash' => '90520b09bec4b53e58f19b97ec4b11d5', 'ignore-duplicates' => 'ignore', 'width-resize' => 100, 'height-resize' => 100, 'build-direction' => 'vertical', 'horizontal-offset' => 50, 'vertical-offset' => 50, 'wrap-columns' => 'on', 'background' => '#FF7DF1', 'use-transparency' => 'on', 'image-output' => 'PNG', 'image-num-colours' => 'true-colour', 'image-quality' => '75', 'use-optipng' => 'on', 'selector-prefix' => 'img.fl', 'class-prefix' => 'sprite-', 'selector-suffix' => '', 'add-width-height-to-css' => 'on');
    $oCssSpriteGen = new CssSpriteGen();
    foreach ($dirOptions as $name => $val) {
        $_POST[$name] = $val;
    }
    $oCssSpriteGen->ProcessForm();
    $sFolderMD5 = $oCssSpriteGen->ProcessFile();
    // look into leonardo img folders instead
    $sFolderMD5 = "{$imgDir}/{$dirName}/";
    $oCssSpriteGen->CreateSprite($sFolderMD5);
    $tmpImgFile = $tmpImgDir . '/' . $oCssSpriteGen->GetSpriteFilename();
    if ($dirOptions['filename']) {
        $realImgFile = "{$imgDir}/sprite_" . $dirOptions['filename'] . ".png";
    } else {
        $realImgFile = "{$imgDir}/sprite_{$dirName}.png";
    }
    $cssStrTmp = $oCssSpriteGen->GetCss();
 /**
  * Grabs background images with no-repeat attribute from css and merge them in one file called a sprite.
  * Css is updated with sprite url and correct background positions for affected images.
  * Sprite saved in {Joomla! base}/images/jch-optimize/
  *
  * @param string $sCss       Aggregated css file before sprite generation
  * @param string $sLnEnd     Document line end
  * @return string           Css updated with sprite information on success. Original css on failure
  */
 protected function generateSprite($sCss, $sLnEnd)
 {
     if (extension_loaded('imagick') && extension_loaded('exif')) {
         $sImageLibrary = 'imagick';
     } else {
         if (!extension_loaded('gd') || !extension_loaded('exif')) {
             return $sCss;
         }
         $sImageLibrary = 'gd';
     }
     $iMinMaxImages = $this->params->get('csg_min_max_images', 0);
     $sDelStart = '~';
     $sRegexStart = '(?:(?<=^|})
                         (?=([^{]+?)({[^}]+?(url\\(([^}]+?\\.(?:png|gif|jpe?g))[^}]*?\\))[^}]+?}))
                         (?:(?!(?:\\s(?<!no-)repeat(?:-(?:x|y))?)|(?:background[^;]+?(?:\\s(?:left|right|center|top|bottom)';
     $sRegexMin = '|(?:\\s0)|(?:\\s\\d{1,5}(?:%|in|(?:c|m)m|e(?:m|x)|p(?:t|c|x)))){1,2}[^;]*?;)';
     $sRegexMax = '|(?:\\s[1-9]\\d{0,4}(?:%|in|(?:c|m)m|e(?:m|x)|p(?:t|c|x)))){1,2}[^;]*?;)';
     $sRegexEnd = ')[^}])*?})';
     $sDelEnd = '~sx';
     $aIncludeImages = $this->getArray($this->params->get('csg_include_images'));
     $aExcludeImages = $this->getArray($this->params->get('csg_exclude_images'));
     $sIncImagesRegex = '';
     if (!empty($aIncludeImages[0]) && !$iMinMaxImages) {
         foreach ($aIncludeImages as $sIncImage) {
             $sIncImage = str_replace('.', '\\.', $sIncImage);
             $sIncImagesRegex .= '|(?:(?<=^|})([^{]+?){[^}]+?(url\\(([^}]+?' . $sIncImage . ')[^}]*?\\))[^}]*?})';
         }
     }
     $sExImagesRegex = '';
     if (!empty($aExcludeImages[0]) && $iMinMaxImages) {
         foreach ($aExcludeImages as $sExImage) {
             $sExImage = str_replace('.', '\\.', $sExImage);
             $sExImagesRegex .= '|(?:\\b' . $sExImage . ')';
         }
     }
     $sMinMaxRegex = $iMinMaxImages ? $sRegexMax : $sRegexMin;
     $sRegex = $sDelStart . $sRegexStart . $sMinMaxRegex . $sExImagesRegex . $sRegexEnd . $sIncImagesRegex . $sDelEnd;
     $iResult = preg_match_all($sRegex, $sCss, $aMatches);
     //print_r($aMatches);
     if ($iResult <= 0) {
         return $sCss;
     }
     require_once dirname(__FILE__) . DS . 'cache' . DS . 'css-sprite-gen.inc.php';
     $aDeclaration = $aMatches[2];
     $aImages = $aMatches[4];
     //print_r($aDeclaration);
     //print_r($sImages);
     $aFormValues = array();
     $aFormValues['wrap-columns'] = $this->params->get('csg_wrap_images', 'off');
     $aFormValues['build-direction'] = $this->params->get('csg_direction', 'vertical');
     $aFormValues['image-output'] = $this->params->get('csg_file_output', 'PNG');
     $oSpriteGen = new CssSpriteGen($sImageLibrary, $aFormValues);
     $aImageTypes = $oSpriteGen->GetImageTypes();
     $oSpriteGen->CreateSprite($aImages);
     $aSpriteCss = $oSpriteGen->GetCssBackground();
     //print_r($aSpriteCss);
     $aNeedles = array();
     $aReplacements = array();
     $sImageSelector = '';
     $sBaseUrl = JURI::base(true);
     $sBaseUrl = $sBaseUrl == '/' ? $sBaseUrl : $sBaseUrl . '/';
     for ($i = 0; $i < count($aSpriteCss); $i++) {
         if (@$aSpriteCss[$i]) {
             $aNeedles[] = $aDeclaration[$i];
             preg_match('~(?<=background)(?:[^;]+?)(
                         (?<!/)\\b(?:aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|purple|red|silver|teal|white|yellow)\\b(?!/)
                         |\\#[a-fA-F\\d]{3,6}
                         |rgb\\([^\\)]+?\\))
                         (?:[^;]*?);~ix', $aDeclaration[$i], $aBgColor);
             preg_match('~(?<=background)(?:[^;]+?)\\s(
                             scroll|fixed
                             )\\s(?:(?:[^;]+?)?;)~ix', $aDeclaration[$i], $aBgAttach);
             $sBgImage = 'url(' . $sBaseUrl . 'images/jch-optimize/' . $oSpriteGen->GetSpriteFilename() . ')';
             $sBackground = 'background: ' . @$aBgColor[1] . ' ' . $sBgImage . ' ' . @$aBgAttach[1] . ' ' . $aSpriteCss[$i] . ' no-repeat; ';
             $sDecUnique = preg_replace('~background[^;]+?;~sx', '', $aDeclaration[$i]);
             $aReplacements[] = str_replace('{', '{' . $sLnEnd . $sBackground, $sDecUnique);
         }
     }
     $sCss = str_replace($aNeedles, $aReplacements, $sCss);
     //$sCss    =   $sImageSelector.'{background-image:'.$sBgImage.');}'.$sCss;
     return $sCss;
 }