2018年1月

使用 fit 方法

public Intervention\Image\Image fit( int $width, [ [int $height], [Closure $callback, [ string $position ]] ])
// open file a image resource
$img = Image::make('public/foo.jpg');

// crop the best fitting 5:3 (600x360) ratio and resize to 600x360 pixel
$img->fit(600, 360);

// crop the best fitting 1:1 ratio (200x200) and resize to 200x200 pixel
$img->fit(200);

// add callback functionality to retain maximal original image size
$img->fit(800, 600, function ($constraint) {
    $constraint->upsize();
});

EventTarget 接口 addEventListener, removeEventLisener, dispatchEvent

// Simple implementation of EventTarget
var EventTarget = function() {
  this.listeners = {};
};

EventTarget.prototype.listeners = null;
EventTarget.prototype.addEventListener = function(type, callback) {
  if (!(type in this.listeners)) {
    this.listeners[type] = [];
  }
  this.listeners[type].push(callback);
};

EventTarget.prototype.removeEventListener = function(type, callback) {
  if (!(type in this.listeners)) {
    return;
  }
  var stack = this.listeners[type];
  for (var i = 0, l = stack.length; i < l; i++) {
    if (stack[i] === callback){
      stack.splice(i, 1);
      return;
    }
  }
};

EventTarget.prototype.dispatchEvent = function(event) {
  if (!(event.type in this.listeners)) {
    return true;
  }
  var stack = this.listeners[event.type];

  for (var i = 0, l = stack.length; i < l; i++) {
    stack[i].call(this, event);
  }
  return !event.defaultPrevented;
};