image.jpg
There is a load of lines like this with different pictures.
What i need to do is to make a script of some sort that would allow displaying those images without the need to write anything new in the body (it has to find the files from href's and display them ... without causing any more work for a person who puts those pictures there) and without the need to reorganize files (those files are tied to many other things ... change in the directory = everything crashes)
but i cant just find much ... most of the scripts i find requires me to place files in a specific folder or even worse ... to make img src tags for them
Can anyone point me towards some solution here ?
I'm not exactly clear on the question, but if I decipher it correctly a possible solution is to user scandir to read the contents of a directory. Likely will need to modify inside the foreach loop to fit your design (not sure if you have embedded script or not) but this will dynamically fetch and display images inside a directory.
if ($images = scandir('path_to_your_image_directory')) {
foreach ($images as $image) {
print '$image.jpg'; // Might need to preface the $image variable with path to your image directory
}
}
Related
I have an .Rmd file where I would like to adapt the style depending on whether it is a mobile version or not - in css it is quite easy to check the window width with #media;
But then, let's say I have a root variable
--mobile: 1;
How would I be able to use this variable in an R chunk of a .Rmd file, i.e. how could I retrieve this variable?
I think this is essentially impossible, unless you are just parsing a .css file that you're intending to include:
CSS values can only be determined by looking at the .html file produced from the .Rmd file, but at the time the R code is running, the .html file hasn't yet been produced.
One way to get a guess at the value might be to have your R code render an .html file from the same .Rmd you're currently using (and you'll have to be careful that the nested .Rmd doesn't render itself again, in an infinite recursion). Then parse that .html file to find the active value of that variable within the code chunk that wants to know it.
My problem is when I print the files within the directory, it prints out stuff like 'thumbs.db' and 'desktop.ini'. How to i make it print the name itself. All the files are .png by the way
static File overlayPath1 = new File(Minecraft.getMinecraft().mcDataDir, "\\TVMod\\" + filesList[0].getName());
thumbs.db and desktop.ini are both files in this directory, but normally in Windows they're hidden. However, because they're still there, they'll show up in your fileList.
If you don't want to use these files, you're going to have to skip them somehow. The implementation I'd suggest is to convert it to an ArrayList, then remove elements that don't match the .png extension.
However, without knowing more about your implementation, though, I can't easily suggest a way to do this.
Currently my application using this path for taking images:
D:\Workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\webapps\patternImages
So inside of this folder might be n number of folders may contains n number folders and inside of the folders it may contain n number of images,
I need to get all the image names.
In this case there are 2 scenario we go for usually getting image path
Using the static string path to get the image names
Using system.getproperty()
But I need more dynamic way of approach:
By this "patternImages" folder can be placed anywhere in the web and I should have to get all the folders name and image names without any issue and also should know which folder contains which image? any way?
It's hard to say what would be a perfect fit for your application since we don't know everything you're doing, but since it's a GUI application, I imagine presenting the user with a GUI to pick the file path is your best option. What class you use for that depends on what GUI library you're using, but here's a tutorial for JFileChooser: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html. The basic code you need is this:
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
} else {
// Do nothing or log it
}
where fc is an instance of JFileChooser. Just make sure you configure the object for picking directories and not files. You would probably wire this up to a button. Whatever library you use should provide some kind of dialog element to allow the user to pick a directory.
If a graphical directory picker is not an option, then I think you're stuck with a configuration file. Even if you can use a directory picker, you might want to consider a configuration file to save the user's last choice.
My Code:
I'm currently developing a game and throughout several different parts of the code I'm using some resources (Images/Sounds/Animations etc.). To avoid loading the same resource twice I wrote a ResourceManager, that returns the wanted resource if I pass a string to it.
Here's an example:
Image myImage = imageManager.getImage("princess");
This way I can reference a resource without knowing the name of the file or position of it, when I want to use it.
The trick here is that I have to load the images before I can get them like so:
imageManager.loadImage("res/princessImage.png", "princess");
This creates the ImageObject from the given file, and stores it into a HashMap with the given key.
My Problem:
I really don't want to hardcode the paths to these resources, because I'd have to change the sourcecode every time I decide to move or rename any of the resource-files.
A possible solution (?):
I thought about creating another HashMap that reads some kind of configFile and maps the in-code-resource-names to the resource-paths in a HashMap. The file would look somewhat like this:
princess: res/princess.png
hero: res/hero.png
sword: res/items/sword.png
This way I could use resource-names like "princess", "hero" or "sword" safely and don't worry about their position on the hard drive while I'm coding. Whenever I move or rename a resource-file I just update the path/name in this configFile and everything would be fine.
On the other hand I think it's pretty ugly to have one giant file that maps every in-code-resource-name to a path. This could result in one giant String to String HashMap which I'd have to store in the ResourceManager aswell. Things could get pretty confusing/unclear.
Does anyone have a better solution for me?
I'd really appreciate your help,
Thanks :)
Using a config or resource file as you described is a fine approach. Instead of populating a HashMap, though, consider using ResourceBundle or PropertyResourceBundle. It is designed to hold/access such things. http://docs.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html
Following on from my previous question, my program doesn't detect the 300 images that have just been created in a particular directory; instead, it only detects desktop.ini, which is not the case as I can physically see that the files have been created within said directory and do exist.
Can somebody please explain why this happens as when I run the program the next time, it seems to work just fine?
The only way that something is detected within the directory on the first run is when there is at least one file which exists in the directory before the program is compiled and executed.
Many thanks.
UPDATE: Files are detected as follows:
//Default greyscale image directory (to convert from greyscale to binary).
static File dirGrey = new File("test_images\\Greyscale");
//Array of greyscale image filenames.
static File imgListGrey[] = dirGrey.listFiles();
without knowing how you create the images, this question is akin to 'How many kittens are under my desk right now?'
Are you creating the files yourself? If so, are you closing any file handles referring to those files once they are created?
You're creating the file list in a static array, and it's created when the class containing the array is loaded by the Java class loader, which is probably before you create the image files. That's why the array contains an outdated list.
static is rarely needed, mostly useful for constants (things that never change, such as 42), for pure functions (Math.sqrt()) and a few other special cases. When you use it, you have to learn all the tricky initialization order stuff. Otherwise, just stick with non-static variables.