-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added height, width, and hide_fields parameters to template preview. C…
…loses #52, Closes #53. * Introducde an ImageUtility module to resize images system-wide. Scales to fit height and width if needed or stretches the image to match exact dimensions. * Wired up Template#display to use the image utility. * Wired up Template#preview to use the image utility, taking height and width as params. Setting the hide_fields param to true (&hide_fields=true) will not render the fields on a preview. * Made the font size selection smarter when rendering templates.
- Loading branch information
Showing
6 changed files
with
85 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Common image utilities. | ||
module ImageUtility | ||
|
||
# Resize an image to a height and width. | ||
# If maintain_aspect_ratio (default true) is set the constraining value | ||
# is used when resizing the image (i.e. the largest side will match the smallest dimension) | ||
# otherwise the image will be resized to match the width and height. | ||
# Returns an image. | ||
def self.resize(image, width, height, maintain_aspect_ratio=true) | ||
unless width.nil? && height.nil? | ||
if maintain_aspect_ratio && (!width.nil? && !height.nil?) | ||
desired_ratio = width.to_f / height | ||
image_ratio = image.columns.to_f / image.rows | ||
Rails.logger.debug(desired_ratio) | ||
Rails.logger.debug(image_ratio) | ||
Rails.logger.debug("Done") | ||
if image_ratio > desired_ratio | ||
height = nil | ||
else | ||
width = nil | ||
end | ||
end | ||
if width.nil? | ||
width = height * image.columns.to_f / image.rows | ||
end | ||
if height.nil? | ||
height = width * image.rows.to_f / image.columns | ||
end | ||
if image.columns != width && image.rows != height | ||
image = image.scale(width, height) | ||
end | ||
end | ||
return image | ||
end | ||
end |