0

So - I have this website (Pickture) which allows users to upload a photo of virtually any size. This causes a headache when it comes to displaying them.

Currently this happens. The Image is inside a container DIV (which I would prefer to be untouched), and is set to a width of 100% and the height is left blank. This means that all images, even if only 200x200, will be scaled to be around 600x600.

What I would like to happen is this. If the width of the image is greater than 650px, set the width to 100%. If the width is less than that, set the width of the image to be the width of the image (actual size).

I was thinking I could probably do this with a combination of jQuery and PHP - PHP to get the dimensions of the image, and jQuery to set the display dimensions based on some variables put in with PHP - eg:

<script type="text/javascript">
     var imageWidth = <?php echo 'something';?>
     if (imageWidth > 650) {
          $('#image').css('width', '100%');
     } else {
          $('#image').css('width', '<?php echo 'something';?>');
     }
</script>

Thanks to anyone who can help me solve this conundrum.

Alfo
  • 4,801
  • 9
  • 38
  • 51

4 Answers4

2

I'd instead use the CSS max-width property.

You will also have to set display: block.

Of course, there is a catch with this method, and that is that you are sending possibly huge images to the browser. For that, you can resize them dynamically with PHP using imagecopyresampled().

Brad
  • 159,648
  • 54
  • 349
  • 530
  • That's not really what I was asking for - as the max width will also have to be set dynamically through PHP - a headache integrating PHP into CSS. – Alfo Nov 15 '11 at 19:58
2

I think your idea should work fine. Here's an example, it does work for me. (I've set style to image div so it'll be visible. If you want to resize the image itself, instead of div, it works the same.

<?php
$image_path = "yourpath/yourimage";

list($width, $height, $type, $attr)= getimagesize($image_path); 
?>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>zzz</title>

<script type="text/javascript" src="pathtojquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 var imageWidth = <?=$width?>;
 if (imageWidth > 650) {
      $('#image').css('width', '100%');
 } else {
      $('#image').css('width', '<?=$width?>');
 }
});
</script>

</head>
<body>
<div id='image' style='background-color: #ececec; border: 2px dotted #000000;'>
<img src='<?=$image_path?>'>
</div>

</body>
</html>
AR.
  • 1,935
  • 1
  • 11
  • 14
0

Definitely you can do what you, wrote in your question, but just make the following change:

  • Append "px" to the width of the image when you are setting the css width, in the else block.

and of course this should be inside $(document).ready(function(){ ... });

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
0

Try using a library such as SLIR: http://shiftingpixel.com/2008/03/03/smart-image-resizer/

It will allow you to dynamically resize images at ease, with mod_rewrite

Reza S
  • 9,480
  • 3
  • 54
  • 84