【问题标题】:Dynamic image watermarking - injecting model attributes into watermarks动态图像水印——将模型属性注入水印
【发布时间】:2013-02-15 20:34:27
【问题描述】:

我目前正在使用 this Paperclip::Processor 通过 imagemagick 的复合命令为图像添加水印。

它允许我注入一个预制的单个文件作为水印,它存在于我的公共文件中。

但是,我正在尝试确定是否可以对其进行修改以动态生成包含模型属性的水印。例如,水印包括摄影师的姓名,或拍摄它的相机型号。

Getty 最近有changed their watermark to do just this - 它非常聪明,我很想知道他们是如何做到的。

提前非常感谢。我不希望人们确切地知道如何做到这一点,但任何想法或一般原则将不胜感激。

【问题讨论】:

  • 我对这种事情没有直接的经验,但我想它可以让你添加动态文本而不仅仅是静态图像,所以你能不喜欢吗?

标签: ruby-on-rails image-processing imagemagick paperclip watermark


【解决方案1】:

是的,它可以用 Imagemagick 完成;我已经用 php 和一个批处理脚本完成了它。不过,我不知道如何将此批处理脚本转换为 ruby​​-on-rails。

您可以获取 EXIF 数据中包含的大多数值,并在与此类似的代码中使用它们。

:: Do not display the code while running 
@ECHO OFF 

:: Select the F number from the EXIF data and set the FNumber variable 
FOR /F %%x IN ('identify -ping -format "%%[EXIF:FNumber]" %1') DO SET FNumber=%%x 

:: Set the FNumber1 variable to the F number value 
:: Image Magick returns the F number in the format of 700/100 or 40/10 
FOR /F %%x IN ('convert xc: -ping -format "%%[fx:%FNumber%]" info:') DO SET FNumber1=%%x 

:: Set the value of the shutter variable to the shutter speed 
:: Select the shutter speed from the EXIF data 
:: Image Magick returns the shutter speed in the format of 810/100 
FOR /F %%x IN ('identify -ping -format "%%[EXIF:ShutterSpeedValue]" %1') DO SET shutter=%%x 

:: Format the speed to 8.1 and set the speed variable 
FOR /F %%x IN ('convert xc: -ping -format "%%[fx:%shutter%]" info:') DO SET speed=%%x 

:: Calculate the speed in seconds using the power of 2 
:: and save it in the shutterspeed variable 
FOR /F %%x IN ('convert xc: -ping -format "%%[fx:floor((pow(2,%speed%)))]" info:') ^ 
DO SET shutterspeed=%%x 

:: Add the F number and shutter speed to the image 
:: and save as exif_OriginalImageName 
convert %INPUTFILE% ^ 
-pointsize 16 -fill black -gravity northwest ^ 
-annotate +10+5 "Aperture: F%FNumber1% Shutter speed: 1/%shutterspeed% sec" "%~p1EXIF_%~n1.jpg"  

【讨论】:

  • 谢谢,非常感谢。实际上,我在 RoR 上找到了一种非常简单的方法......在下面回答。
【解决方案2】:

感谢您对比灵顿的帮助。实际上,我通过在回形针缩略图过程中将convert_options 添加到图像中找到了一种方法。所以,在图像模型上,a 是图像:

has_attached_file :image, 
    processors: [:thumbnail],
    styles: { 
      wide: {
        geometry: "1120x",
        convert_options: ->(a) { "-quality 92 -font Arial -pointsize 72 -gravity center gradient: -alpha on -channel rgba -fill 'rgba(255,255,255,0.3)' -opaque 'rgba(20,20,20,1)' -draw \"text 0, 340 #{a.picusername}\" -pointsize 30 -draw \"text 0, 390 'license copy here'\"  
      "}
      }

这会在图像上写上a.picusername,并带有各种位置和样式细节。你可以找到更多这些here

最后一点 - 如果您使用类似 simpleform 的方式向图像模型添加属性,则上述处理指令会在图像本身被添加到数据库的瞬间应用...因此添加任何模型属性之后 图像附件(字面意思是图像附件下方的表单输入)将无法识别,因为它们尚不存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多