Does anyone have a script to automatically generate image tags from an image directory?
I do a lot of slicing in Photoshop and it gets tedious for you to manually write a tag <img />
for each one - write to filename, check height and width, write alt tags, etc.
I can use Photoshop to generate HTML, but they usually strip it away without producing XHTML, or wrapping it in tables, and so on. I'm trying to make my life easier than this.
So I'm wondering if anyone is using a script that automatically generates img tags based on a directory? Or if some IDE I don't know about does this? I just want it to generate a bunch of tags like this:
<img src="{filename}" alt="" width="{width}" height="{height}" />
First I want to thank you for the information on the width and height of the image. It is very good that you do this, everyone should do it. You can use any scripting language like Python or PHP for this with an image library like imagemagick or gd. Without knowing what language or tools you are using to launch your site, I cannot provide an example.
In Python, using the templating system, you can do something like this:
<div>
<% addImg("/images/myImage.png") %>
</div>
To create the correct image tag:
<div>
<img src="/images/myImage.png" alt="MyImage" width="200" height="100" />
</div>
Somewhere in your python, you define:
def addImg(imgPath):
#do image processing here
a source to share
If Photoshop gets the job done, it might be easiest to process the output there using a regular expression or something to fix the problems in it. This will probably be easier than trying to override this functionality from scratch.
If you can post a sample HTML Photoshop output, I can provide a regex search / replace that will convert it to what you are looking for.
a source to share
Made by php script:
<?php
$images = scandir('images');
$txt = '';
$tmpl = '<img id="{{id}}" src="images/{{name}}">';
foreach($images as $image) {
$tag = $tmpl;
$tag = str_replace('{{id}}', explode('.', $image)[0], $tag);
$tag = str_replace('{{name}}', $image, $tag);
$txt .= $tag . "\n";
}
file_put_contents('tags.txt', $txt);
Create a php file and run it:
Php script.php
The PHP script must be located in the same directory as the images folder.
You will get your tas image in tags.txt file.
a source to share