code_junkie

Get real image width and height with Javascript in Safari/Chrome?

Nov 12th, 2011
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. var pic = $("img")
  2.  
  3. // need to remove these in of case img-element has set width and height
  4. pic.removeAttr("width");
  5. pic.removeAttr("height");
  6.  
  7. var pic_real_width = pic.width();
  8. var pic_real_height = pic.height();
  9.  
  10. var img = $("img")[0]; // Get my img elem
  11. var pic_real_width, pic_real_height;
  12. $("<img/>") // Make in memory copy of image to avoid css issues
  13. .attr("src", $(img).attr("src"))
  14. .load(function() {
  15. pic_real_width = this.width; // Note: $(this).width() will not
  16. pic_real_height = this.height; // work for in memory images.
  17. });
  18.  
  19. function getOriginalWidthOfImg(img_element) {
  20. var t = new Image();
  21. t.src = (img_element.getAttribute ? img_element.getAttribute("src") : false) || img_element.src;
  22. return t.width;
  23. }
  24.  
  25. jQuery(document).ready(function(){
  26. if (jQuery.browser.safari && document.readyState != "complete"){
  27. //console.info('ready...');
  28. setTimeout( arguments.callee, 100 );
  29. return;
  30. }
  31. ... (rest of function)
  32.  
  33. $("img").one("load", function(){
  34. var img = this;
  35. setTimeout(function(){
  36. // do something based on img.width and/or img.height
  37. }, 0);
  38. });
  39.  
  40. $(window).load(function() {
  41. var pic = $('img');
  42.  
  43. pic.removeAttr("width");
  44. pic.removeAttr("height");
  45.  
  46. alert( pic.width() );
  47. alert( pic.height() );
  48. });
  49.  
  50. (function( $ ){
  51. $.fn.getDimensions=function(){
  52. alert("First example:This works only for HTML code without CSS width/height definition.");
  53. w=$(this, 'img')[0].width;
  54. h=$(this, 'img')[0].height;
  55.  
  56. alert("This is a width/height on your monitor: " + $(this, 'img')[0].width+"/"+$(this, 'img')[0].height);
  57.  
  58. //This is bad practice - it shows on your monitor
  59. $(this, 'img')[0].removeAttribute( "width" );
  60. $(this, 'img')[0].removeAttribute( "height" );
  61. alert("This is a bad effect of view after attributes removing, but we get right dimensions: "+ $(this, 'img')[0].width+"/"+$(this, 'img')[0].height);
  62. //I'am going to repare it
  63. $(this, 'img')[0].width=w;
  64. $(this, 'img')[0].height=h;
  65. //This is a good practice - it doesn't show on your monitor
  66. ku=$(this, 'img').clone(); //We will work with a clone
  67. ku.attr( "id","mnbv1lk87jhy0utrd" );//Markup clone for a final removing
  68. ku[0].removeAttribute( "width" );
  69. ku[0].removeAttribute( "height" );
  70. //Now we still get 0
  71. alert("There are still 0 before a clone appending to document: "+ $(ku)[0].width+"/"+$(ku)[0].height);
  72. //Hide a clone
  73. ku.css({"visibility" : "hidden",'position':'absolute','left':'-9999px'});
  74. //A clone appending
  75. $(document.body).append (ku[0]);
  76. alert("We get right dimensions: "+ $(ku)[0].width+"/"+$(ku)[0].height);
  77. //Remove a clone
  78. $("#mnbv1lk87jhy0utrd").remove();
  79.  
  80. //But a next resolution is the best of all. It works in case of CSS definition of dimensions as well.
  81. 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.");
  82. imgcopy=$('<img src="'+ $(this, 'img').attr('src') +'" />');//new object
  83. imgcopy.attr( "id","mnbv1lk87jhy0aaa" );//Markup for a final removing
  84. imgcopy.css({"visibility" : "hidden",'position':'absolute','left':'-9999px'});//hide copy
  85. $(document.body).append (imgcopy);//append to document
  86. alert("We get right dimensions: "+ imgcopy.width()+"/"+imgcopy.height());
  87. $("#mnbv1lk87jhy0aaa").remove();
  88.  
  89.  
  90. }
  91. })( jQuery );
  92.  
  93. $(document).ready(function(){
  94.  
  95. $("img.toreaddimensions").click(function(){$(this).getDimensions();});
  96. });
  97.  
  98. $(this).clone().removeAttr("width").attr("width");
  99. $(this).clone().removeAttr("height").attr("height);
  100.  
  101. $("#myImg").one("load",function(){
  102. //do something, like getting image width/height
  103. }).each(function(){
  104. if(this.complete) $(this).trigger("load");
  105. });
  106.  
  107. graph= $('<img/>', {"src":'mySRC', id:'graph-img'});
  108. graph.bind('load', function (){
  109. wid = graph.attr('width');
  110. hei = graph.attr('height');
  111.  
  112. graph.dialog({ autoOpen: false, title: 'MyGraphTitle', height:hei, width:wid })
  113. })
  114.  
  115. $(window).load(function(){
  116.  
  117. //these all work
  118.  
  119. $('img#someId').css('width');
  120. $('img#someId').width();
  121. $('img#someId').get(0).style.width;
  122. $('img#someId').get(0).width;
  123.  
  124. });
  125.  
  126. var pic_real_width = 0,
  127. img_src_no_cache = $('img#someId').attr('src') + '?cache=' + Date.now();
  128.  
  129. $('<img/>').attr('src', img_src_no_cache).load(function(){
  130.  
  131. pic_real_width = this.width;
  132.  
  133. });
  134.  
  135. //make json call to server to get image size
  136. $.getJSON("http://server/getimagesize.php",
  137. {"src":url},
  138. SetImageWidth
  139. );
  140.  
  141. //callback function
  142. function SetImageWidth(data) {
  143.  
  144. var wrap = $("div#image_gallery #image_wrap");
  145.  
  146. //remove height
  147. wrap.find("img").removeAttr('height');
  148. //remove height
  149. wrap.find("img").removeAttr('width');
  150.  
  151. //set image width
  152. if (data.width > 635) {
  153. wrap.find("img").width(635);
  154. }
  155. else {
  156. wrap.find("img").width(data.width);
  157. }
  158. }
  159.  
  160. <?php
  161.  
  162. $image_width = 0;
  163. $image_height = 0;
  164.  
  165. if (isset ($_REQUEST['src']) && is_file($_SERVER['DOCUMENT_ROOT'] . $_REQUEST['src'])) {
  166.  
  167. $imageinfo = getimagesize($_SERVER['DOCUMENT_ROOT'].$_REQUEST['src']);
  168. if ($imageinfo) {
  169. $image_width= $imageinfo[0];
  170. $image_height= $imageinfo[1];
  171. }
  172. }
  173.  
  174. $arr = array ('width'=>$image_width,'height'=>$image_height);
  175.  
  176. echo json_encode($arr);
  177.  
  178. ?>
Add Comment
Please, Sign In to add comment