Class: MediaPlugURL

Inherits:
Object
  • Object
show all
Defined in:
lib/mediaplug/mediaplug_url.rb

Overview

The MediaPlugURL is a class that allows you to chain together MediaPlug actions and then get the resulting URL by using its to_s method. It should be instantiated through the MediaPlug class, not by itself:

Constant Summary collapse

FLIP_MAP =
{"vertical" => "v",
"horizontal" => "h",
"both" => "b"}

Instance Method Summary collapse

Constructor Details

#initialize(server, media_type, url, format, access_id) ⇒ MediaPlugURL

Returns a new instance of MediaPlugURL.



6
7
8
9
10
11
12
13
# File 'lib/mediaplug/mediaplug_url.rb', line 6

def initialize(server, media_type, url, format, access_id)
  @server = server
  @media_type = media_type
  @url = url
  @format = format
  @access_id = access_id
  @actions = []
end

Instance Method Details

#clipping(start_at, duration, options = {}) ⇒ Object

Video and Audio Only: Clip the video or audio to any duration and starts at the specified time. Video Preview Options:

Framestep is duration in seconds of per frame capture

  • :width: The width of thumbnail image

  • :height: The height of thumbnail image



438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/mediaplug/mediaplug_url.rb', line 438

def clipping(start_at, duration, options={})
  raise MediaPlug::InvalidAction, "Only audio or video may be clipped." unless video? or audio?
  
  action = video? ? "video_clipping " : "audio_clipping "
  
  action << " start=#{start_at}" if start_at
  action << " duration=#{duration}" if duration
  action << " format=#{@format}"
  
  @actions << action
  self    
end

#convert(new_format, options = {}) ⇒ Object

Converts the source into the specified format, with the options provided (options vary based on media type).

Allowable Formats:

  • Video: flv, mp4, mov, ipod, psp, 3gpp

  • Audio: mp3

  • Image: jpg, png, gif

Video Conversion Options:

  • :bitrate: The bitrate in which to encode (e.g. “200k”)

  • :width: The width of encoded video

  • :height: The height of encoded video

Audio Conversion Options:

  • :bitrate: The bitrate in which to encode (e.g. “64k”)

  • :samplerate: The sample rate with which to encode (e.g. 128)



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/mediaplug/mediaplug_url.rb', line 345

def convert(new_format, options = {})
  options.keys.each{|k| options[k.to_sym] = options.delete(k)}
  
	if image?
		action = "rotate 0 format=#{new_format}"
	elsif audio?
   action = "audio_convert format=#{new_format}"
   action << " bitrate=#{options[:bitrate]}" if options[:bitrate]
   action << " samplerate=#{options[:samplerate]}" if options[:samplerate]
	elsif video?
   action = "video_convert format=#{new_format}"
   action << " bitrate=#{options[:bitrate]}" if options[:bitrate]
   action << " width=#{options[:width]}" if options[:width]
   action << " height=#{options[:height]}" if options[:height]
	end
  @actions << action
  self
end

#crop(string_or_hash_or_array) ⇒ Object

Options:

  • :gravity: *Image Only* The position of crop. May be any of NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast. Default is center.

  • :x-offset: x coordinate offset of the top-left corner related to center position set by the gravity parameter.

  • :y-offset: y coordinate offset of the top-left corner related to center position set by the gravity parameter.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mediaplug/mediaplug_url.rb', line 73

def crop(string_or_hash_or_array)
  argument = string_or_hash_or_array
  raise MediaPlug::InvalidAction, "Only images or videos may be cropped." unless image? || video?
  action = image? ? "crop " : "video_crop "
  
  case argument
  when String, Array
    argument = argument.split("x") if argument.is_a?(String)      
    action << "gravity=center " if image?
    action << "width=#{argument[0]} height=#{argument[1]} "
  when Hash
    argument.keys.each{|k| argument[k.to_sym] = argument.delete(k)}
    action << "gravity=#{argument[:gravity]} " if argument[:gravity] && image?
    action << "x-offset=#{argument[:x_offset]} " if argument[:x_offset]
    action << "y-offset=#{argument[:y_offset]} " if argument[:y_offset]
    if argument[:width] && argument[:height]
      action << "width=#{argument[:width]} height=#{argument[:height]} "
    else
      raise MediaPlug::InvalidAction, "Could not understand the options given to the crop command."
    end
  end
  
  action << "format=#{@format}"
  @actions << action
  self
end

#detection(detection_type, options = {}) ⇒ Object

Example:

image = mp.image('http://some.url/')
image.detection('Face') #apply face detection on the Image


316
317
318
319
320
321
322
323
324
325
# File 'lib/mediaplug/mediaplug_url.rb', line 316

def detection(detection_type, options = {})
  raise MediaPlug::InvalidAction, "Only images may be detected." unless image?
  action = "detection "
  
  action << "type=#{detection_type} "
  action << "format=#{@format}"
  
  @actions << action
  self    
end

#filter(filter_type, options = {}) ⇒ Object

Image Only: Apply Art-Like Filter to the image. Valid Filter Type:

Vignette, OilPainting, Charcoal, Sketch, Emboss

Options:

  • :level: The filter mask size, maybe low, medium, high.

Example:

image = mp.image('http://some.url/')
image.filter('vignette')


211
212
213
214
215
216
217
218
219
220
# File 'lib/mediaplug/mediaplug_url.rb', line 211

def filter(filter_type, options={})
  raise MediaPlug::InvalidAction, "Only images may be swirled." unless image?
  action = "filter "  
  action << "filter_type=#{filter_type} "   
  options.keys.each{|k| options[k.to_sym] = options.delete(k)}       
  action << "level=#{options[:level]} " if options[:level]
  action << "format=#{@format}"
  @actions << action
  self
end

#flip(direction) ⇒ Object

Image Only: Flip the image in the given direction.

Valid Directions:

  • :horizontal or :h

  • :vertical or :v

  • :both or :b

Example:

image = mp.image('http://some.url/')
image.flip(:both)


139
140
141
142
143
144
145
146
147
148
149
# File 'lib/mediaplug/mediaplug_url.rb', line 139

def flip(direction)
  raise MediaPlug::InvalidAction, "Only images or video may be flipped." unless image? || video?
  argument = direction
  argument = FLIP_MAP[argument.to_s] if FLIP_MAP.key?(argument.to_s)
  if image?
  "flip #{argument} format=#{@format}"
  else
  "video_flip #{argument} format=#{@format}"
  end
  self
end

#implode(factor) ⇒ Object

Image Only: Create IMPLODE or EXPLODE effect on the image by the given number of factor.

Valid impode factor: [-1..1] nagtive value: expolode effect, positive value: implode effect Example:

image = mp.image('http://some.url/')
image.implode(1)


244
245
246
247
248
249
250
251
252
# File 'lib/mediaplug/mediaplug_url.rb', line 244

def implode(factor)
  raise MediaPlug::InvalidAction, "Only images may be imploded." unless image?
  action = "implode "    
  action << "factor=#{factor}"
  
  action << " format=#{@format}"
  @actions << action
  self
end

#metadata!Object

Generate a URL to retrieve the metadata about a file instead of the file itself.



459
460
461
462
# File 'lib/mediaplug/mediaplug_url.rb', line 459

def metadata!
  @metadata = true
  self
end

#polaroid(options = {}) ⇒ Object

Image Only: Create Polaroid Frame for the image.

Options:

  • :angle: The tilting angle of the Polaroid Frame .

  • :caption: Text annontation that to be added at the bottom of the Polaroid Frame.

Example:

image = mp.image('http://some.url/')
image.polaroid(:angle => '15', :caption => "Group Picture 3/15/2011")


175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/mediaplug/mediaplug_url.rb', line 175

def polaroid(options={})
  raise MediaPlug::InvalidAction, "Only images may be swirled." unless image?
  action = "polaroid "    
  options.keys.each{|k| options[k.to_sym] = options.delete(k)}

  action << "angle=#{options[:angle]} " if options[:angle]
  action << "caption=#{options[:caption]} " if options[:caption]
  
  action << "format=#{@format}"
  @actions << action
  self
end

#preview(framestep, options = {}) ⇒ Object

Video Only: Generates an animated GIF image for video preview. Video Preview Options:

Framestep is duration in seconds of per frame capture

  • :width: The width of thumbnail image

  • :height: The height of thumbnail image



387
388
389
390
391
392
393
394
395
396
397
# File 'lib/mediaplug/mediaplug_url.rb', line 387

def preview(framestep, options={})
  raise MediaPlug::InvalidAction, "Only videos may be previewed." unless video?
  options.keys.each{|k| options[k.to_sym] = options.delete(k)}
  
  action = "preview framestep=#{framestep}"
  action << " width=#{options[:width]}" if options[:width]
  action << " height=#{options[:height]}" if options[:height]
  @media_type = :image
  @actions << action
  self
end

#recache!Object

Causes the resultant URL to be recached. Should not be called each time.



452
453
454
455
# File 'lib/mediaplug/mediaplug_url.rb', line 452

def recache!
  @actions.unshift("recache")
  self
end

#resize(string_or_hash_or_array) ⇒ Object

Image Only: Resize an image to the dimensions specified. The dimensions may be specified as a string, a Hash, or an Array.

Example:

image = mp.image('http://some.url/', :png)
image.resize('100x100')
image.resize(:width => 100, :height => 100)
image.resize([100,100])
# resize width to 128, height proportionally
image.resize(:width => 128)

# halve the image size
image.resize(["50%","50%"])


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mediaplug/mediaplug_url.rb', line 34

def resize(string_or_hash_or_array)
  argument = string_or_hash_or_array
  raise MediaPlug::InvalidAction, "Only images may be resized." unless image?
  action = "resize "
  
  case argument
  when String, Array
    argument = argument.split("x") if argument.is_a?(String)
    action << "width=#{argument[0]} height=#{argument[1]}"
  when Hash
    argument.keys.each{ |k| argument[k.to_sym] = argument[k] }
    action << "#{"width=#{argument[:width]}" if argument[:width]}#{" height=#{argument[:height]}" if argument[:height]}"
  end
  
  action << " format=#{@format}"
  
  @actions << action
  self
end

#rotate(degree) ⇒ Object

Image Only: Rotate the image by the given number of degrees.

Example:

image = mp.image('http://some.url/')
image.rotate(90)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mediaplug/mediaplug_url.rb', line 106

def rotate(degree)
  raise MediaPlug::InvalidAction, "Only images may be rotated." unless image?
  argument = degree
  action = "rotate "
  
  case argument
  when String, Integer
    action << "degree=#{argument}"
  when Hash
    action << action_hash_to_string(argument)
  end
  
  action << " format=#{@format}"
  @actions << action
  self
end

#shortenObject



14
15
16
17
18
# File 'lib/mediaplug/mediaplug_url.rb', line 14

def shorten
	raise MediaPlug::AccessIdMissing if @access_id.blank?
  @mp_shorten ||= Mediaplug::Client.new(@server, @access_id)  	
 @mp_shorten.shorten(self.to_s)
end

#swirl(degree) ⇒ Object

Image Only: Create SWIRLING effect on the image by the given number of amplitude and wavelength.

Example:

image = mp.image('http://some.url/')
image.swirl(180) #create a swirling effect with degree = 180


156
157
158
159
160
161
162
163
164
# File 'lib/mediaplug/mediaplug_url.rb', line 156

def swirl(degree)
  raise MediaPlug::InvalidAction, "Only images may be swirled." unless image?
  action = "swirl "    
  action << "degree=#{degree}"
  
  action << " format=#{@format}"
  @actions << action
  self
end

#thumbnail(seek, options = {}) ⇒ Object

Video Only: Generates a thumbnail of a video at the specified frame. Video Thumbnail Options:

  • :width: The width of thumbnail image

  • :height: The height of thumbnail image



369
370
371
372
373
374
375
376
377
378
379
# File 'lib/mediaplug/mediaplug_url.rb', line 369

def thumbnail(seek, options={})
  raise MediaPlug::InvalidAction, "Only videos may be thumbnailed." unless video?
  options.keys.each{|k| options[k.to_sym] = options.delete(k)}
  
  action = "thumbnail seek=#{seek}"
  action << " width=#{options[:width]}" if options[:width]
  action << " height=#{options[:height]}" if options[:height]
  @media_type = :image
  @actions << action
  self
end

#to_sObject

Outputs the final URL taking into account all of the actions that have been chained onto this MediaPlugURL



466
467
468
469
470
471
472
473
# File 'lib/mediaplug/mediaplug_url.rb', line 466

def to_s
  url = "http://#{@server}/mp/get?mpsrc=#{URI.escape(@url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}#{"&mpaction=#{URI.escape(@actions.join(","), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}" if @actions.any?}"
  if @metadata
    "http://#{@server}/mp/metadata?mpsrc=#{url}"
  else
    url
  end
end

#video_watermark(text, options = {}) ⇒ Object

  • :x_offset: integer, x-offset, x coordiante for the location of the top left corner of text watermark

  • :y_offset: integer, y-offset, y coordiante for the location of the top left corner of text watermark

  • :font_color: The color to be used for drawing fonts. Either a string (e.g. “red”) or in 0xRRGGBB format (e.g. “0xff000033”), possibly followed by an alpha specifier ([email protected] red withe alpah = 0.2).

    The default value of fontcolor is "0x80acbc44".
    
  • :bg_color: The color to be used for drawing box around text. Either a string (e.g. “yellow”) or in 0xRRGGBB format (e.g. “0xff00ff”), possibly followed by an alpha specifier ([email protected] red withe alpah = 0.2).

    If no value is specified, no box will be drawn around the text.
    


416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/mediaplug/mediaplug_url.rb', line 416

def video_watermark(text, options={})
  raise MediaPlug::InvalidAction, "Only videos may be video watermarked." unless video?
  options.keys.each{|k| options[k.to_sym] = options.delete(k)}
  
  action = "video_watermark text=#{text}"
  action << " text-font-size=#{options[:font_size]}" if options[:font_size]
  action << " x-offset=#{argument[:x_offset]} " if argument[:x_offset]
  action << " y-offset=#{argument[:y_offset]} " if argument[:y_offset]
  action << " font-color=#{options[:font_color]}" if options[:font_color]
  action << " bg-color=#{options[:bg_color]}" if options[:bg_color]
  action << " format=#{@format}"
  
  @actions << action
  self
end

#water_reflectionObject

Example:

image = mp.image('http://some.url/')
image.water_reflection()


193
194
195
196
197
198
199
200
# File 'lib/mediaplug/mediaplug_url.rb', line 193

def water_reflection
  raise MediaPlug::InvalidAction, "Only images may be swirled." unless image?
  action = "water_reflection "    

  action << "format=#{@format}"
  @actions << action
  self
end

#watermark(watermark_type, text_or_url, options = {}) ⇒ Object

AvantGarde-Book, AvantGarde-BookOblique, AvantGarde-Demi, AvantGarde-DemiOblique, Bookman-Demi, Bookman-DemiItalic, Bookman-Light, Bookman-LightItalic Courier, Courier-Bold, Courier-BoldOblique, Courier-Oblique, Helvetica, Helvetica-Bold, Helvetica-BoldOblique, Helvetica-Narrow, Helvetica-Narrow-Bold Helvetica-Narrow-BoldOblique, Helvetica-Narrow-Oblique, Helvetica-Oblique, Palatino-Bold, Palatino-BoldItalic, Palatino-Italic, Palatino-Roman Symbol, Times-Bold, Times-BoldItalic, Times-Italic, Times-Roman, Century-Schoolbook-Bold, Century-Schoolbook-Bold-Italic, Century-Schoolbook-Italic Century-Schoolbook-Roman, DejaVu-Sans-Bold, DejaVu-Sans-Book, DejaVu-Sans-Mono-Bold, DejaVu-Sans-Mono-Book, DejaVu-Serif-Bold, DejaVu-Serif-Book Dingbats-Regular, URW-Chancery-Medium-Italic



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/mediaplug/mediaplug_url.rb', line 276

def watermark(watermark_type, text_or_url, options = {})
  raise MediaPlug::InvalidAction, "Only images may be watermarked." unless image?
  action = "watermark "
  options.keys.each{|k| options[k.to_sym] = options.delete(k)}
  
  case watermark_type
  when :image
    action << "url=#{text_or_url} "
  when :text
    action << "text=#{text_or_url.gsub(' ',"\\ ")} "
    action << "style=#{options[:style]} " if options[:style]
    action << "weight=#{options[:weight]} " if options[:weight]
    action << "text-font-size=#{options[:font_size]} " if options[:font_size]
    action << "font-type=#{options[:font_type]} " if options[:font_type]
  else
    raise MediaPlug::InvalidAction, "The watermark_type must be :image or :text"
  end
  
  action << "gravity=#{options[:gravity]} " if options[:gravity]
  action << "alpha=#{options[:alpha]} " if options[:alpha]
  action << "x-offset=#{argument[:x_offset]} " if argument[:x_offset]
  action << "y-offset=#{argument[:y_offset]} " if argument[:y_offset]
  
  action << "format=#{@format}"
  
  @actions << action
  self
end

#wave(amp, wl) ⇒ Object

Image Only: Create WAVE effect on the image by the given number of amplitude and wavelength.

Example:

image = mp.image('http://some.url/')
image.wave(50,200) #create a wave effect with amplitude = 50 and wavelength = 200


227
228
229
230
231
232
233
234
235
# File 'lib/mediaplug/mediaplug_url.rb', line 227

def wave(amp, wl)
  raise MediaPlug::InvalidAction, "Only images may be waved." unless image?
  action = "wave "    
  action << "amplitude=#{amp} wavelength=#{wl}"
  
  action << " format=#{@format}"
  @actions << action
  self
end