Use image in a SAS Stored Process's HTML Stream - image

I am creating a report with SAS STP and I want to display a image(a logo) on the report. Okay here is what is happening:
data _null_;
file _webout;
put '<html>';
put '</html>';
run;
I am PUTing HTML because I have complex table formats which I need to display and I am not using %STPBEGIN & %STPEND because that opens up an ODS Stream which frankly I do not know how to handle and I am having problems. Not using %STPBEGIN means the above code. This has been a very successful mechanism for me. I can show beautiful reports with CSS and everything. The only problem is images. A client has recently requested to put logo on every report. i though this was going to be easy but it has not been. Ok here is the deal, I tried to use <img src=" "/ > tag and I thought I would use some relative path and my image will show. This technique succeeded and failed.
I added an image to a folder location using SAS Management Console
and use its relative path '/Products/SAS Enterprise GRC/****' (didn't work)
I copied an image to default theme's images folder under Web/Staging/*** and tried to used the relative path (didn't work). So i tried to use other images from the the default theme. It worked.
I am stuck, how can I use a custom images here?

If your image is static, you can embed it into your results using a datastep without having to copy files to the server.
The trick to doing this is to encode the image into Base64 encoding, then you can embed the image into an <img src="" /> statement by using this magical notation:
<img src="data:image/png;base64,...." />
You can see that the src= attribute contains metadata to tell the browser that the value contains image data, that represents a png file (I used a png file when testing this post, you may have a JPG/BMP etc...) and that the value is encoded using base64. The 4 periods at the end would be replaced by your image data represented in base64 notation. This would look something like this:
<img src="data:image/png;base64,iVBORw0KGgoAAAAN
... much much more base64 content here ...
HSLyz+h9xy+7HbHRL83L1tv9h8+4d/+Ic/Gf8DiYav3mpqHAMAAAAASUVORK5CYII=" />
Converting your image to base64 is simple. You can simply google for an "online base64 image converter" such as this one. Drag and drop your image and it will produce your base64 code for you.
To get this into a datastep in sas, it's simply a case of:
data _null_;
file _webout;
put '<html>';
put '<img src="data:image/png;base64,iVBORw0KGgoAAAAN......etc..." />';
put '</html>';
run;
If you image is particularly big (say greater than ~32k) you may run into issues trying to output it from a datastep. I probably need to test this to clarify. You can work around this by reading the base64 image from a file in SAS and streaming it directly to _webout, using code similar to below:
data _null_;
file _webout;
infile '\path\to\base64\file.ext';
input;
put _infile_;
run;
If you want to get really tricky, you can take any image you like (such as a chart generated in SAS) and convert it to base64 on the fly, then stream it out. Here is some SAS code that will take an image file and convert it to Base64:
data _null_;
length base64_format $20 base64_string $32767;
infile "\your_sasdir\hi.png" recfm=n;
file "\your_sasdir\hi.base64";
input byte $16000. ;
* FORMAT LENGTH NEEDS TO BE 4n/3 ROUNDED UP TO NEAREST MULTIPLE OF 4;
format_length = 4*(lengthn(byte)/3);
mod = mod(format_length,4);
if mod ne 0 then do;
format_length = format_length - mod + 4;
end;
base64_format = cats("$base64x",format_length,".");
base64_string = putc(cats(byte), base64_format);
put base64_string;
run;
Here is the image I used to test this with:
Once converted, the Base64 representation should look like:
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABaSURBVDhP5YxbCsAgDAS9/6XTvJTWNUSIX3ZAYXcdGxW4QW6Khw42Axne81LG0shlRvVVLyeTI2aZ2fcPyXwPdBI8B999NK/gKTaGyxaMX8gTJRkpyREFmegBTt8lFJjOey0AAAAASUVORK5CYII=
I'm going to see if I can find a way to streamline this as this is something we do frequently at work.
EDIT : Interestingly, SAS9.4 seems to support doing this directly using ODS HTML5 in conjunction with the inline option. See the doc here.
See also this post, by Don Henderson, that provides a similar way to approach this problem. Thanks to Vasilij for the link.

When you define pictures in SAS metadata, it can be accessed via SAS Content server.
To get picture URL log into: 'https://severhost/SASContentServer/repository/default/sasfolders' and search for your picture.
If you defined your picture in catalog /Products/SAS Enterprise GRC/PictureName.gif, it should be accessible from adres 'https://severhost/SASContentServer/repository/default/sasfolders/Products/SAS Enterprise GRC/PictureName.gif(Report)'
Of course you have to remember, that customer user need to have access permission in SAS Metadata to read picture object.
If this won't solve your problem, please type which version of SAS software you are using.

I had a similar problem to you once. I have added the image to our intranet which happens to be SharePoint at the time. I defined that image to have public access level and then references in all my reports.
The idea that since the report is only for internal audience, they all will have access to intranet, but not necessarily to the Content Server so it circumvents the problem that Bagin mentioned.
If you don't have a suitable intranet, you could always reference a logo from your public website which is probably available to all of your audience even if they are external, but then you don't have control over that logo file and one day it might change in some undesirable way.
Regards,
Vasilij

Using SASjs you can compile ANY binary content into a SAS web service (Stored Process or Viya Job).
Here's an example using an MP3 file: https://github.com/allanbowe/sasrap

Related

Include pictures while converting ps1 to exe with PowerGUI

I use the PowerGUI editor to convert a ps1 file to an exe file. Reason is that I dont want people to see my source code. The script includes a own little GUI with a picture on it. My problem is that after converting the script to an exe file the picture will only be shown when it exists on a specific path. If I delete or move the picture from that path it wont be shown when starting the exe.
How can I include the picture to the exe? I want to have only one file in the end ...
One way you could do this is by converting your image into a Base 64 String, using the following:
[convert]::ToBase64String((get-content C:\YourPicture.jpg -encoding byte)) > C:\YourString.txt
With the string that is produced in the text file "C:\YourString.txt" you can copy and paste it into your code and load it into a picture object on the form like so:
$logo.Image = [System.Convert]::FromBase64String('
iVBORw0KGgoAAAANSUhEUgAAAfQAAACMCAYAAACK0FuSAAAABGdBTUEAALGPC/xhBQAAAAlwSFlz
AAAXEQAAFxEByibzPwAAAAd0SU1FB98DFA8VLc5RQx4AANRpSURBVHhe7J0FmBTX0oZ7F1jc3d3d
nU+dOtXw4MGDZfft25fmyJEjPu+//76Xqzke8pCHPOQhD3lIQGxxcGSapvHdd9+FF3hHEdjGFHgn
....... Many more lines of string .......
OqelFQzDMAzD/CZoztADbUwhUm0JERoXCNfEQYhyPAQryiBIUQSBiiTwl1sb3skwDMMwzG+KWLVe
jTEBH6U7JvJ0CJQXoaGPQMWBm5W+Va27k14MwzDMfwCA/wfUstOLO+nBIAAAAABJRU5Jggg==')
Do this will mean that your image is stored within the code already and doesn't need to be loaded from somewhere.
Note: Make sure that the pictures size on disk is the smallest you can get it as producing the string can take sometime and could turn out thousands of lines long. So I would recommend that you only use a pic that is less that 75 Kilobytes in size. You could do it with larger one but this will take a long time to process.

Get real path for google images

I am trying to fetch google trends page and the images that are shown are something in the code are actually like this
http://t2.gstatic.com/images?q=tbn:ANd9GcReZ5l9k236B8fRJQo2XuoaB30s-4wsUPZEYOWurvMjArDatu0vN_z2pHt4VAn_7Za_6xozCU3W
Since i need the real path to the image with its extension, is there any way to retrieve it?
Thank you.
First off, this sounds like you're trying to screenscrape. Your life might be easier if you can review the API for google-trends and use that instead (but I don't know that api well enough to help you on that front; I did add the google-trend (in a to be peer reviewed edit) to your post to try to attract who do.)
Having said that, can you download the image and look at the content-type (assuming that image doesn't redirect to the underlying image in which case your problem should be solved.) You didn't specify what language you were using, so I'm going to assume you're using the right one :).
Example code (python, using requests library):
import requests
r = requests.get("http://gstatic.foo.com/blah/randomkeyboardtypingdetected/")
ctype = r.headers.get("content-type",None)
lookup = {"image/jpeg":"jpg","image/png":"png"} # add others as needed
if ctype and lookup.get(ctype,None):
print lookup.get(ctype)
else:
print "Error, server didn't specify.
It is a real path, image is retrieved dynamically so path is not like regular one with file-name, extension etc.
You can check content type from response headers if you want to determine type of image.

ExpressionEngine: File Manager

I’m new to EE and trying to learn the basics. Some questions about the File Manager:
I upload a photo and put “cat, kitten” in the description. When I do a search for “kitten”, it finds the photo. But when I do a search for “cat”, I get nothing. Any ideas what’s going on?
The file metadata are: file title, file name, description, credit, and location. What if I wanted to add custom fields? How do I do that?
In the template files, how do I access a particular manipulation (I call this “rendition”) of an image? Say I define a rendition “thumbnail” to be 100x100. How do I access that particular rendition in a template?
Is there a way to randomize the file names of the files being uploaded?
After uploading an image and testing it against PageSpeed, it turns out that the image can still be optimized via losslessly compressing it. How can this problem be addressed?
Ah, the file manager. Not EE's brightest spot.
It would not surprise me if the search in the File Manager was not
very robust. I'd try more variations to narrow it down (what kind of
characters affect the results - commas, dashes, spaces, etc ... do
partial terms match?)
You cannot currently add custom metadata to files in the file manager.
Use this syntax: {field_name:rendition}, e.g.,
{my_image:thumbnail} (docs).
Nope.
EE just uses the GD library available in your PHP install to resize
images. If you want the highest possible optimization, you'll have
to do your image manipulations yourself.
Given your queries, I would suggest you have a look at Assets by Pixel and Tonic. It offers a far superior file management experience on most of these fronts.

how do i create a grayscale image from non image data

I have a array containing data. This array contains only image data/ or it can be just random data. No header information is available. So writing this to a file and making its extension as jpg is not going to work. Can someone please recommend a library that would do this for me.
Any language that isnt a scripting language is ok. Any environment. I would prefer if its in C/Java/Matlab.
If you have your array in MATLAB (let's say it's in a variable called im), then you can just type
imwrite(im, 'myfilename.bmp', 'bmp')
and your array will be written to a .bmp file. You can choose from a number of other common formats too. See the documentation for imwrite.
You can even write random data in this way:
a = rand(100,100);
imwrite(a,'testimg.jpg','.jpg')

Reading PDF files as string through iPhone application

I am confuse that what argument should i pass in CGPDFDictionaryGetString function for "key"?I want to extract text and image from PDF file.
The method you have specified is normally used for extracting a String COS object, and will probably be of little direct use in getting the text off the PDF page. COS objects are stored within the PDF's document catalog tree. You normally acquire a COS object in the tree by using its key value. COS objects can be of several different types (Dictionary, Array, Number, String, Stream etc.) each type is identified with a key that allows it to be identified and retrieved via methods like:
CGPDFDictionaryGetString(key)
CGPDFDictionaryGetNumber(key)
CGPDFDictionaryGetDictionary(key)
I've never had the need to extract the on-page text myself, but looking over a simple PDF file, the on-page text seems to be in the page's "Contents" stream.
So in your case you probably want to do something like
1) Get the Document Catalog
2) Get the 'Pages' Dictionary
3) Get Page(n) that you are concerned with
4) Get that page's "Contents" stream and parse it for the text.
Images are normally stored under the page's "Resource" dictionary (which resides at the same level as the "Contents" stream.
If you want to get a better understanding of the COS object tree and its structure, you can view it for the currently viewed PDF using Acrobat's "Preflight" utility. Under the Advanced menu: Preflight... | options | Browse Internal PDF structure...
And of course, flipping through the official spec is a good Idea:
Hope that helps!

Resources