I have a problem with paperclip in rails 3. When I upload a file my processor throws error because imagemagick get command:
"composite -gravity South /home/xxx/xxx/public/images/watermark.png /tmp/a s20121207-5819-1dq7y81.jpg /tmp/a s20121207-5819-1dq7y8120121207-5819-1juqw7a"
composite: unable to open image `/tmp/a':
processor:
def make
dst = Tempfile.new([#basename, #format].compact.join("."))
dst.binmode
if #watermark_path
command = "composite"
params = "-gravity #{#position} #{#watermark_path} #{fromfile} "
params += tofile(dst)
begin
p " >>>>>>>>>>>>>>>>> #{command} #{params}"
success = Paperclip.run(command, params)
rescue PaperclipCommandLineError
success = false
end
unless success
raise PaperclipError, "There was an error processing the watermark for #{#basename}" if #whiny
end
return dst
else
return #file
end
end
def fromfile
File.expand_path(#file.path)
end
def tofile(destination)
File.expand_path(destination.path)
end
it only occurs when filename have whitespace or other non alfanum chars. The /tmp/a should be /tmp/a b.jpg.
I'va tried http://www.davesouth.org/stories/make-url-friendly-filenames-in-paperclip-attachments and more but still filename in processor is wrong
any ideas? or processor that works ok for this issue? :(
Try it maybe with:
dst = Tempfile.new([#basename, #format].compact.join(".").gsub(" ","")
I came back to this today and figured out you just need to enclose the path with a quote as in the ImageMagic documentation (http://www.imagemagick.org/script/command-line-processing.php):
If the image path includes one or more spaces, enclose the path in quotes:
'my title.jpg'
As an example, in my processor I have:
command = "convert \"#{File.expand_path(#file.path)}\" -crop #{crop_area} -resize 150x150 \"#{File.expand_path(destination.path)}\""
Paperclip.run(command)
.
I am on Windows at the moment and single quote doesn't seem to work.
Like you, I tried changing attributes of #file with no success.
Hope it helps.
Related
I’m using TexturePacker from the command line and am unable to get it to pack multiple sprites into 1 sheet. It allows me to do 1 sprite so the command is fine.
This is the command I am using.
"TexturePacker --format sparrow --texture-format atf --opt DXT5
--max-width 2048 --max-height 2048 --size-constraints POT --pack-mode Best --enable-rotation --trim-mode Trim --algorithm MaxRects
--multipack --border-padding 5 --shape-padding 5 --reduce-border-artifacts --scale " + sheetInfo.scale + " --data " + imageOutputDirectory + "\" + lanfPrefix + "\" + sheetInfo.name +
".xml " + "--sheet " + imageOutputDirectory + "\" + lanfPrefix + "\"
+ sheetInfo.name + ".atf image1.png image2.png";
Any ideas why this isn't working? According to the documentation, it should work.
I was unable to find any real way to fix this even contacted the developer of texture packer but got no response. Instead I was able to accomplish the desired outcome by copying all needed files to a temp directory and then add the directory to the end of the texturepacker call instead of individual images.
Due to the poor TexturePacker documentation, it took me much trial and error to figure this out!
To add multiple images to a single sprite sheet, here is a sample command that will create an atlas called out.png (the default) containing images img_1 to img_4...
TexturePacker --format unity-texture2d img_1.png img_2.png img_3.png img_4.png
The key is the list of image filenames separated only by spaces. I am working from Python, so here is the script I use to create the same atlas that the sample line above will give you. Using glob with wildcards allows me to select images from a folder containing many images and eliminates the need to isolate the files I want into a folder just for TexturePacker's sake.
import subprocess, glob
TP = r"C:\Program Files\CodeAndWeb\TexturePacker\bin\TexturePacker.exe"
baseFrame = "img"
def FillAtlas(baseFrame):
globString = baseFrame + "_*.png"
frameList = glob.glob(globString)
imgList = []
for frame in frameList:
imgList.append(frame)
TPargs = [TP, "--format", "unity-texture2d"] + imgList
subprocess.call(TPargs)
FillAtlas(baseFrame)
I´m trying to work out a way in Puppet to get the current zpool capacity numbers for my FreeBSD storage servers, storing them in custom facts and to generate alert if capacity reaches a "too high" level. Closest match to my problem that I´ve found so far is:
Returning multiple custom facts with puppet Facter
That pointed me to this solution:
operatingsystem = Facter.value('operatingsystem')
case operatingsystem
when "FreeBSD"
present_zpools = IO.popen('zpool list -H -o name').read.chomp
if ! present_zpools.empty?
Facter.add(:zpools) do
setcode do
zpools = IO.popen('for i in $(zpool list -H -o name); do echo $i; done').read.chomp.split("\n")
end
end
def addZpoolCapacityFact(zpool)
zpool_capacity = IO.popen('zpool get -H -o value capacity #{zpool}').read.tr('%','').chomp
Facter.add("capacity_" + zpool) do
setcode do
zpool_capacity
end
end
end
zpools = Facter.value(:zpools)
zpools.each do |zpool|
addZpoolCapacityFact(zpool)
end
end
end
But doesn´t quite produce the result I was expecting, e.g:
capacity_pool1: 10 30
capacity_pool2: 10 30
When I was really expecting:
capacity_pool1: 10
capacity_pool2: 30
What am I doing wrong?
OK, solved!
The problem was using IO.popen two times in same script, even though I tried nil'ing the variables, the first split function applied to variable 'zpools' was also run on 'zpool_capacity', I think, which made the result look like:
"capacity_pool1":"10\n12","capacity_pool2":"10\n12"
Notice the '\n' between the numbers? I´m sure there´s a Ruby way to be able to use IO.popen multiple times but I don´t know how, so I just changed the commands to execute with plain backticks (`) and here´s the working code:
operatingsystem = Facter.value('operatingsystem')
case operatingsystem
when "FreeBSD"
present_zpools = `zpool list -H -o name`.chomp
if ! present_zpools.empty?
Facter.add(:zpools) do
setcode do
zpools = `for i in $(zpool list -H -o name); do echo $i; done`.chomp.split("\n")
end
end
def addZpoolCapacityFact(zpool)
zpool_capacity = `zpool get -H -o value capacity #{zpool}`.tr('%','').chomp
Facter.add(zpool + "_capacity") do
setcode do
zpool_capacity
end
end
end
zpools = Facter.value(:zpools)
zpools.each do |zpool|
addZpoolCapacityFact(zpool)
end
end
end
Now result looks like I´d expect:
pool1_capacity: 10
pool2_capacity: 30
In RubyMotion, I'm using AVFoundation for screencapture in an attempt to implement this gist from the Mac Developer Library. The program should capture video from the screen and write to a .mov file.
I don't quite understand why I'm getting this error:
* -[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] - no active/enabled
connections.
The simple code is:
# Setup recording pipeline
#session = AVCaptureSession.alloc.init
#session.sessionPreset = AVCaptureSessionPresetMedium
input = AVCaptureScreenInput.alloc.initWithDisplayID(KCGDirectMainDisplay)
#session.addInput(input)
movieFileOutput = AVCaptureMovieFileOutput.alloc.init
if #session.canAddOutput(movieFileOutput)
#session.addOutput(movieFileOutput)
else
Logger.error "Could not add ouput #{movieFileOutput}"
end
#session.startRunning()
# Delete exisiting file
fileManager = NSFileManager.defaultManager
path = "~/Desktop/video.mov"
if fileManager.fileExistsAtPath(path)
err = Pointer.new(:object)
unless fileManager.defaultManager.removeItemAtPath(path, error:err)
Logger.error "Can't delete existing movie"
end
end
# Start recording
movieFileOutput.startRecordingToOutputFileURL(NSURL.fileURLWithPath(path), recordingDelegate:self) # <--- Problem
What am I doing incorrect?
I used an incorrect constant for display id. This works:
input = AVCaptureScreenInput.alloc.initWithDisplayID(CGMainDisplayID())
I am trying to save compressed strings to a file and load them later for use in the game. I kept getting "in 'finish': buffer error" errors when loading the data back up for use. I came up with this:
require "zlib"
def deflate(string)
zipper = Zlib::Deflate.new
data = zipper.deflate(string, Zlib::FINISH)
end
def inflate(string)
zstream = Zlib::Inflate.new
buf = zstream.inflate(string)
zstream.finish
zstream.close
buf
end
setting = ["nothing","nada","nope"]
taggedskills = ["nothing","nada","nope","nuhuh"]
File.open('testzip.txt','wb') do |w|
w.write(deflate("hello world")+"\n")
w.write(deflate("goodbye world")+"\n")
w.write(deflate("etc")+"\n")
w.write(deflate("etc")+"\n")
w.write(deflate("Setting: name "+setting[0]+" set"+(setting[1].class == String ? "str" : "num")+" "+setting[1].to_s)+"\n")
w.write(deflate("Taggedskill: "+taggedskills[0]+" "+taggedskills[1]+" "+taggedskills[2]+" "+taggedskills[3])+"\n")
w.write(deflate("etc")+"\n")
end
File.open('testzip.txt','rb') do |file|
file.each do |line|
p inflate(line)
end
end
It was throwing errors at the "Taggedskill:" point. I don't know what it is, but trying to change it to "Skilltag:", "Skillt:", etc. continues to throw a buffer error, while things like "Setting:" or "Thing:" work fine, while changing the setting line to "Taggedskill:" continues to work fine. What is going on here?
In testzip.txt, you are storing newline separated binary blobs. However, binary blobs may contain newlines by themselves, so when you open testzip.txt and split it by line, you may end up splitting one binary blob that inflate would understand, into two binary blobs that it does not understand.
Try to run wc -l testzip.txt after you get the error. You'll see the file contains one more line, than the number of lines you are putting in.
What you need to do, is compress the whole file at once, not line by line.
I am making a crawler parsing images on the Gantz manga at http://manga.bleachexile.com/gantz-chapter-1.html and on.
I had success until my crawler tried to open a image (on chapt 273):
bad URI(is not URI?): http://static.bleachexile.com/manga/gantz/273/Gantz[0273]_p001[Whatever-Illuminati].png
BUT this url is valid I guess, because I can open from Firefox.. Any thoughts?
Partial code:
img_link = nav.page.image_urls.find {|x| x.include?("manga/gantz")}
img_name = RAILS_ROOT+"/public/#{nome}/#{cap}/"+nome+((template).sub('::cap::', cap.to_s).sub('::pag::', i.to_s))
img = File.new( img_name, 'w' )
img.write( open(img_link) {|f| f.read} )
img.close
It is not a valid uri. Only certain characters are allowed for uri's. By the way firefox like all browsers try to do as much as possible for the user instead of complaining when it does not look standard compliant.
It is valid in the following form:
open("http://static.bleachexile.com/manga/gantz/273/Gantz%5B0273%5D_p001%5BWhatever-Illuminati%5D.png") # => #<File:/tmp/open-uri20100226-3342-clj08a-0>
You could try to escape it like this:
uri.gsub(/\/.*/) do |t|
t.gsub(/[^.\/a-zA-Z0-9\-_ ]/) do |c|
"%#{ c[0]<16 ? "0" : "" }#{ c[0].to_s(16).upcase }"
end.gsub(" ", "+")
end
But be carefull, if the website uses correct escaped uri's and you escape them a second time. The uri's wont point to the same location anymore.