Agamnentzar / ag-psd

Javascript library for reading and writing PSD files
Other
489 stars 66 forks source link

the problem of inconsistent layer sizes #179

Closed zhanghang318 closed 3 months ago

zhanghang318 commented 3 months ago

Hey, I have a question, When I uploaded a PSD file on the Photosea website, I found that the size of many layers did not match the size resolved by this library, and they were always within 5 pixels. How can I maintain consistency

Agamnentzar commented 3 months ago

Can you upload an example here of a file that does this? How do you check layer size in Photopea? If the Photopea only tells you the visible size of the layer that might be the problem here. The layers can extend beyond canvas area, for example if the layer extends to the left beyond canvas bounds it will have negative layer.left property.

zhanghang318 commented 3 months ago

Thank you for your prompt reply. Here is a file called mask, which contains a layer called email. It has a size of 46 46 on Photosea, and the library parses a size of 49 49. My calculation process is as follows: width = layer.right - layer.left
height = layer.bottom - layer.top

image

mask.psd.zip

Agamnentzar commented 3 months ago

I think in this case Photopea might be telling you precisely measured size of the vector mask, that's why the values are not whole numbers. layer.right - leyer.left gives you size in pixels of rendered layer bitmap, it will be rounded to whole pixels and will also have extra pixels to accommodate antialiasing.

zhanghang318 commented 3 months ago

Okay, I have been trying to smooth out these differences before because some of the layers are quite special, with effects such as blurring, shadows, rounded corners, or borders. At one point, I thought it was these factors that affected the size, but eventually found that I couldn't find the pattern. There is another thing that affects me, which is that the Photosea website can effectively parse the size of layers and ultimately export usable CSS properties. I am curious about how it does this

image
Agamnentzar commented 3 months ago

It seems that the layer bitmap has bunch of transparent pixels around it, you could get to 46px by trimming transparent pixels on each side of the bitmap image, I don't know why Photoshop didn't trim it itself in this situation.

image

zhanghang318 commented 3 months ago

Thank you very much. Actually, as long as I prove that the size difference is not affected by certain effects inside the layer, I don't need to verify the patterns within the layer