Rails3 - Problem saving base64 image with paperclip? - image

This is similar to the problem posted here, but that solution doesn't work for me. Maybe it's because I'm not passing the data in correctly.
I'm pulling screenshots from Flash and displaying them on the page using Jquery:
$SNAPSHOT_PREVIEW.attr("src","data:image/jpg;base64," + imgData);
$HIDDEN_BASE64_STRING.val(imgData);
I had it nice and working where you could save the image to Rails in Flash, but Flash won't allow you invoke a post action without the user pressing a button for security reasons. Makes sense. Anyway, now I can't get Paperclip to save the image coming from the HTML form:
#(photo has_attached_file:image)
#photo = params[:photo]
data = StringIO.new(Base64.decode64(params[:base64_string]))
data.class.class_eval { attr_accessor :original_filename, :content_type }
data.original_filename = "screenshots.jpg"
data.content_type = "image/jpg"
#photo.image = data
Yields the error:
NoMethodError (undefined method `image=' for #<ActiveSupport::HashWithIndifferentAccess:0x8c2e420>):
How do I need to finesse the base64 image data into a paperclip attachment?
For bonus points, do I need the hidden field to pass the data or is there a clever, browser compatible way to use the image src as a form value?

You have the code:
#photo = params[:photo]
params is just a hash, so later, when you call #photo.image, Rails bugs out. Perhaps you want:
#photo = Photo.new(params[:photo])
instead?

Related

Can't save user profiler image

I'm working on Ruby script with Twitter API. I try to save profile image fetched but couldn't.
#image = subject.user.profile_image_url
=> #<Addressable::URI:xxxxxxxx URI:http://pbs.twimg.com/profile_images/xxxxxxxxxx/m_xxxxxxxxxxx.jpg>
I want to save only image URI. It's not concise.
How can I remove #<Addressable::URI:xxxxxxxx >?
You have to use path method:
#image = subject.user.profile_image_url.path
Check more in documenation

Ruby Watir-webdriver saving image when navigating directly to the image

I'm trying to grab a set of information from a series of pages that are loaded via JS and to accomplish that I'm using watir-webdriver to load the page and nokogiri to parse them. This is working great, however, I need to grab a picture off of the page. The path of the picture is generated upon the page's loading so I wrote the following to create an array of relative URLS to the images and navigate directly to the absolute URL of the first index of the array, which is always the image I want.
img_srcs = $page_html.css('img').map{ |i| i['src'] } #genereates an array of relative urls pointing to every image
imageURL= "website.com" + img_srcs[1].gsub("..","").to_s #take the relative URL of image at index position 1 (the image) and converts it to an absolute URL
$browser.goto(imageURL)
How can I save this image which the browser has directly loaded? Any help would be appreciated and please let me know if I anything is unclear.
Edit:
I've now added the following code
image_source = $browser.image(:class => "decoded").image.src
File.open("#{$imageID}.txt", "w") do |f|
f.write open(image_source).read
f.close
end
However, I'm getting the error
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.4/lib/watir-webdriver/el
ements/element.rb:490:in 'assert_exists': unable to locate element, using {:tag_
name=>"img"} (Watir::Exception::UnknownObjectException)
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.4/lib/watir
-webdriver/attribute_helper.rb:71:in 'block in define_string_attribute'
from 12.rb:121:in 'imageDownload'
from 12.rb:134:in 'navAndGrab'
from 12.rb:137:in '<main>'
When you do:
$browser.image(:class => "decoded").image.src
You are looking for the html:
<img class="decoded">
<img src="what_you_want"></img>
</img>
I am guessing your html is not like that, hence you get the exception regarding finding the image within the image.
You probably just want the first image with class decoded (remove the second .image):
image_source = $browser.image(:class => "decoded").src
Or maybe you want the full list of images and then get the first one:
image_source = $browser.images(:class => "decoded").first.src

How to pass AJAX POST data to jQuery nyroModal?

Ealier I tried using same functionality with colobox but could not success, now I'm trying to use nyroModal and trying to send ajax data using post method with iframe. But its not going through. Here is the code which I have tried:
var src = "this is test data';
$.nmManual('try_python.php',{
callbacks: {
initFilters: function(nm) {
nm.filters.push('link');
nm.filters.push('iframe');
},
ajax:{data:'code='+src, type:"post"}
}
});
I'm able to open iframe and content from try_python.php is properly displayed in the frame but passed data is not accessible to try_python.php.
I will really appreciate you for any input in this regard.
Kind Regards
Mohtashim
I could not success in posting my content using POST method but I found a workaround where did the following:
(a) Saved my content in a hidden <div id="source" style="display:none;">...</div>
(b) Access this content from the iframe window upon successful load using the following:
var code = window.parent.$("#source");
and this is what I wanted to have....May be it will help many others. If you find a solution to pass direct content using post method kindly share.

How can I resize external images and serve them on-the-fly?

I have a sinatra app that gets image urls from an API and I want to scale them and then serve them without storing them on the server. Most of the gems I have seen only get local images and then processes each one in a queue. I only need to scale five images and display them on the page. Is there a fast way to do this?
More Clarification:
I need a way to get an image externally (e.g. notmysite.com/img.jpg) and for the code to serve the scaled image on the page. I cant do it with css or other front-end methods because this page is going to be rendered by a script that distorts images scaled front-end.
Dragonfly uses imagemagick to scale the images. Here's some code I've cobbled together from previous stuff I've done with MiniMagick, so it'll be fairly similar.
Get yourself the file into a Tempfile. I've done this here with Faraday and Typheous. Then scale it using magick!
require 'faraday'
require 'faraday_middleware'
#require 'faraday/adapter/typhoeus' # see https://github.com/typhoeus/typhoeus/issues/226#issuecomment-9919517 if you get a problem with the requiring
require 'typhoeus/adapters/faraday'
configure do
Faraday.default_connection = Faraday::Connection.new(
:headers => { :accept => 'image/*',
:user_agent => "Sinatra via Faraday"}
) do |conn|
conn.use Faraday::Adapter::Typhoeus
end
end
helpers do
def grab_image_and_scale
response = Faraday.get url # you'll need to supply this variable somehow, your choice
filename = "SOMETHING.jpg"
tempfile = Tempfile.open(filename, 'wb') { |fp| fp.write(response.body) }
thumb = MiniMagick::Image.open( tempfile.path )
thumb.thumbnail( "75x75" )
thumb.write( File.join settings.public, "images", "thumb_#{filename}")
scaled = MiniMagick::Image.open( secure_path )
scaled.resize( "600" )
scaled.write( File.join settings.public, "images", "scaled_#{filename}")
end
end
I'll leave it to you to work out how to change the path to the public images folder into a tempfile (and it'd be nice if you shared how it's done:)
One way, that is not related to Ruby or sinatra, is to add width and height attributes to img HTML tag. You'll end up with something like this:
<img src="img.source.from.API.JPG" width="2000px" height="100px" />
Edit
Another method is to change the dimension using javascript, as suggested here: https://stackoverflow.com/a/11333825/693597. You might consider writing your JS file and included in the header of your HTML.

image using .ashx

i am using .ashx to retrive image and i place the the image inside the ajax update panel it retrive the image when a new image is added to the form but when we change the image it is not updating the image it dont even call the .ashx file but when i refresh the browser it works properly
Sounds like a caching issue. Try adding some of the lines found here to your ashx file and it should hopefully force the browser to rerequest the image. (I know that the link is for ASP rather than ASP.NET, but things like Response.Expires = -1 should work)
Alternatively, can you change the path to the image in the updatepanel? If you just add a random parameter on to the end of it the browser will treat it as a fresh request (we use the current date/time as a parameter when we're doing this. The parameter is ignored by ASP.NET unless you explicitly reference it)
Do something like this:
var sPath = "../../handlers/ProcessSignature.ashx?type=View&UserID=" + userID + "&d=" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
That puts a 4 character alpha numeric string at the end of your query string. It's not needed, but it will force browsers to pick up the latest version of that image because the URL is different.
I tried the above and some browsers ignore the headers. I threw all of those in and Chrome/FireFox 3 didn't try to update.
IE7 worked sometimes
IE6 just twiddled it's thumbs and asked why it was still in existence.
Changing the path above will fix it in all browsers.

Resources