I'm able to create/draw chart with jqplot. When I right-click on it and save it as PNG, it is saving a blank image.
Here is the image data:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbkAAAEeCAYAAAAXTWt+AAACAElEQVR4nO3BMQEAAADCoPVPbQ0PoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJ4MtD8AAaRK8nUAAAAASUVORK5CYII=
Could anybody let me know is this image data is good and can I save it?
Thanks a million!
-Parameshwar
jqPlot can convert it's chart to a image with the function jqplotToImageStr()
You have to run a JavaScript to convert the dynamic chart to a savable image.
See the answers here:
JqPlot As Image
I render images from graphs without any problem...
var imgData = $('#chart').jqplotToImageStr({});
Then I send to a php page the imgData var (that contains the base64 image) to let it be downloaded directly.
I you want just to show that image, you just have to add that var to the src attr of a image tag (or opening it as a popup passing the var as the href attr of the window)
window.open(imgData,"_blank","scrollbars=0");
Anyway, if you pass that base64 code to a php page, you can download it using that code (and appropriate headers):
$data = $_POST['data']; //your base64 code
$data = substr($data,22); //remove some unuseful chars
$img=base64_decode($data); //decode data
$out = "../tmp/report_".date("d-m-Y_His").".png"; //set output image location
file_put_contents($out, $img); //create file
echo $out; //let user direct download it
Related
I want to crop an image with Google App Script if an image outside the page frame, but as far as I checked in Google App Script documentation and I could not find a way to crop the image.
pageElements.asImage().replace (imgBlob, true); it is not allowed to pass cropping dimensions as parameters in .replace() to crop a image.
i know this can be achieved using a custom API , passing the image blob and crop area that will call cropping method on another server.
But how it will be possible to work with Google App Script, looking for expert advice.
How about this answer?
Issue:
I think that in the current stage, replace(blobSource, crop) has the limitation. The official document says as follows.
crop Boolean: If true, crops the image to fit the existing image's size. Otherwise, the image is scaled and centered.
I confirmed that when the image is cropped using replace(blobSource, crop), the center of image is left. It seems that this is the current specification. And although there is the "cropProperties" of "UpdateImagePropertiesRequest" in Slides API, unfortunately, in the current stage, this cannot be still used. This has already been reported. Ref
Sample script:
If you use replace(blobSource, crop) under the current specification, how about the following sample script? As the sample situation, 2 images of "image1" and "image2" are prepared in the 1st slide, and "image1" is cropped using "image2".
The flow of this script is as follows.
Flow:
Retrieve 2 images from a slide on Google Slides.
Crop "image1" using "image2". By this, "image2" is replaced with "image1".
Move the cropped image to "image1".
Remove the original "image1".
Script:
function myFunction() {
// 1. Retrieve 2 images from a slide on Google Slides.
var slide = SlidesApp.getActivePresentation().getSlides()[0];
var images = slide.getImages();
var image1 = images[0]; // Red image.
var image2 = images[1]; // Blue image.
// 2. Crop "image1" using "image2". By this, "image2" is replaced with "image1".
var replacedImage = image2.replace(image1.getBlob(), true);
// 3. Move the cropped image to "image1".
replacedImage.setTop(image1.getTop()).setLeft(image1.getLeft());
// 4. Remove the original "image1".
image1.remove();
}
Result:
When the script is run, "image1" is cropped. But it is found that in the current stage, the center of "image1" is left by the crop.
Note:
Slides API and Slides Service are growing now. So I think that this situation might be changed by the future update. But if you want this soon, how about requesting this to the issue tracker as the future request? Ref
References:
replace(blobSource, crop)
CropProperties
Added:
At an additional sample script for using replace(blobSource, crop), I would like to propose the method for using the self image. In this sample script, when the image is sticked out, the image of out of page is removed by cropping. The basic method is the same with above sample script.
Sample script:
function myFunction() {
var s = SlidesApp.getActivePresentation();
var slide = s.getSlides()[0];
var images = slide.getImages();
var image = images[0];
var pageWidth = s.getPageWidth();
var imagePosition = image.getLeft();
var imageWidth = image.getWidth();
var check = imagePosition + imageWidth - pageWidth;
if (check > 0 && check < imageWidth) {
image
.duplicate()
.setWidth(pageWidth - imagePosition)
.asImage()
.replace(image.getBlob(), true);
image.remove();
}
}
Result:
Note:
In this sample script, as a simple sample, I prepared only the right side of the horizontal direction. So when you want to remove the vertical direction, please modify the script for your actual situation.
I have an html5 canvas and one image object to save this canvas contents
(all drawings: images,draws,..).
i want to play with the order (zindex) of these 2 objects from 1 button as below:
when i want to draw, i call the canvas to the top .
when i want to save i call image to the top(full with datURL). (and rightclick to save as PNG)
These switching operations must be repetitive and reliable.
I used this code (in a function called by the button onclick) but it fails in the second time (canvas have new contents)!
var canvas = document.getElementById('ycanvas1');
// save canvas image as data url (png format by default)
var dataURL = canvas.toDataURL();
// set canvasImg image src to dataURL - it can be saved as an image
document.getElementById('canvasImg').src = dataURL;
NB: i use IE olecontrol embed on a form (working on local) .Is there any security restrictions ?
I guess in the first case:
canvas.style.display="none" // hide the canvas
img.style.display="" //show the image
in the second case:
canvas.style.display="" //show the canvas
img.style.display="none" //hide the image
Canvas image produces awful color shifts in Chrome and Firefox (Mac) when saved to disk or uploaded to server. Safari has faithful color. Examples below + JSFiddle to reproduce with original image. Notice how the subject's face becomes very orange.
http://jsfiddle.net/E4yRv/141/
:: Includes step-by-step with sample images on how to reproduce
Code Excerpt:
canvas.ondrop = function(e) {
e.preventDefault();
var file = e.dataTransfer.files[0],
reader = new FileReader();
reader.onload = function(event) {
var img = new Image(),
imgStr = event.target.result,
imgData = context.getImageData(0,0, context.width,context.height);
state.innerHTML += ' Img source dropped in: <a href="' +
imgStr + '" target="_blank">save image</a><br />';
img.src = event.target.result;
img.onload = function(event) {
context.height = canvas.height = this.height;
context.width = canvas.width = this.width;
context.drawImage(this, 0, 0);
state.innerHTML += ' Canvas img drawn: save canvas <br />*add .jpg extension when saving';
};
};
reader.readAsDataURL(file);
return false;
};
When canvas draws the image in the respective browser the color renders to match the original. However, if the image is saved to disk locally and viewed in Photoshop (the only program that knows how to true handle color) the colors have shifted! Same occurs if viewing the saved file in a different browser.
Inspecting the images in photoshop, none have an embedded color profile. However, there is some translation of color that has occurred! It does not appear to be a case of misaligned profile or missing profile.
I have produced a detailed JS-Fiddle below on how to reproduce the issue.
http://jsfiddle.net/E4yRv/141/
I have encountered the similar color shift phenomenon in browser recently, and the translation of color in the human face can be reproduced by the JSFiddle code. After some research and thanks to the suggestion provided by Alexander O'Mara (the color profile in image), here is my conclusion:
The phenomenon should be caused by the embedded color profile in the sample image. If you open the original image in photoshop, it will show a warning window, which indicates that the color profile doesn't match the current used color gamut, and three options can be chosen:
use embedded color profile (in short, let's call it ref_ICC)
convert the color to the current used color gamut (after the conversion, a new ICC, i.e., the con_ICC, will be embedded in the resulting image)
ignore the color profile
After saving the three images by choosing the respective options, let's open them in some image viewer. I use XnView to check if any color profile (ICC) is embedded in image, and use Photoshop to check the RGB histogram. The result is:
(the downloaded* stands for the image obtained by the JSFiddle code of canvas.toDataURL)
| option 1 | option 2 | option 3 | downloaded*
histogram | ref_hist | con_hist | ref_hist | con_hist
ICC | ref_ICC | con_ICC | X | X
In a viewer (FastStone, for example) which doesn't care about the color profile (ICC), the displayed RGB value only depends on the histogram. So the original image (option 1) looks different from the downloaded one, in which the histogram seems to be converted by the browser in a way similar to the photoshop does for option 2. On the other hand, the image pairs of "option 1 & 3" and "option 2 & downloaded" show the same displayed value.
If the images are viewed in photoshop, then the three images (option 1 & 2 & downloaded) show the same displayed value. And as a final comparison, the displayed value of the downloaded image is the same in FastStone and Photoshop, while the original image shows different displayed value in these two viewers.
I use Rubyvis to generate SVG plots, and then allow the user to save either to SVG directly, or to some other format using RMagick.
The SVG plots have a set size, which is specified in the SVG file. It seems to me, then, that it should be trivial to convert to a PDF of the same size.
Unfortunately, this appears not to be the case. I can produce a PDF in this manner, but it is much larger (dimension-wise) than the PDFs produced if I first open the SVG in inkscape and then print-to-file as a PDF.
Worse, the PDF image quality is terrible.
Am I missing some instruction for Magick? Here's the code:
image = Magick::Image::from_blob(svg_string_data) { self.format = 'SVG' }
image[0].format = 'PDF'
image[0].to_blob
I then write the value returned (the PDF blob) directly into a file.
The answer comes to you based on a tip from cptjazz on github.
First of all, the documentation for RMagick is often wrong. I doubt this is a version problem, because the ImageMagick-hosted docs are v2.12.0, and I'm using v2.13.1. Either way, here is what you need to know.
The docs claim you can use image[0]['pdf', 'use-cropbox'] = true, since true.to_s yields the String 'true'. In fact, it needs an explicit string, and the []= method takes only one key, not two.
I did not experiment with pdf:use-trimbox, mainly because I wanted an option that also works for postscript. For postscript, you should be able to amend it only slightly, and set ps:use-cropbox to 'true', but RMagick's docs are unclear as to how one may properly set the geometry on a PS, PS2, or PS3. (Normally in ImageMagick, one would set the density to '300x300' and the geometry to '24%', supposedly.) And for some reason, PS3-format output files do not scale as well as the PDFs produced by RMagick. But I digress.
Here is what I used for PDF:
image = Magick::Image::from_blob(svg_string) { self.format = 'SVG' }
image[0].format = 'PDF'
image[0]["use-cropbox"] = 'true'
image[0].to_blob
And here is what I used for PS:
image = Magick::Image::from_blob(svg_string) { self.format = 'SVG' }
page = image[0].page.dup
image[0]["use-cropbox"] = 'true'
image[0].format = 'PS3'
image[0].density = '100x100'
image[0].page = page
image[0].to_blob
For some reason, setting a higher density makes the image smaller. Why that should be is a mystery.
I have existing PDFs to which I need to dynamically add an image/images. The image comes from a file upload. Once I have the file uploaded, how can specify where to place the image on the PDF. One code snippet I found does not work correctly. This needs to work for PDFs with any number of pages. From what I understand, absolute positioning is set from the bottom-left corner of the last page of the PDF. If I need an image to be displayed 30 pixels from the top and 50 pixels from the left of page 1, how can I accomplish this? Or, if I need to display an image 50px from the top/100 px from the left on page 2?
I've tried using the code found at http://rip747.wordpress.com/2009/03/26/add-an-image-dynamically-to-a-pdf-with-cf-and-itext/. I've modified it for my needs below:
<cfscript>
myLeft = 30;
myTop = 50;
myPageNum = 1;
// output buffer to write PDF
fileIO = createObject("java","java.io.FileOutputStream").init(myOutputPath);
// reader to read our PDF
reader = createObject("java","com.lowagie.text.pdf.PdfReader").init(mySourcePath);
// stamper so we can modify our existing PDF
stamper = createObject("java","com.lowagie.text.pdf.PdfStamper").init(reader, fileIO);
// get the content of our existing PDF
content = stamper.getOverContent(reader.getNumberOfPages());
// create an image object so we can add our dynamic image to our PDF
image = createobject("java", "com.lowagie.text.Image");
// initalize our image
img = image.getInstance(imgPath);
x = (reader.getPageSize(1).width() - img.scaledWidth()) - myLeft;
y = (reader.getPageSize(1).height() - img.scaledHeight()) - myTop;
// now we assign the position to our image
img.setAbsolutePosition(javacast("float", x), javacast("float", y));
// add our image to the existing PDF
content.addImage(img);
// flattern our form so our values show
stamper.setFormFlattening(true);
// close the stamper and output our new PDF
stamper.close();
// close the reader
reader.close();
</cfscript>
The above code places my image at the top-right corner of page 2 - 50px form the top/30px from the left.
I know I'm close...just need a little help getting this nailed down for my needs.
I've updated my code. This gets the image to the top left corner of page 2 - correct positioning, but I want it on page 1:
x = myLeft;
y = (reader.getPageSize(1).height()) - img.scaledHeight() - myTop;
I thought maybe I needed to add the height of page 1 to get the image up to page 1, but the image completely disappears when I try either of the options below:
// I figure I'll need something like this to handle multi-page docs
y = (reader.getPageSize(1).height() * reader.getNumberOfPages()) - img.scaledHeight() - myTop;
y = reader.getPageSize(1).height() + reader.getPageSize(1).height() - img.scaledHeight() - myTop;
You're getting your "OverContent" from stamper.getOverContent(reader.getNumberOfPages());. The parameter for getOverContent() is the page number. So your code is getting a PdfContentByte for the last page, not the first.
I found my answer:
The page number has to be set in com.lowagie.text.pdf.PdfStamper.getOverContent():
content = stamper.getOverContent(myPageNum);
Knew it was easy.
are you using CF8+? You can use
<cfpdf action="addWatermark" source="myPDF.pdf" image="myImage.jpg"
position="0,0" rotation="0" showOnPrint="true" opacity="10">