1

i have a simple tag based image gallery which saves all images in a database. To show these images there is a small php script

<?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"]);
?>

After a search there is a list of images that looks like

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

I use lightbox to resize the image on click. So i need to get the size of the image source (showimage.php?id=1).

I have the following code snippet

 $('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
});
});

These function doesn't work all time. Often the resize image is very small. I don't have any idea to fix it.

Here is a sample gallery www.phyxius.de/projects/phx.imga/

Marc
  • 16,170
  • 20
  • 76
  • 119
Sentencio
  • 230
  • 1
  • 13
  • Possibly asked here: http://stackoverflow.com/questions/5240532/get-image-size-with-jquery – samayo Nov 05 '12 at 14:07
  • 2
    use php to get the size. Image has to be fully loaded in browser before dimensions can be obtained with javascript. php can have them long before that. Add the dimensions to a `data-` attribute and extract from that for your lightbox – charlietfl Nov 05 '12 at 14:12
  • @Eritrea as explained below the image object gives me the width and height of the image which are defined in css – Sentencio Nov 05 '12 at 14:31
  • @charlietfl that's it! Thank you very much. I created an php image object und save the width and height in an input field. Jquery get the values and BAM it works !! – Sentencio Nov 05 '12 at 15:12

1 Answers1

1

You need to wait until the image is loaded to get the image size.

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");

Though frankly, I don't understand why you're using a different Image at all. Try this instead:

$('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
    });
});​
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Hi, i'm using a new image instance because the image object of lightbox gives me the small image size defined in css. So i use a new object to generate an image in original size – Sentencio Nov 05 '12 at 14:22