Define paperclip style sizes dynamicaly - ruby-on-rails-3.1

I have an application where a user chooses a template. On this template the user can upload an image.
I use paperclip to upload the images.
Each template has different image sizes.
Is it possible to set an image style => 'widthxheight' dynamically in paperclip?
I want this functionality because if the user decides to change template then they don't have to upload the photo again they just crop the 'original'.
Thanks for any help.
I will try to clear this up.
A user uploads an image for the header of a page. The style could be called "header" and the dimensions should be the dimensions for that header space, say "400x600"
Now the user views the images they have uploaded in a gallery. They want to select one of the images for their page but this time its for the "sidebar" which has dimensions "300x100". I don't want to make them upload the same image again. I want to create a new style called "sidebar" with dimensions "300x100". I also don't want to delete the "header" style or resize it.
How can I do this with paperclip?

If I understand you have in mind something like that: Paperclip change size
Additionally:
attr_accessor :size
...
self.dimensions = self.size.split("x")
Controller:
def create
...
#file.size = params[:size] # OR Simply include such field in form
...
end
Example:
Model:
class File
has_attached_file :upload
attr_accessor :size
before_save :extract_dimensions
serialize :dimensions
...
def extract_dimensions
...
self.dimensions = self.size.split("x")
end
end
Form:
<form action="some link">
...
<label>Size: </label><select name="file_size">
<option value="100x200">Large</option>
...
</select>
</form>

Related

Copy slide with images python pptx

My end goal is to change the theme of a presentation. To do this, I have created a source template and new template (with the correct theme). I iterate over each slide in the source template then add a new slide to the new template with the contents of the source using the code below - source. If there is a better way to do this I'd love to hear it.
This works great for text and text boxes, however the test image cannot be displayed in the new powerpoint (show in the image below):
Code
def copy_slide_from_external_prs(self, src, idx, newPrs):
# specify the slide you want to copy the contents from
src_slide = src.slides[idx]
# Define the layout you want to use from your generated pptx
slide_layout = newPrs.slide_layouts[2]
# create now slide, to copy contents to
curr_slide = newPrs.slides.add_slide(slide_layout)
# remove placeholders
for p in [s.element for s in curr_slide.shapes if 'Text Placeholder' in s.name or 'Title' in s.name]:
p.getparent().remove(p)
# now copy contents from external slide, but do not copy slide properties
# e.g. slide layouts, etc., because these would produce errors, as diplicate
# entries might be generated
for shp in src_slide.shapes:
el = shp.element
newel = copy.deepcopy(el)
curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
return newPrs
I was trying many different solutions and tried creating a new Picture using the image.blob property in the source image. However, then the image does not have an element. Do I need to convert the blob to a PNG, save it, then create a new image using that saved PNG?
There must be a better way to do this. Again, I just want to change the theme.
Thanks in advance!
Here is the workaround I developed. I first check if the shape is an image, and if it is, I write the image to a local directory. Then I add a picture to the slide using that saved image. Finally, I delete the locally saved image.
Now this copy_slide function works for images:
def copy_slide_from_external_prs(src, idx, newPrs):
# specify the slide you want to copy the contents from
src_slide = src.slides[idx]
# Define the layout you want to use from your generated pptx
SLD_LAYOUT = 5
slide_layout = prs.slide_layouts[SLD_LAYOUT]
# create now slide, to copy contents to
curr_slide = newPrs.slides.add_slide(slide_layout)
# create images dict
imgDict = {}
# now copy contents from external slide, but do not copy slide properties
# e.g. slide layouts, etc., because these would produce errors, as diplicate
# entries might be generated
for shp in src_slide.shapes:
if 'Picture' in shp.name:
# save image
with open(shp.name+'.jpg', 'wb') as f:
f.write(shp.image.blob)
# add image to dict
imgDict[shp.name+'.jpg'] = [shp.left, shp.top, shp.width, shp.height]
else:
# create copy of elem
el = shp.element
newel = copy.deepcopy(el)
# add elem to shape tree
curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
# add pictures
for k, v in imgDict.items():
curr_slide.shapes.add_picture(k, v[0], v[1], v[2], v[3])
os.remove(k)
Using code from #Michael-Berk I made very elegant method for copying slides (including images). Check this answer: https://stackoverflow.com/a/73954830/20159015

Jade image gallery using extends, mixins and includes not working

I'm trying to build a Jade image gallery using mixins and includes, but can't get it working. Here's my setup:
layout-template.jade (the base layout template)
block vars
doctype html
(head and all it's meta tags)...
block body
(body content)...
block main-content
h1 This is the default layout
block footer
script(src='/js/min/app-min.js')
image-gallery-page.jade (the page with the image gallery)
extends ../../__JADE/_layout-template.jade
block vars
- var pageTitle = 'New page title'
block body
body
block main-content
(main page content...)
include ../../__JADE/_include-view-original.jade
+originalImage('http://placehold.it/800x400', 'Original Image 1')
+originalImage('http://placehold.it/800x400', 'Original Image 2')
+originalImage('http://placehold.it/800x400', 'Original Image 3')
image-gallery-include.jade (the include file being pulled into the image-gallery-page, with the mixin)
.row
.large-10.columns
.text-center Click for larger view
.owl-carousel.owl-theme.popup-gallery#owl-carousel-original-article
mixin originalImage(originalLink, originalTitle)
a(href="#{originalLink}" title="#{originalTitle}")
img(src="#{originalLink}")
The original images 1-3 from the mixin appear, but not inside the owl-carousel div that I need them to be in. If I indent the mixin under the include, they don't compile.
What am I doing wrong? Thank you!
For me personally, I would dedicate a file to my mixins or individual mixins and include them at the top of a file I wish to use them in.
Then you can simply invoke the mixin where necessary.
Currently, you have the following ;
.row
.large-10.columns
.text-center Click for larger view
.owl-carousel.owl-theme.popup-gallery#owl-carousel-original-article
mixin originalImage(originalLink, originalTitle)
a(href="#{originalLink}" title="#{originalTitle}")
img(src="#{originalLink}")
But altering to;
mixin originalImage(originalLink, originalTitle)
a(href="#{originalLink}" title="#{originalTitle}")
img(src="#{originalLink}")
.row
.large-10.columns
.text-center Click for larger view
.owl-carousel.owl-theme.popup-gallery#owl-carousel-original-article
Would make the difference.
However, if you choose to do this, you could do a little shuffling and result with something like;
extends ../../__JADE/_layout-template.jade
include ../../__JADE/_mixins.jade
block vars
- var pageTitle = 'New Page Title'
block body
body
block main-content
(main page content...)
.row
.large-10.columns
.text-center Click for larger view
.owl-carousel.owl-theme.popup-gallery#owl-carousel-original-article
+originalImage('http://placehold.it/800x400', 'Original Image 1')
+originalImage('http://placehold.it/800x400', 'Original Image 2')
+originalImage('http://placehold.it/800x400', 'Original Image 3')
Additionally, you could define a map and loop over them to invoke your mixins to save you more code.
Something like the following could potentially work;
- var images = ['http://placehold.it/800x400', 'http://placehold.it/800x400', 'http://placehold.it/800x400']
each val, index in images
+originalImage(val, 'Original Image ' + index)
That last bit might need a little tweaking as it's off the top of my head but hopefully that will help you out a little.
I try to separate my Jade files into many manageable modules and pieces so that it's simple to maintain. As your project grows maybe think about having folders for the different markup components. For example, having a folder for different mixins and being able to include specific mixins for different parts of your project.
#jh3y

TYPO3 getting image from page content

I'm working on my first TYPO3 project and I'm done with the template, I just can't figure out how to make this work:
My page content is one column with header, text and title in every field.
I don't have any problems showing header and text on my website, but the image just won't work.
My image-path is fileadmin/user_upload/ and I can show it by setting the filename in my code, but thats obviously not what I want.
This is my Content Object Array, the code for the image is one of the versions I found when searching, but none of them worked:
page.20 = CONTENT
page.20.table = tt_content
page.20.wrap = <div id="content">|</div>
page.20.renderObj = COA
page.20.renderObj.wrap = <div id="news">|</div>
page.20.renderObj {
10 = TEXT
10.stdWrap.field = header
10.stdWrap.wrap = <div id="newstitle"><span>|</span></div>
20 = IMAGE
20.stdWrap.field = image
20.stdWrap.wrap = <div id="newsimage><img src="fileadmin/user_upload/|"</img></div>
30 = TEXT
30.stdWrap.field = bodytext
30.stdWrap.wrap = <div id="newstext"><p>|</p></div>
}
Hope someone could help me out, so I can finally finish this project!
Maybe I don't understood correctly, but... you don't need to write it yourself, just in your main TS template include CSS Styled Content from extension, so you can render whole column like:
page.20 < styles.content.get
If you need to change some wrappings (i.e.: to add your news CSS classes) you can also override them in setup field.
edit: Go to Templates > Template analyzer and check the EXT:css_styled_content/static/ code to find declaration you'd like to change (prepare for looong reading ;)) when you finally find it, just you can overwrite it in your TS, ie: to wrap text section of Text w/ image CE you can use:
tt_content.textpic.20.text.wrap = <div class="myNewsClass"> | </div>
Finally I'd suggest not to replace original classes but add your own so the above sample would be like:
tt_content.textpic.20.text.wrap = <div class="csc-textpic-text myNewsClass"> | </div>
de facto all these classes csc- has some CSS default styling therefore you can choose to align the image to the left or right (etc) out of the box. If you'll remove it you'll need to handle these things yourself.

How to set an image for the panel background with seesaw?

I want to set a custom image for the panel background in my clojure app. Using seesaw I can set some color for the background:
(defn make-panel []
(border-panel
:north (flow-panel :align :center
:items [(label :text "TEXT")])
:center (canvas :class :board
:background :black)
:border 5))
but how to choose an image using its url?
Seesaw lets you use an image for frame content, via the icon function (now in seesaw.icon), like so:
(frame :title "Hola!"
; ....
:content (label :icon img_bg)
where img_bg is a File, URL, etc. However, looking at the Seesaw code, I don't see a way to put a background image directly in a panel through the Seesaw API. You may have to drop down to Java interop and use the Swing API directly. This SO question would suggest that it's possible, and may get you started.

How to remove link tag from image using Nokogiri

I'm parsing an HTML document using Nokogiri. The code contain several images like this:
<img alt="alternative-text" border="0" height="427" src="http://url_to_my_photo.jpg?" title="Image Title" width="640">
I'm trying to save that image to my S3 storage, change the style and remove the link. All the images have the css tag ".post-body img".
So far, the closest I got is this:
#doc.css(".post-body img").each do |image|
#new_photo = Photo.create!(
#Params required to save and upload the photo to S3.
...
...
)
# The url of the photo upload to S3 is #new_photo.photo.url
image['src']= #new_photo.photo.url
image['class'] = "my-picture-class"
image.parent['src] = '#'
puts image.parent.content
#doc.to_html
end
This removes the link to the big photo but obviously it isn't a good solution.
I've tried to replace the parent using image.parent << image as suggested on http://rubyforge.org/pipermail/nokogiri-talk/2009-June/000333.html but doesn't do anything and image.parent = image returns "Could not reparent node (RuntimeError)"
To convert that mailing list example over to apply to your situation, you have to remember that node is the node they are trying to get rid of, which in your case is image.parent.
So instead of image.parent['src] = '#' you should try:
link = image.parent
link.parent << image
link.remove
Edit:
Actually, the above code would probably move all the images to the bottom of whatever element contains the link, so try this instead:
link = image.parent
link.replace(image)

Resources