从 PHP 获取 CSS 属性

发布于 2024-12-13 19:26:51 字数 978 浏览 3 评论 0原文

这是我第一次在这里提问(我曾多次访问过该网站,但从未询问过)。好吧,让我们讨论这个问题:

碰巧我正在开发一个实时图像缩放器(我知道它已经存在,但我正在为自己的项目做自己的事情)。它有三个参数:图像路径(显然)、我想要调整大小的大小以及我想要添加的额外边距。这个想法是调整方形尺寸的盒子内的图像大小。问题是,我要调整的尺寸取决于外箱的尺寸。例如,我有这个 HTML 代码:

<div class="image_outer_box">
   <img width="300px" height="199px" style=" margin: 65.5px 15px;" src="img.jpg">
</div>

“image_outer_box”的属性如下:

.image_outer_box
{
   height: 330px;
   width: 330px;
   border:solid 1px #737373;
}

我调用的函数是这样的:

liveResize($img, $size, $extramargin);

这里的问题是当我发送 $size 时,因为我必须知道“image_outer_box”的 CSS 属性以便进行正确的调整。而且,正如您所看到的,这不仅与“image_outer_box”中指定的大小有关,而且与我要添加的边距有关。实际上,我想为图像添加 15px 的额外边距,并将图像大小调整到 300x300 以内。

是否有办法从这个特定类获取 CSS 属性并不重要,但如果我(和其他开发人员)可以在不知道将放置图像的容器的 CSS 属性的情况下应用该函数,那将会有所帮助。

如果有办法实现的话,我会很棒。

谢谢! (其他时候我来这里搜索解决方案,我找到了它!!!)

编辑:我找到了一种不需要获取 CSS 属性的方法,但我仍然想获得有关此事的信息。感谢那些到目前为止一直试图提供帮助的人!

It's my first time asking here (I have visited the site several times, but never asked). Well, let's go to the question:

It happens that I'm developing a live image resizer (I know it already exists, but I'm doing my own for my own projects). It has three parameters: the image path (obviously), the size I want to resize and the extra margin I want to add. The idea is to resize the image inside a box with square dimentions. The problem is, the size I want to resize depends on the size of the outer box. For example, I have this HTML code:

<div class="image_outer_box">
   <img width="300px" height="199px" style=" margin: 65.5px 15px;" src="img.jpg">
</div>

The properties of "image_outer_box" are the following:

.image_outer_box
{
   height: 330px;
   width: 330px;
   border:solid 1px #737373;
}

The function I call is this:

liveResize($img, $size, $extramargin);

The problem here is when I send the $size, since I must be aware of the CSS properties of "image_outer_box" in order to make the right resize. And, as you can see, it's not only a matter of the size specified in "image_outer_box" but the margin I want to add. Actually, I wanted to add 15px of extra margin for the image and resize the image inside 300x300.

It's not critical to have a way to get the CSS attributes from this particular class, but it would help if I (and other developers) can apply the function without being aware of the CSS attributes of the container where the image will be placed.

If there's a way to make it, I would be great.

Thanks! (for the other times I came here to search a solution and I found it!!!)

EDIT: I found a way that doesn't require to get the CSS properties, but still I would like to have info for this matter. Thanks for those who tried to help 'til now!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-12-20 19:26:51

您想实际存储调整大小的图像,还是只是想显示调整大小的图像?如果是后者,以下解决方案可能会有所帮助:

HTML

<div class="image_outer_box" style="background:url('img.jpg') no-repeat; background-position:50% 50%; background-size:100%;"> 
</div>

CSS

.image_outer_box
  {
  margin:10px;
  height: 330px;
  width: 330px;
  border:solid 1px #737373;
  }

当然,这不允许您手动设置图像的宽度和高度,因为这完全取决于我们的 CSS。改变CSS,图片的大小也会自动改变。我非常喜欢这种方法,但如果您想存储图像或希望能够手动设置图像大小,它不会走得太远。

Do you want to actually store the resized image, or are you just trying to display it resized? If the latter is the case, the following solution might help:

HTML

<div class="image_outer_box" style="background:url('img.jpg') no-repeat; background-position:50% 50%; background-size:100%;"> 
</div>

CSS

.image_outer_box
  {
  margin:10px;
  height: 330px;
  width: 330px;
  border:solid 1px #737373;
  }

Of course, this doesn't allow you to manually set the width and height of the image, since that depends on our CSS entirely. Change the CSS, and the size of the image will automatically change as well. I like this method quite a bit, but it won't get very far if you want to store the image or if you want to be able to set the image size manually.

吖咩 2024-12-20 19:26:51

我认为为了了解 CSS 并根据图像的当前客户端状态进行更改,您应该(我的意思是我建议)使用 Javascript,也许如果您想保留该函数,则可以通过 AJAX 调用它。

I think in order to be aware of the CSS and change according to the current client side state of the image you should (I mean I would recommend) use Javascript and maybe if you want to keep the function call it over AJAX.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文