chitosystems/silverstripe-watermarkable

Silverstripe 图像水印

安装: 13

依赖: 0

建议者: 0

安全性: 0

星标: 0

关注者: 3

分支: 7

类型:silverstripe-module

dev-master 2016-02-16 04:42 UTC

This package is auto-updated.

Last update: 2024-09-21 19:57:44 UTC


README

扩展 SilverStripe 3 网站以添加图像水印。

基本用法

从 "WatermarkImage" 类扩展您的图像类,并实现方法 getWatermarkgetWatermarkPositiongetWatermarkTransparency

<?php

class MyImage extends WatermarkImage {

  // by overriding this, you can define whether to automatically add the watermark or not
  // (this can also be controlled in templates for every single image)
  protected $addWatermark = true;
  
  /**
   * @return Image
   */
  public function getWatermark() {
    // in this example we assume has an image named "Watermark"
    $siteConfig = SiteConfig::current_site_config();
    if ($siteConfig->Watermark()) {
      return $siteConfig->Watermark();
    }
    return null;
  }
  
  /**
	 * @return int
	 */
  public function getWatermarkPosition() {
    // return the position at which the watermark should appear on the image
    // can be 1 to 9 (representing the positions on your number pad)
    return 3; // bottom right
  }
  
	/**
	 * @return int
	 */
  public function getWatermarkTransparency() {
    // return the transparency of the watermark
    // can be 0 to 100 (0 = fully transparent, 100 = no transparency)
    return 90;
  }
  
}

在您的 DataObject 中,使用类 MyImage 而不是 Image 来处理图像。

<?php
class MyDataObject extends DataObject {
  
  public static $has_one = array(
    'CoverImage' => 'MyImage'
  );
  
  public static $has_many = array(
    'Images' => 'MyImage'
  );
  
}

模板用法

在您的模板中,您可以开启和关闭水印

<% with $CoverImage %>
  <!-- this image will have a watermark, if $addWatermark (from the first example) is set to true, 
       otherwise the watermark is omitted -->
  <img src="$SetRatioSize(400, 300).URL" />
<% end_with %>

<% loop Images %>
  <!-- example: we do not want the watermark to appear on thumbnails, but we want it on our big images -->
  <a href="$WithWatermark.SetRatioSize(800, 800).URL">
    <img src="$WithoutWatermark.CroppedImage(100, 100).URL" />
  </a>
<% end_loop %>

添加标准图像水印

您也可以在 PHP 中显式添加图像水印

// assuming we have a $has_one = array('Image' => 'Image')
$this->Image()->addWatermark($watermarkImageObject, $watermarkPosition, $watermarkTransparency);

欢迎您改进此模块并向我发送 Pull Requests。