views:

104

answers:

4

I have a large number of images that I am putting into web pages. Rather than painstakingly enter all of the image attributes, I thought I could write a script that would do the work for me. I just need a little push in the right direction.

I want the script to get the width and height of each image, then format this into an img tag that would include the url I'm using. I'm thinking that first I would loop through the files in the directory and output the results that I'm looking for to a file. Then I can copy and paste.

Ideally, it should eventually have a GUI where you can choose each file, save the result to the clipboard, then paste it directly into the document. But I believe this is beyond me at this point.

I have a basic understanding of coding and have done a smattering of scripts in Python and PHP.

Where do I start? Can I use Python? Or PHP? Or a different language? PHP has the getimagesize() function that returns the data I need. Does Python have a similar function?

Sorry to be long winded. And thanks for any input.

+1  A: 

Maybe the beter solution would be not to create a script that generates a list of all the image elements for you to put in your document but rather generate the image elemetns on the fly ind the desired document. This could be done like this:

if ($handle = opendir('/path/to/images')) {
    while (false !== ($file = readdir($handle))) {
        ?>
        <img src="http://you.server.com/path/to/images/&lt;?php echo $file ?>" alt="<?php echo $file ?>" />
        <?php
    }
    closedir($handle);
}

Furthermore i dont understand why you would want to include image height and width in the img elements as there is no need to specify those. The height and the width are only used to modify the image dimensions as the browser automaticly display's the actual size.

But to include the image size the getimagesize() fuction would work:

if ($handle = opendir('/path/to/images')) {
    while (false !== ($file = readdir($handle))) {
        $size=getimagesize('/path/to/images/'.$file);
        ?>
        <img <?php echo $size[3] ?> src="http://you.server.com/path/to/images/&lt;?php echo $file ?>" alt="<?php echo $file ?>" />
        <?php
    }
    closedir($handle);
}

more info: http://nl2.php.net/function.getimagesize

Mathijs Henquet
Because if you don't specify the width and height, the rendering engine has to wait until the images are loaded (at least partially) to know their dimensions, which leads to annoying changes in the layout while the page is loading.
Ölbaum
Ok well to add image size you could do like this: if ($handle = opendir('/path/to/images')) { while (false !== ($file = readdir($handle))) { $size=getimagesize('/path/to/images/'.$file); ?> <img <?php echo $size[3] ?> src="http://you.server.com/path/to/images/<?php echo $file ?>" alt="<?php echo $file ?>" /> <?php } closedir($handle); }more info: http://nl2.php.net/function.getimagesize
Mathijs Henquet
+3  A: 

Check out the PIL:

from PIL import Image
im = Image.open("yourfile.jpg")
print im.size

For looping through files see this tutorial.

Tristan
A: 

You can also use shell identify command from Imagemagick:

for file in dir/*; do
  identify -format "Width: %w, Height: %h" $file
done
pingw33n
A: 

I think this should do what you want. But it's not necessarily the best way (it's late, I'm tired, etc...):

<?php

$directory = "img"; // The path in which your images are located.

if ($directory) {
$files = scandir($directory); // $files becomes an array of files in the relevant directory.
}

foreach($files as $k => $v) {
  if ($v == "." || $v == "..") {
    // because I'm not good enough to think of a better way
    unset($k);
  }
  else {
    $size = getimagesize($directory . "/" . $v);
    $images[] = array('img' => $v, 'w' => $size[0], 'h' => $size[1]); // $size[0] is width, $size[1] is height, both in px.
  }
}

unset($files); // I just like to clear as I go. I make more than enough mess as it is without keeping it around.

  if ($images) {
    foreach($images as $key => $value) {
      echo "\n\t\t<img src=\"$directory/" . $value[img] . "\" width=\"" . $value[w] . "px\" height=\"" . $value[h] . "px\" alt=\"" . $value[img] . "\" />";
    }
  }

?>
David Thomas
Thanks very much for your reply to my post. Since I've been dabbling in PHP I went with what you suggested with a few revisions. Here's what I came up with:<?php$directory = "/home/ben/myDirectory"; $url = "http://www.myUrl.com/"; $files = scandir($directory); $fp = fopen('/home/ben/dir/for/file', 'w'); foreach($files as $k => $v) { if ($v == "." || $v == "..") { unset($k); } else { $properties = getimagesize($directory."/".$v); fwrite($fp, "<img src=\"$url$v\" $properties[3]>\n"); }}?>This does what I need, but it may not follow good procedures. Tx
If you went with my suggestion, then I think you're meant to accept the answer =) On the other hand, if your code is sufficiently different -by your own assessment- you should answer the question yourself, and accept that. If only because code's a darn sight easier to read in an answer than in a comment... =)
David Thomas