php - 转换 BBCode 文本时检查图像大小

标签 php jquery html css image

在我使用 ckEditor 作为 BBCode 编辑器的任务中,我的网站直接遇到了问题。

我有用户提交的内容,可以包含 [img] 标签。

我真的希望图像的宽度为 100%,这很好。我将任何 [img] 标签转换为 html 并应用一个按照我的意愿设计样式的类。

function basicbbcode($text) {
$text = str_replace("[IMG]", "<img class='buildimage' src='", "$text");
$text = str_replace("[/IMG]", "'>", "$text");
$text = str_replace("[img]", "<img class='buildimage' src='", "$text");
$text = str_replace("[/img]", "'>", "$text");
}

问题是当有人使用小/低分辨率图像时。我不想拉伸(stretch)这个图像,因为它看起来很可怕。

我的目标是找到一个阈值,因此如果图像宽度超过 800px,则将其指定为 buildimage 类。如果它较小,则给它一个 buildimage-small 类。

然后我会保持该图像的自然大小。我正在努力促进用户提供高质量的图像,但同时希望保持网站看起来很棒,并且不会因为分辨率很小而出现拉伸(stretch)不良的图像。

那么,有没有办法在图像大小作为 $text 变量从数据库中检查出来时检查图像大小,然后采取相应的操作。最好使用 php,但也可以使用 jquery?

对此一无所知,可能吗?

最佳答案

您可以使用getimagesize():

The getimagesize() function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondant HTTP content type.

<?php
    list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
?>

来源:https://www.php.net/getimagesize

关于php - 转换 BBCode 文本时检查图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20893850/

相关文章:

javascript - Symfony 3 - CSRF token 仅在使用 AJAX 提交时无效

Android 浏览器拉伸(stretch)图像

javascript - 历史记录恢复后,Knockout 绑定(bind)将解除绑定(bind)

javascript - 使用 jquery 更改输入的 id

javascript - 如何使用 Bootstrap Tab Collapse 插件在标签内获取 h4 标签?

javascript - document.getElementById 对内部 html 的调用适用于 $_POST 和 $_GET 变量,但不适用于普通的 php 变量

php - 如何在不同的表行中获取提交按钮来执行不同的SQL查询?

php - 将图像上传到数据库和文件夹中

php - 将 TIMESTAMP SQL 转换为 12 小时时间

在队列中延迟的 Javascript 事件执行