Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var pic = $("img")
- // need to remove these in of case img-element has set width and height
- pic.removeAttr("width");
- pic.removeAttr("height");
- var pic_real_width = pic.width();
- var pic_real_height = pic.height();
- var img = $("img")[0]; // Get my img elem
- var pic_real_width, pic_real_height;
- $("<img/>") // Make in memory copy of image to avoid css issues
- .attr("src", $(img).attr("src"))
- .load(function() {
- pic_real_width = this.width; // Note: $(this).width() will not
- pic_real_height = this.height; // work for in memory images.
- });
- function getOriginalWidthOfImg(img_element) {
- var t = new Image();
- t.src = (img_element.getAttribute ? img_element.getAttribute("src") : false) || img_element.src;
- return t.width;
- }
- jQuery(document).ready(function(){
- if (jQuery.browser.safari && document.readyState != "complete"){
- //console.info('ready...');
- setTimeout( arguments.callee, 100 );
- return;
- }
- ... (rest of function)
- $("img").one("load", function(){
- var img = this;
- setTimeout(function(){
- // do something based on img.width and/or img.height
- }, 0);
- });
- $(window).load(function() {
- var pic = $('img');
- pic.removeAttr("width");
- pic.removeAttr("height");
- alert( pic.width() );
- alert( pic.height() );
- });
- (function( $ ){
- $.fn.getDimensions=function(){
- alert("First example:This works only for HTML code without CSS width/height definition.");
- w=$(this, 'img')[0].width;
- h=$(this, 'img')[0].height;
- alert("This is a width/height on your monitor: " + $(this, 'img')[0].width+"/"+$(this, 'img')[0].height);
- //This is bad practice - it shows on your monitor
- $(this, 'img')[0].removeAttribute( "width" );
- $(this, 'img')[0].removeAttribute( "height" );
- alert("This is a bad effect of view after attributes removing, but we get right dimensions: "+ $(this, 'img')[0].width+"/"+$(this, 'img')[0].height);
- //I'am going to repare it
- $(this, 'img')[0].width=w;
- $(this, 'img')[0].height=h;
- //This is a good practice - it doesn't show on your monitor
- ku=$(this, 'img').clone(); //We will work with a clone
- ku.attr( "id","mnbv1lk87jhy0utrd" );//Markup clone for a final removing
- ku[0].removeAttribute( "width" );
- ku[0].removeAttribute( "height" );
- //Now we still get 0
- alert("There are still 0 before a clone appending to document: "+ $(ku)[0].width+"/"+$(ku)[0].height);
- //Hide a clone
- ku.css({"visibility" : "hidden",'position':'absolute','left':'-9999px'});
- //A clone appending
- $(document.body).append (ku[0]);
- alert("We get right dimensions: "+ $(ku)[0].width+"/"+$(ku)[0].height);
- //Remove a clone
- $("#mnbv1lk87jhy0utrd").remove();
- //But a next resolution is the best of all. It works in case of CSS definition of dimensions as well.
- alert("But if you want to read real dimensions for image with CSS class definition outside of img element, you can't do it with a clone of image. Clone method is working with CSS dimensions, a clone has dimensions as well as in CSS class. That's why you have to work with a new img element.");
- imgcopy=$('<img src="'+ $(this, 'img').attr('src') +'" />');//new object
- imgcopy.attr( "id","mnbv1lk87jhy0aaa" );//Markup for a final removing
- imgcopy.css({"visibility" : "hidden",'position':'absolute','left':'-9999px'});//hide copy
- $(document.body).append (imgcopy);//append to document
- alert("We get right dimensions: "+ imgcopy.width()+"/"+imgcopy.height());
- $("#mnbv1lk87jhy0aaa").remove();
- }
- })( jQuery );
- $(document).ready(function(){
- $("img.toreaddimensions").click(function(){$(this).getDimensions();});
- });
- $(this).clone().removeAttr("width").attr("width");
- $(this).clone().removeAttr("height").attr("height);
- $("#myImg").one("load",function(){
- //do something, like getting image width/height
- }).each(function(){
- if(this.complete) $(this).trigger("load");
- });
- graph= $('<img/>', {"src":'mySRC', id:'graph-img'});
- graph.bind('load', function (){
- wid = graph.attr('width');
- hei = graph.attr('height');
- graph.dialog({ autoOpen: false, title: 'MyGraphTitle', height:hei, width:wid })
- })
- $(window).load(function(){
- //these all work
- $('img#someId').css('width');
- $('img#someId').width();
- $('img#someId').get(0).style.width;
- $('img#someId').get(0).width;
- });
- var pic_real_width = 0,
- img_src_no_cache = $('img#someId').attr('src') + '?cache=' + Date.now();
- $('<img/>').attr('src', img_src_no_cache).load(function(){
- pic_real_width = this.width;
- });
- //make json call to server to get image size
- $.getJSON("http://server/getimagesize.php",
- {"src":url},
- SetImageWidth
- );
- //callback function
- function SetImageWidth(data) {
- var wrap = $("div#image_gallery #image_wrap");
- //remove height
- wrap.find("img").removeAttr('height');
- //remove height
- wrap.find("img").removeAttr('width');
- //set image width
- if (data.width > 635) {
- wrap.find("img").width(635);
- }
- else {
- wrap.find("img").width(data.width);
- }
- }
- <?php
- $image_width = 0;
- $image_height = 0;
- if (isset ($_REQUEST['src']) && is_file($_SERVER['DOCUMENT_ROOT'] . $_REQUEST['src'])) {
- $imageinfo = getimagesize($_SERVER['DOCUMENT_ROOT'].$_REQUEST['src']);
- if ($imageinfo) {
- $image_width= $imageinfo[0];
- $image_height= $imageinfo[1];
- }
- }
- $arr = array ('width'=>$image_width,'height'=>$image_height);
- echo json_encode($arr);
- ?>
Add Comment
Please, Sign In to add comment