Store images in Javascript object - javascript

I am not sure if this is possible, but I want to store an image in a JavaScript variable or an object and when the page loads, I want to make those images appear where desired.
I want to know if some images are converted to binary form. Can they be converted back to images with JavaScript?

It appears that the OP is requesting how to do data islands in JavaScript, specifically for images. None of the answers previously given provide such a method, so here you go.
Basically, you encode the image as a base64 string and then set that as the source of a DOM element. Setting the source of an Image object to a url is not equivalent, since it requires an addition HTTP connection.
var data = 'data:image/gif;base64,'+
'R0lGODlhAAEwAMQAAJ2M5Me98GRK1DoYyYBr3PHv++Pe99XO81Y50auc6PBkZEgpzbmt7HJa2I57'+
// snip //
'fS3CqU7XGYgE+GqHvrLJ8Tr6qXmqiwAF9CffgnMNqmWHAWNBwwGsKpKsrmJqltOOV69nuYxSkqpo'+
'Tata18rWtrr1rTIIAQA7';
var icon_elem = document.getElementById("icon_here");
icon_elem.src = data;
The above code and a full example can be found here: http://www.kawa.net/works/js/data-scheme/base64-e.html

You can simply use
var img = new Image();
img.src = "http://yourimage.jpg";
to create a DOM image.
A DOM image is an object in memory that contains the image binary form, so there's no need to convert it back to an image since it's already one.

See, this is a simple matter. But the method to approach this problem is not the way you are trying right now.
What you think will work:
We'll store the image (its binary data) in a js variable, and then slap it on the page any time.
How it will work much more easily:
you just have to create a DOM image on the page, and set its source. The browser will fetch the image from the server automatically.
Examples:
ex-1:
var img_src = "http://someserver/yourimage.png";
var node = document.getElementById('the-node-in-which-i-want-my-image');
node.innerHTML = "<img src='"+img_src+"' alt='my image'>";
ex-2: (using jquery) - this is essentially the same as above, only much easier to write:
var img_src = "http://someserver/yourimage.png";
$('#the-node-in-which-i-want-my-image')
.html("<img src='"+img_src+"' alt='my image'>");
Now, there's one more thing: the browser starts fetching the image after this code runs, so the image actually appears a little after you insert it into the DOM.
To prevent this, you can pre-fetch the images using:
var prefetch = new Image();
prefetch.src = "http://someserver/yourimage.png";
Cheers!

Related

How to get loaded image property in phaser.js?

I have a problem for getting properties of loaded image in Phaser.js.
Now I resolve it by accessing private variable (a suck method I known...):
var image = game.textures.get("imageA")
console.log("width",image.frames.__BASE.width);
Does anyone has a better solution to get these properties?
Thanks a lot.
Depending on your UseCase, you could use the getSourceImage or get method, of the Texture object.
It is abit longer, also but works (if you need/want the html element ):
var image = game.textures.get("imageA");
console.info(image.getSourceImage().width);
here the link to the documentation
Or you could use the get function of the texture (if you need/want the phaser frame):
var image = game.textures.get("imageA");
console.info(image.get().width);
here the link to the documentation
The parameter for get and getSourceImage are optional, but you could enter a name/index of a frame, if you need a specific frame.

How to replace a graphic with given file in InDesign via script?

I need to replace some images in an InDesign document with a given file. This happens using the InDesign server, but scripting is almost the same as with regular InDesign, except no user interaction is possible.
What I have is a InDesign Document, the ID of an Rectangle containing some image and the Path to a new image that should replace the image.
The image should be replaced, but the settings like FitOptions etc. should stay the same. Also, the new file shall be embedded in the InDesign Document. There is already some code that sort of works:
function changeImages(doc) {
var arrayLength = changeImage.length;
for (var i = 0; i < arrayLength; i++) {
var fr = doc.textFrames.itemByID(1 * changeImage[i].id);
if (!fr)
continue;
var file = File(imagePath + changeImage[i].file);
fr.place(file);
fr.fit (FitOptions.CONTENT_TO_FRAME);
fr.fit (FitOptions.PROPORTIONALLY);
fr.fit (FitOptions.CENTER_CONTENT);
}
}
This doesn't seem right. Why is it using doc.textFrames when the object is a rectangle? I am actually confused this even works.
Also it just sets some FitOptions, but I want to keep the existing.
I am very new to InDesign scripting, so I am lost here. I am reading the docs and other resources, but I am confused. e.g why is there doc.textFrames.itemByID but nothing like that for other Frames? Do I have to iterate doc.allPageItems and compare ids?
itemByID is a method available for all pageItems and both textFrames and rectangles are subclass of pageItem. So you have access to this method from both, and it'll give the same result. You should be able to use doc.rectangles.itemByID as well. See: http://www.indesignjs.de/extendscriptAPI/indesign11/#Rectangles.html#d1e201999__d1e202138
But you are right that the description is a bit confusing, it says:
Returns the TextFrame with the specified ID.
which is obviously not the case. If you already have the IDs you want to target, you could use doc.pageItems.itemByID, which is maybe less confusing, since basically you're looking for pageItems when using itemByID.
As for fitting options, they are a property of your rectangle object, so placing a new image shouldn't change the fitting options. If you want to keep the same, simply remove the calls to fit(). See in property list of Rectangle, frameFittingOptions: http://www.indesignjs.de/extendscriptAPI/indesign11/#Rectangle.html
Josef,
I've had the same problem with InDesign CS4 with keeping the original FitOptions. I was never able to figure out how to get the settings currently being used in InDesign CS4.
To get around the problem what I did was to set the value in the Fitting on Empty Frame in the Frame Fitting Options in the InDesign document.
Then in code I used that setting, something like this:
changeImages (app.activeDocument);
function changeImages(doc)
{
with(doc)
{
var rec = doc.rectangles.itemByID(207);
var file = new File("c:\\new_image.png");
rec.place(file);
rec.fit(rec.frameFittingOptions.fittingOnEmptyFrame);
}
}

How does javascript recognise objects

I'm trying to test something which I might later use in a more complex application however I'm struggling to find why this following code is not working. I'm not that familiar with javascript however I have noticed that there is no such thing as casting?
I have the following piece of code which in Java if casted should work however in this following script, it doesn't. To be more specific the following error gets returned:
Uncaught TypeError: Failed to execute 'putImageData' on 'CanvasRenderingContext2D': No function was found that matched the signature provided. test.html:16
window.onload
Here is the code
<script>
window.onload = function(){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var imageObject = ctx.getImageData(0,0,100,200);
var stringed = JSON.stringify(imageObject);
var nowShouldBeImage = JSON.parse(stringed);
ctx.putImageData(nowShouldBeImage,0,0);
}
</script>
My question is why is it that the object after being parsed is not getting recognised correctly? What am I missing here? And how would I tackle the lack of this functionality in javascript? ( Maybe not lack of functionality but rather difference from Java)
The JSON object parses files into simple structures.
You're basically getting an array of channel values back, I believe.
.putImageData doesn't work with arrays, but rather with ImageData objects (which may behave like arrays, but are not).
You've got multiple solutions, here, if what you're interested in is sending something to / loading something from the server.
If you want to use XHR, then you can use base64 to go back and forth.
var canvas = getMyCanvas(),
ctx = canvas.getContext("2d"),
encImgData = canvas.toDataURL();
var img = new Image();
img.src = encImgData;
ctx.drawImage(img, 0, 0);
If you want to go through and manually edit that image's pixel-range thereafter, draw it to an offscreen canvas, .getImageData() to match the width and height of the image you cared about, edit the pixels, and .putImageData() using the edited object.
But JSON isn't going to marshal things into specific types for you.
It takes simple data structures, puts them in strings, and outputs those structures into other simple structures.
You can also write a node-modifying function which will be called as each node in the structure is being rebuilt.
If you specifically want to write a function which checks if the node is an array and it's filled with what you assume are per-channel values, and then use ctx.createImageData, and manually loop through and fill each channel of each pixel with the array value, and then return the constructed ImageData object, you can do that, too, I suppose. But that's not going to do you any favours regarding speed.
Java ≠ Javascript, not use javascript as java.
this step seems useless:
var stringed = JSON.stringify(imageObject); //object to string
var nowShouldBeImage = JSON.parse(stringed); //same string to JSON?!?! nothing change... why?!!?
remove the useless step:
var imageObject = ctx.getImageData(0,0,100,200);
//var stringed = JSON.stringify(imageObject); remove this two line
//var nowShouldBeImage = JSON.parse(stringed);
ctx.putImageData(imageObject,0,0);
The object nowShouldBeImage isn't the same of imageObject because inside of this object there are some properties that are particular because are the result of getImageData() that after JSON.stringify are lost (nowShouldBeImage is an instance of Object, imageObject is an instance of ImageData)
You are serializing binary data with JSON. If you really need to do that, try encoding the ImageData object from getImageData() as base64 first.

Is there a way to prevent loading of images in a jQuery objectified HTML fragment

I have a HTML fragment that I'm objectifying via jQuery for the purpose of extracting some data from it. This fragment has some image resources that I don't want the browser to download. Is there a way to do it.
A simplified version of my current code:
var html = '<p class="data">Blah Blah</p>...<img src="/a/b/...png">...<div>...</div>';
var obj = $(html); // this makes the browser download the contained images as well!!!
var myData = {
item_1: obj.find('.data:first').text(),
item_2: obj.find('.data2:first').text(),
.... // and so on..
};
Unless you think there will be instances of the substring src= in the string that matter to you, you could do:
html = html.replace(/src=/g, "data-src=");
or possibly
html = html.replace(/src\s*=/g, "data-src=");
...to allow for whitespace. That way, the browser won't see the src attribute, and won't trigger the download.
Sometimes the direct approach is best. Of course, if you think there may be src= substrings that will matter in terms of what you're trying to extract, then...

convert an image server side to CanvasPixelArray

edit
so it looks like i have to somehow convert an image to
https://developer.mozilla.org/en/DOM/CanvasPixelArray
serverside
my current attempts have been to *
Pass a base64 encoded image from the server ... then converting it to binary with atob()
but then i get stuck trying to draw this onto a canvas without having to do something like
var i = new Image();
i.onload = function(){.....}
i.src = "base64 string"
So an even more general statement
I am trying to bypass creating a Image object by any means to display a new image
If your goal is to avoid the use of javascript and to simply embed the image in your page, you can simply do
<img src="data:image/png;base64,UhhIaminbase64...">
If your base64 is obtained dynamically in ajax, then the way to use the decoding library is to create an Image object with the code you made.
An alternate solution would be to send directly the RGB values in a binary array (base64 encoded if you use json) and to iterate client-side on this array in order to change your canvas' imageData. That seems easy enough but somewhat inefficient as you wouldn't have the compression of PNG or JPEG.
With html5 you can do so:
<script>
window.onload = function()
{
var img = new Image();
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAQx0lEQVRogbWZeZBldXXHP+fc+5bu6Z6F2ZkBZkH2xSWiwOBWhkAwYoAoSRUYQ4VQYNSkrFQllZRLWSkrJCVixKSwXChTCoqaYELGwmWiIooyQqFkyMg2A8zAbN3T/V6/9+7vfPPH777X0yxaMcmdufV+/bv3/u45v/M953zPucavcDRXbGTNBe9hyWkXUpQNGoXRbBQUzkQKO0loQ2G2HAMkSykOiXjSsZ9B7J/rB/1BRTKn9/TP2Lftk0z95Gu/iijY/+Tm8WNeynF/cDPtVZsoBx3ajSBVuqBXcXlnoN+YaBdr1i5rctR4k7GW0SiNwUB0+4mDswOePtTlwOHe1Fhp29pNu7Vd+m3dStUcLdLcLHvvvJ4D3/30/4MCXrDxmi8wefJraAxmGC9pdQbx/kOdeNep6yfGX75pCaeuWwSGnj5Y6cDswDu9iF5KNMy83bJYNtZg7bKWj7fQQ7s7tv3Rg/x45wHaDb9l+eLmX3R71ZOz0aLqd9j1yd+n+/iP/28UWHT8uRx39S00TEw2xOwgfXC2G3/1hjNWcf4Zy3lif4/tj83w0NNdDndFWRQUbmAgBVIQKUiRSFWiWRonrh3nFZuWcMqxi7hnxyG+es8TGLplxeLmNdPdqjvXWMzU9z7Hnq/85f9OgeWvv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3YP/uHnB+LisnDKEhVG7SCjLw/ZGsOxDdGEW1hgQQivAqpKrJywe845unHdKSv9vqNaxuoJY7w0EgV4QVmWI98rioJms8nWrVu54YYbsgJbt27lmWeewczKiFgEjLn74ogYAybdfQmKoiy8NHRUu6B4bCo23bGzevN3dw82zw0o3bHCLTx3HRcqAJFqdlvlxG5mplcdXe6+cHNj68tW+4/mKtqBzaXQvpAqM08RMQUcdvduREwDXXefjYhq+fLlXHvttVmBO+64g6mpqUngMuB8YC2wHBg74rT6bAFFYUSzoDLEjoPRvG9PlA8fGLB7Wra/UzMyYxT1JtrGMUuME5YWOn1lGWeu9l5hpn6iqOZr2QT06qcEdOuzAxwEngK+DnxpYmLi8HXXXUcJueqPiIa7r4uI44A1tQWaQNPdi4hQ/es1HEjyliSOm3SOX1rglBSee15zFcwOYKwBY+WwQDEGSZaCopdsPCJwd4aFm7s3IqIFVO5uETEO9N19EBGLgaGMjSHrLQFarRZmdgC4Efg8sAxYAUzUv2M1FI6qr40OiWYlrQhsLDN3cidCEQ2HKtyn5rKE7uYRCsDcbQA8C8yxMDsdBvYBg/ravtoCB+r7nwWmG40GZVlmCF100UVceeWVDAYDfslR8PxPs0NYlb/s4eccQ7jEC8ynX/Rgo9Fg27Zt3HTTTfw3QU9CopWc95cAAAAASUVORK5CYII=";
document.body.appendChild(img);
};
</script>
Here is a working example: http://jsfiddle.net/4ZaUu/3/

Categories