How to write lossless WebP files with PerlMagick - graphicsmagick

I'm trying to convert PNG files to lossless WebP in Perl with Graphics::Magick. Command line for that is:
$ gm convert in.png -define webp:lossless=true out.webp
My Perl code looks something like that:
use Graphics::Magick;
my $image = Graphics::Magick->new();
$image->Read("in.png");
my $image_data = $image->ImageToBlock(magick => "webp");
print $out_fh $image_data;
This code writes lossy WebP files perfectly, but how can I express the "-define" thing in terms of Perl API?
Thanks,
Update: looks like I need to call AddDefiniton API function (http://www.graphicsmagick.org/api/image.html#adddefinition). Looks like it's not exported via Perl API as of now.

I know it is of no help to you, but for those interested in how to do it in PHP, here is how:
$im = new \Gmagick($src);
$im->setimageformat('WEBP');
// Not completely sure if setimageoption() has always been there, so lets check first.
if (method_exists($im, 'setimageoption')) {
$im->setimageoption('webp', 'lossless', 'true');
}
$imageBlob = $im->getImageBlob();
$success = #file_put_contents($destination, $imageBlob);
For more webp options, check out this code

Related

Key binding by file type

Description
I'm trying to create a key binding that behaves differently based on the file type.
Ideally what id like to do is the following:
If the file type is .md then run the command markdown-preview-plus:toggle
else run the command script:run
I know it's something along the lines of:
file init.coffee :
editor.command('custom:command', e => {
if ( of file type .md) {
markdown-preview-plus:toggle
} else {
script:run
}
})
Then in the keymap.cson i have to add something like:
'atom-text-editor':
'cmd-i': 'custom:command'
But obviously this is pseudocode. I've tried reading the documentation specifically this
but there isn't enough information.
I was able to do this by adding the following to the keymap.cson file:
"atom-text-editor[data-grammar='source gfm']":
'cmd-i': 'markdown-preview-plus:toggle'
"atom-text-editor:not([data-grammar='source gfm'])":
'cmd-i': 'script:run'
For anyone trying to do something similar to this, I used this as reference:
Atom grammer syntax

How to copy and paste indd file content into an another indd file?

I'm newbie in indesign scripting stuffs.So I apologise as I couldn't post my trials.
Objective:
I have an indd document which will have figure caption,label etc. I need to copy content(figure which is editable) from other indd file to this document where related figure label exists.
For example:
sample.indd
Some text
Fig.1.1 caption
some text
I need to copy the content of figure1.indd and paste into the sample.indd document where Fig.1.1 string exists and so on. Now I'm doing it manually. But am supposed to automate this.
So, I need some hint how to acheive it using extendscript?
I have found something like below to do this, but I don't have any clue to develop it further and also am not sure whether this approach is correct to get my result. Pls help me
myDocument=app.open(File("file.indd"),false); //opening a file to get the content without showing.
myDocument.pages.item(0).textFrames.item(0).contents="some text";
//here I could set the content but I don't knw how to get the content
// ?????? Then I have to paste the content into active document.
I found the script for my requirement.
var myDoc = File("sample.indd");//Destination File
var myFigDoc = File("fig.indd");//Figure File
app.open(File(myFigDoc));
app.activeDocument.pageItems.everyItem().select();
app.copy();
app.open(File(myDoc));
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "FIG. 1.1 ";//Figure caption text
//app.findGrepPreferences.appliedParagraphStyle = "FigureCaption";//Figure Caption Style
myFinds = app.activeDocument.findGrep();
for(var i=0;i<myFinds.length;i++){
myFinds[i].insertionPoints[0].contents="\r";
myFinds[i].insertionPoints[0].select();
app.paste();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
If acceptable for you, you can place an indesign file as link (place…). So a script could try to catch the "fig…" strings and do the importation.
Have a look at scripts that use finGrep() and place() command.

How can i replace url from css

I'm trying to combine multiple css files into a single file (from many different folder in my host, and including external css files). I use the following short code:
Declare array:
$styles=array(
array(
'name'=>'style1.css',
'path'=>'http://mydomain.com/css/'
),
array(
'name'=>'camera.css',
'path'=>'http://mydomain.com/js/camera/'
),
array(
'name'=>'style3.css',
'path'=>'http://external.com/assets/css/'
));
Get content and replace url:
foreach ($styles as $style) {
echo preg_replace('/url\(\s*[\'"]?\/?(.+?)[\'"]?\s*\)/i', 'url('.$style['path'].'$1)', file_get_contents($style['path'].$style['name']));
}
After combined into one css file, i have some css background image url as follows:
url(ttp://mydomain.com/css/../image/background.png) //Internal path - Case 1A
url(http://mydomain.com/js/camera/../../image/background.png) //Internal path - Case 1B
url(http://external.com/assets/css/../../image/background.png) //External path - Case 2
Actual, the internal path (Case 1A, 1B) can display the background image (despite the lack of professionalism), but in the external path (Case 2) cannot display the background image, my question is:
How can i replace wrong path with correct path (REPAIR BASE ON CURRENT RESULTS) as:
url(http://mydomain.com/image/background.png) //Correct internal path
url(http://external.com/image/background.png) //Correct external path
(I understand the problem is if find a keyword containing '../' will remove keyword and remove 1 string before the keyword contain '*/', , but I can't figure out how to do this).
Thanks in advance.
(I have tried find out and tried searching for 1 week before ask new question).
additionally replace /up-level-directory/../ with /
that is '/([^\/]+)/../' with / globally
$correct_url = preg_replace('|/\w+/\.\./|', '/', $url);

Is there a standard way to diff du outputs to detect where disk space usage has grown the most

I work with a small team of developers where we share a unix file system to store somewhat large datasets. This file system has a somewhat prohibitive quota on it so about once a month we have to figure out where our free space has gone and see what we can recover.
Obviously we use du a fair amount but this is still a tedious process. I had the thought that we may be able to keep last months du output around and compare it to this months to see where we've had the most growth. My guess this plan isn't very original.
With this in mind I am asking if there are any scripts out there that already do this.
Thanks.
I wrote a program to do this called diff-du. I can't believe nobody had already done this! Anyhow, I find it useful and I hope you will too.
I really don't know if there is a standard way but I need it sometime ago and I wrote a small perl script to handle that. Here is the part of my code:
#!/usr/bin/perl
$FileName = "du-previous";
$Location = ">";
$Sizes;
# Current +++++++++++++++++++++++++++++
$Current = `du "$Location"`;
open my $CurrentFile, '<', \$Current;
while (<$CurrentFile>) {
chomp;
if (/^([0-9]+)[ \t]+(.*)$/) {
$Sizes{$2} = $1;
}
}
close($CurrentFile);
# Previous ++++++++++++++++++++++++++++
open(FILE, $FileName);
while (<FILE>) {
chomp;
if (/^([0-9]+)[ \t]+(.*)$/) {
my $Size = $Sizes{$2};
$Sizes{$2} = $Size - $1;
}
}
close(FILE);
# Show result +++++++++++++++++++++++++
SHOW: while (($key, $value) = each(%Sizes)) {
if ($value == 0) {
next SHOW;
}
printf("%-10d %s\n", $value, $key);
}
close(FILE);
#Save Current +++++++++++++++++++++++++
open my $CurrentFile, '<', \$Current;
open(FILE, ">$FileName");
while (<$CurrentFile>) {
chomp;
print FILE $_."\n";
}
close($CurrentFile);
close(FILE);
The code is not very error-tolerant so you may adjust it.
Basically the code, get the current disk usage information, compare the size with the lastest time it run (saved in 'du-previous'), print the different and save the current usage information.
If you like it, take it.
Hope this helps.
What you really really want is the awesome kdirstat.
For completeness, I've also found du-diff and don't see it mentioned in any other answer. Andrew's diff-du (mentioned in another answer) seems to be more advanced that this one.

Update configSource of XML element in web.config using Powershell by passing in Parameters

I am trying to figure out a way to update my web.config for different environments by updating the configSource for the appSettings element in the web.config.
Here are the way I know how to do it.
$xml.get_DocumentElement().appSettings.configSource = $replaced_test
The problem is that I want one base script where I can pass in different nodes to the script that I want to change and update but I am not sure how to do it.
For example, I want to be able to call a powershell script like this
changeWebConfig.ps1 nodeToChange newValueofNode
I hope this was clear enough.
This is the code I have now.
$webConfigPath = "C:\web.config"
# Get the content of the config file and cast it to XML
$xml = [xml](get-content $webConfigPath)
#this was the trick I had been looking for
$root = $xml.get_DocumentElement()."system.serviceModel".client.configSource = $replace
# Save it
$xml.Save($webConfigPath)
The problem I was having was the configuration node
I had to change it from
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
this to
<configuration>
I am not sure how to find the node with the configuration node in it's orginal state yet, but I'm getting closer.
function Set-ConfigAppSetting
([string]$PathToConfig=$(throw 'Configuration file is required'),
[string]$Key = $(throw 'No Key Specified'),
[string]$Value = $(throw 'No Value Specified'))
{
if (Test-Path $PathToConfig)
{
$x = [xml] (type $PathToConfig)
$node = $x.SelectSingleNode("//client[#configSource]")
$node.configSource = $Value
$x.Save($PathToConfig)
}
}
set-configappsetting "c:\web.config" CurrentTaxYear ".\private$\dinnernoworders" -confirm
Finally figured it out.
$root = $xml.get_DocumentElement().SelectSingleNode("//client[#configSource]").configSource = "test"
of course, I will replace "//client[#configSource]" with a variable so I can pass in different nodes as parameters to create my base script.
Im looking for a way to modify the code as well.
Here is a way you can view whats the node:
$path = 'c:\site\web.config'
$PublishState = (Select-Xml -Path $path -XPath "configuration/appSettings/add[#key='PublishState']/#value").Node.'#text'
$PublishState

Resources