【问题标题】:Get size of an image from database从数据库中获取图像的大小
【发布时间】:2012-10-25 09:15:48
【问题描述】:

我有一个简单的基于标签的图像库,它将所有图像保存在数据库中。为了显示这些图像,有一个小的 php 脚本

<?php
    include_once("config/config.php");

    $mysql_user = USER;
    $password = PWD;
    $database_host = HOST;
    $database = DB;

    mysql_connect($database_host, $mysql_user, $password) or die ("Unable to connect to DB server. Error: ".mysql_error());
    mysql_select_db($database);


    header('Content-type: image/jpeg');
    $query = "SELECT imgblob from images where id=". intval($_GET["id"]);
    $rs = mysql_fetch_array(mysql_query($query));
    //echo mysql_error();
    echo base64_decode($rs["imgblob"]);
?>

搜索后有一个看起来像的图像列表

<a class="lightbox" href="showimage.php?id=1" title="" ><img src="showimage.php?id=1" /></a>

我使用灯箱来调整点击图像的大小。所以我需要获取图片源的大小(showimage.php?id=1)。

我有以下代码sn-p

 $('a.lightbox').each(function() {
var img = $(this).children("img");

var theImage = new Image();
theImage.src = img.attr("src");
$(this).fancybox({
'transitionIn' : 'elastic',
'transitionOut' : 'elastic',
'speedIn' : 600,
'speedOut' : 200,
'overlayShow' : false,
'type' : 'iframe',
'width' : theImage.width + 20,
'height' : theImage.height + 20
});
});

这些功能并非一直有效。通常调整大小的图像非常小。我没有任何想法来解决它。

这是一个示例库www.phyxius.de/projects/phx.imga/

【问题讨论】:

  • 使用 php 获取大小。图像必须在浏览器中完全加载,然后才能使用 javascript 获取尺寸。 php 可以在此之前很久就拥有它们。将尺寸添加到 data- 属性并从中提取用于您的灯箱
  • @Eritrea 如下所述,图像对象为我提供了在 css 中定义的图像的宽度和高度
  • @charlietfl 就是这样!非常感谢。我创建了一个 php 图像对象并将宽度和高度保存在输入字段中。 Jquery 获取值和 BAM 它的工作原理!

标签: php jquery database image lightbox


【解决方案1】:

需要等到图片加载完毕才能获取图片大小。

var theImage = new Image();
$(theImage).load(function () {
    $(this).fancybox({
        'transitionIn' : 'elastic',
        'transitionOut' : 'elastic',
        'speedIn' : 600,
        'speedOut' : 200,
        'overlayShow' : false,
        'type' : 'iframe',
        'width' : theImage.width + 20,
        'height' : theImage.height + 20
        });
    });
});

theImage.src = img.attr("src");

坦率地说,我完全不明白您为什么要使用不同的Image。试试这个:

$('a.lightbox').each(function() {
    var $img = $(this).children("img");

    $(this).fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 600,
        'speedOut': 200,
        'overlayShow': false,
        'type': 'iframe',
        'width': $img.width() + 20,
        'height': $img.height() + 20
    });
});​

【讨论】:

  • 嗨,我正在使用一个新的图像实例,因为灯箱的图像对象给了我在 css 中定义的小图像尺寸。所以我使用一个新对象来生成原始大小的图像
猜你喜欢
  • 2013-02-27
  • 2011-11-09
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
相关资源
最近更新 更多