Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Round Images Without Soft Masks? #1028

Closed
emmx opened this issue Jun 6, 2017 · 4 comments
Closed

How to Round Images Without Soft Masks? #1028

emmx opened this issue Jun 6, 2017 · 4 comments

Comments

@emmx
Copy link

emmx commented Jun 6, 2017

I'm trying to create a pdf document in which on each page there are some bounding boxes containing images that must fit either by width or height, and have their corners rounded.

That sounds simple, but I'm having a problem. The images I'm adding should be aligned at the right of the bounding boxes, and I'm using the "fit" parameter so the image is scaled accordingly (the problem is I don't know the size or aspect ratio of the image beforehand).

I know I can round an image using a soft mask with a filled rectangle (#000000) plus a filled rounded rectangle (#FFFFFF) on top of it, that should do the trick. However, since I don't know the image.scaled_width and image.scaled_height until I put it on the bounding_box, I can't create the mask before adding the image, and it seems the image is not affected by the soft if it's added later on.

require "prawn"
require "prawn/measurement_extensions"

file = "example.png"

Prawn::Document.generate "example.pdf", page_size: "A4", page_layout: :portrait do
  bounding_box [0, cursor], width: 200, height: 75 do
    stroke_bounds

    padding = 2.mm
    bounding_box [padding, cursor-padding], width: bounds.width-2*padding, height: bounds.height-2*padding do

      save_graphics_state do
        img = image file, fit: [bounds.width, bounds.height], position: :right, vposition: :center

        # This soft mask doesn't affect the previous image
        soft_mask do
          fill_color "000000"
          fill_rectangle [0, cursor], img.scaled_width, img.scaled_height
          fill_color "FFFFFF"
          fill_rounded_rectangle [0, cursor], img.scaled_width, img.scaled_height, 5.mm
        end
      end
    end
  end
end

How do I round the corners of an image in this scenario?

@emmx emmx changed the title Grid Bug and Rounded Image Problem How to Round Images Without Soft Masks? Jun 6, 2017
@emmx
Copy link
Author

emmx commented Jun 11, 2017

Anyone?

@pointlessone
Copy link
Member

I'm afraid, you have to calculate mask size yourself. You'd need to find out image dimensions beforehand using some other library and calculate scaled size also you'd need to position the mask properly to align with the image.

@ruinunes
Copy link

Here's a quick and rough code example that accomplishes an image crop in PrawnPdf:

bounding_box([bounds.width * 0.5, cursor], width: 200, height: 113) do
  image_width = 200
  image_height = 113
  crop_size = 20
  save_graphics_state do
    soft_mask do
      fill_color 0, 0, 0, 0
      fill_rectangle [crop_size, image_height - crop_size], image_width - crop_size * 2, image_height - crop_size * 2
     end
     image "#{IMAGES_PATH}/cat.jpg", at: [bounds.left, bounds.top], width: image_width, height: image_height
  end
  stroke_bounds
end

@pointlessone
Copy link
Member

After rereading the original issue here are some pointers:

  • soft_mark applies to the following content. It doesn't affect previous image in the graphic state. See @ruinunes code.
  • You'll have to calculate the size of your mask rounded rectangle yourself. You have all the needed information (size of the bounding box, and image size). You've got the right idea to extract image aspect ration but you'll have to do it without embedding the image first. You can use either external tools like ImageMagick or you can risk using Prawns' private method build_image_object(file) to get the image aspect ratio. The rest of the math is not too hard.

I've added a small clarification to documentation where the mask applies. Otherwise it seem that it works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants