Gallery:我是如何将slider外观主题嵌入到我的网站中的

来自站长百科
Firebrance讨论 | 贡献2008年9月17日 (三) 16:30的版本
跳转至: 导航、​ 搜索

使用模板的可视化嵌入

介绍

我曾多次被问到我的网站的样子是如何实现的。Gallery在我的网站 http://underwa.ter.net中融合地很好,机关我所使用的外观主题自身无法适用于这样的整合方式。但是我喜欢这样的哇你管主题,因此我根据自己的需要做了修改。我认为如果你真的希望gallery成为自己网站规划中不可或缺的一部分的话,某些形式的修改总是必要的。下面我就分享一下我在整合时的一些心得。

网站设计规划

首先在开始之前应当做一个网站设计规划。要使用什么样的布局?网站的导航方式?使用什么样的色彩?我想要的是一个非常简单的设计样式,简洁明快,这样就不会喧宾夺主了。我选择使用顶部导航条,这将我的站点分成了一些主要部分:文件夹,旅游报告和新闻以及关于。在整合gallery之前应当做好此步。我认为你应当让gallery来融入到你的设计中而不是反过来让你的设计来适应gallery。

除了PHP部分和gallery之外,我使用Dreamweaver进行站点的组建。对我来说,Dreamweaver在设计开发中表现得很不错,尤其是在CSS处理方面极为优秀。Dreamweaver允许我创建模板的特定部分,如导航条,因此如果我做出一些修改那么整个站点的对应部分就会改变(除了gallery不受影响,我会对此做解释的)。就我个人而言,我尝试使用某些形式的软件来做此事,因为维护起来较容易许多。

Gallery

现在我有了自己的设计方案。我喜欢滑动式的外观主题,因此我希望将其作为图片显示的基本方案。当你在box外使用gallery时,它就会变成一个独立的网页,并且具有自己的外观效果。你可以选用某个外观主题和颜色包,但它看起来仍和原来一样。网站往往不是这个样子,因此我开始逛论坛来看看如何修改gallery以更好地融入整合到我自己的网站中。

阅读Gallery文档资料

Gallery has quite a bit of information about embedding and integrating. Here are some links I used:

Since I wasn't using an existing CMS, I figured I could use the 'editing theme templates' method of integrating gallery. Basically you change the templates that the theme provides to fit your own design. That's what makes gallery such a wonderful tool, it allows you to seperate functionality from design.

创建本地模板

Gallery has a great method to change the templates. Just go to the 'slider' theme directory, and create a directory 'local' in whatever directory the file you want to change exists in. I started by editing theme.tpl so I created a directory 'local' inside the 'templates' directory and copied 'theme.tpl' to that directory. Now I started to edit this template to have a navigation bar. This meant adding some HTML near the top of the templates. You can find the resulting file here. Look for comments marked by the string COR.

滑动外观主题的问题

Unfortunately, this didnt show me a navbar! Whats going on. Maybe I needed to reload the templates. This is something you always have to do when editing templates. Go the the Site Admin link, go to Maintenance, and find the template cache deletion link. Use it often. It didnt fix my problem though. I looked at the source, and saw that the navbar was actually there. So why didnt I see it? I looked a bit closer at the slider theme and noticed it uses absolute positioning. Basically it put the image on top of my navbar making it invisible. Ok, thats not good.

滑动外观主题的修改

Im not going to explain CSS or HTML. Buy a book :) I figured I had to change the css to fix this absolute positioning problem. So, I made a directory 'local' in the same directory as theme.css and copied theme.css there. Right at the top of the css file you see the problem. It positions the image area in the top left corner of the screen. Argh. Ok, what if I change it so the top is not at 0 but at 50 px? Ok, that did something. The image area was moved 50 pixels down and I saw my navbar at the top.

水平导航条

Before I continue, I need to explain my navbar, because some people might not understand it. If you do what I did sofar, the navbar will look ugly. It's not a horizontal navbar at all, but an ugly list. Im not going to fully explain how to go from a vertical list to a horizontal navbar. Use google. It is styled using CSS, and I copied the style information from my site CSS file to the theme CSS file. You can see the resulting theme.css here. With that CSS file you see my navbar on top, but the rest of the gallery stuff still looks like plain old slider theme.

调整色彩

Next I figured id have to change the colors of the slider theme to match my own colors. This again means changing CSS information. I need to change all 'background-color' elements to my own background color. Here's the resulting theme.css. Look for color #282828. This is starting to look good.

解决布局问题

Things arent quite finished though. First of all I wanted to do some more minor visual changes, but more importantly, there are some layout bugs. In IE (what else is new) the current CSS is not working correctly as the image is now being overlapped by the thumbnail bar, no doubt caused by lowering the image display area 50 pixels. Im also seeing some ugly jumping of the navbar when switching from my own pages to the gallery theme pages. In my own pages I needed to do some CSS tricks to avoid that from happening. It's basically working around CSS implementation differences. I need to add these workarounds to the theme.css file.

To fix the overlapping layout in IE, it wasnt enough to change the CSS. The slider theme extensively uses Javascript, and it's in this JS that we have to fix another problem. It computes the size of the image area, and because we lowered the image area 50px, this computation is now wrong. The changed slider.js can be seen here. The changes are around line 186. I also made another minor change to add a permanent scrollbar on the right. This fixes the jumping navbar problem. There may be better fixes for this, but this is what I did.

if (!getstr && !document.slide) return;
var w = data_iw[image_index], h = data_ih[image_index],
aw = image_area.offsetWidth, ah = image_area.offsetHeight, a=0;
  ah = ah - 6;
  aw = aw - 6;
  if(app_is_ie) {
    ah = ah - 80;
  }
if (w > aw || h > ah) {
if ((a = h/w) < ah/aw) {
w = aw; h = Math.round(aw*a);

Argh, this didnt work. The changed javascript wasnt being executed. I quickly noticed that I also need to change header.tpl in the templates directory. Copy it to the local dir and make it load slider.js from the local dir. Here's my header.tpl. I also had to make another small change in theme.tpl, as it has a line to hide the scrollbar, and I wanted to force the scrollbar to avoid jumping of the screen. You can decide for yourself if you want or need this. I commented out the line:

{*body.gallery { overflow: hidden; }*}

额外的修改

我们还没有完成呢。我还想要更多的视觉效果修改。我会一一道来的。


  • 首先我希望图片周围有边界。这可以通过CSS来达成。在theme.css 文件的顶部某处加入下面的语句。
div#image img {
  border: 3px solid #ffffff;
}


  • 我希望gallery的明朝位于导航条的左侧。这就意味着要修改theme.tpl,也就是导航条添加之处。我加入了以下语句行。如果你是管理员的话,它还会添加一个下拉框。请别忘了清除模板缓存:)
{if !empty($theme.item.title)}
  {$theme.item.title|markup}
  {if $user.isRegisteredUser}
    {g->block type="core.ItemLinks"}
  {/if}
{/if}
  • 修改了CSS文件中的一些字体属性。这并非很重要的。
  • 我不想让普通读者看到维护菜单的扳手图标。所以我修改了slider.tpl,这样一来只有管理员可以看到。参见slider.tpl

被修改的外观主题文件

That concludes all the changes. I also changed some minor things in the theme configuration inside gallery. Specifically, I limited the max size each image could be viewed at by non-administrators. Experiment with it.

All the files that I changed can be viewed here. Changes are usually commented, but you may want to do a diff between the original and my version. I may have also forgotten some changes, as Im doing a long time after the actual work. If you have any questions, feel free to send me a message. Remember though, this doc is not meant to fix your own site. It is merely a diary of how I changed my site. You'll almost certainly have to deviate from this document for your own site. And I may also not have chosen the right way to do things, I dont know. But it works for me, so im happy.



额外的嵌入

Besides changing the theme I also did some other forms of embedding. You can call gallery functions from within any PHP file which allows you to do interesting tricks outside of gallery itself. My site contains several such additions..

  • The homepage has two randomized images. I did that by making two special albums inside gallery, one for me, one for my wife. We can each drop images in that gallery and have them automatically be used for this random image. The code for it. Include() it in your regular php file, and change whatever you have to change to make it work for you.

不要不做修改就使用此代码,因为这只是示例。红色部分是肯定要修改为正确的相册ID的

<?php
require_once(dirname(__FILE__) . "/../gallery2/embed.php");
$ret = GalleryEmbed::init(array('embedUri' => '/gallery2','g2Uri' => '/gallery2/','fullInit' => true));
list ($ret, $imageBlockCor) = GalleryEmbed::getImageBlock(array('blocks' => 'randomImage', 'show' => 'none', 'itemId' => '120' ));
list ($ret, $imageBlockJulie) = GalleryEmbed::getImageBlock(array('blocks' => 'randomImage', 'show' => 'none', 'itemId' => '121' ));
$url = $_SERVER['REQUEST_URI'];
if(preg_match('/(<img.*\\>)/', $imageBlockCor, $matches)) {
  $cor = $matches[1];
}
if(preg_match('/(<img.*\\>)/', $imageBlockJulie, $matches)) {
  $julie = $matches[1];
}

echo "<a href=\"/gallery2/main.php?g2_view=core.ShowItem&g2_itemId=147\">$cor</a>\n"; echo "<a href=\"/gallery2/main.php?g2_view=core.ShowItem&g2_itemId=148\">$julie</a>\n";

GalleryEmbed::done();
?>
  • The travel page has a list of thumbnails on the right. These are auto-generated! If I add a travel album, it automatically gets displayed there. I do some extra tricks to allow me to only make it visible when im ready. All the albums here are children of 1 specific album, which allows me to traverse that album to fetch all these travel galleries.

不要不做修改就使用此代码,因为这只是示例。红色部分是肯定要修改为正确的相册ID和输出HTML的

<?php
require_once(dirname(__FILE__) . "/../gallery2/embed.php");
/* connect to embed library */
$ret = GalleryEmbed::init(array('embedUri' => '/gallery2/index.php','embedPath' => '/gallery2','relativeG2Path' => ,'fullInit' => true));

/* fetch all albums by item id */
list ($error,$albums) = GalleryCoreApi::fetchAlbumTree(69);
/* fetch all thumbnails for these albums */
if(!$error) {
  /* fetch album info for all albums */
  list ($error,$items) = GalleryCoreApi::loadEntitiesById(GalleryUtilities::arrayKeysRecursive($albums));
  if(!$error) {
    $urlGenerator =& $gallery->getUrlGenerator();
    foreach ($items as $item) {
      $title = $item->getTitle();
      $description = $item->getDescription();
      $keywords = $item->getKeywords();
      if(strstr($keywords, "visible")) {
        list($ret, $thumbnails) = GalleryCoreApi::fetchThumbnailsByItemIds(array($item->getid()));
        foreach($thumbnails as $thumbnail) {
          $thumbnailImg = $urlGenerator->generateUrl(array('view' => 'core.DownloadItem', 'href' => "/gallery2/main.php", 'itemId' => $thumbnail->getid()));
          $thumbnailUrl = $urlGenerator->generateUrl(array('view' => 'core.ShowItem', 'itemId' => $item->getid()));
        }
        echo "<div class=\"travelgalleriesgallery\">\n";
        echo "<p><a href=\"$thumbnailUrl\"><img src=\"$thumbnailImg\" /></a></p>\n";
        echo "<p>$title</p>\n";
        $description = unhtmlspecialchars($description);
        echo "<p>$description</p>\n";
        echo "</div> <!-- travelgalleriesgallery -->\n";
      }
    }
  }
}
function unhtmlspecialchars( $string )
{
  $string = str_replace ( '&', '&', $string );
  $string = str_replace ( ''', '\, $string );
  $string = str_replace ( '"', '"', $string );
  $string = str_replace ( '<', '<', $string );
  $string = str_replace ( '>', '>', $string );

  return $string;
}
?>


  • 在文件夹区域中的下拉列表显示所有可查看的文件夹。这些同样都是一特定相册的子,这样我就可以进行traverse。代码:

不要不做修改就使用此代码,因为这只是示例。红色部分是肯定要修改为正确的相册ID和输出HTML的

<?php
require_once(dirname(__FILE__) . "/../gallery2/embed.php");
/* connect to embed library */
$ret = GalleryEmbed::init(array('embedUri' => '/gallery2/index.php','embedPath' => '/gallery2','relativeG2Path' => ,'fullInit' => true));

/* fetch all albums by item id */
list ($error,$albums) = GalleryCoreApi::fetchAlbumTree(70);
if(!$error) {
  /* fetch album info for all albums */
  list ($error,$items) = GalleryCoreApi::loadEntitiesById(GalleryUtilities::arrayKeysRecursive($albums));
  if(!$error) {
     echo "<form>\n";
     echo "<select name=\"url\" onchange=\"location = this.options[this.selectedIndex].value;\">\n";
     echo "<option value=\"#\">View other portfolios</option>\n";
     foreach ($items as $item) {
       $title = $item->getTitle() ? $item->getTitle() :
       $item-> getPathComponent();
       echo "<option value=\"/gallery2/main.php?g2_view=core.ShowItem&g2_itemId=" . $item->getId() . "\">$title</option>\n";
     }
     echo "</select></form>\n";
  }
}
?>



我希望此页面对某些人有所帮助。就算你不使用滑动外观主题你也可以明白如何修改使得gallery的视觉效果符合自己的网站设计风格。如果你是使用该份文档修改的自己的网站的话,请给留个信儿。我很想看看效果如何。

致礼,

Cor Bosman
http://underwa.ter.net