imagemagick no output - php

here is my code:
<?php
error_reporting(E_ALL) ;
$orig = "original.jpg";
$out = "output.jpg";
$cmd = "-sepia-tone 90%";
exec("/usr/bin/convert $orig $cmd $out ");
?>
After I run this, I expected a sepia toned version of original.jpg named output.jpg. when I check the directory, output.jpg doesnt exist.
What am I missing?

<? php should be <?php.

Related

How to run powershell command in PHP

I want to run powershell through PHP program. In powershell I have to run the command "Get-FileHash -Algorithm sha256 .\SHYAM.jpeg" to generate hash value. Shyam.jpeg is a file which is located in the directory C:\wamp64\www\Dedup. Here is my code
<?php
$psPath = "C:\\Windows\\SysWOW64\WindowsPowerShell\v1.0\\powershell.exe";
$psDIR = "C:\\wamp64\\www\\Dedup";
$psScript = "SHYAM.JPEG";
$runScript = $psDIR. $psScript;
$runCMD = $psPath.'Get-FileHash -Algorithm sha256./'.$psDIR.$psScript;
$out= shell_exec($runCMD);
echo "<pre>";
print_r($out);
echo "</pre>";
?>
But it is not working. I am struggling to make it work. When I tried "Get-FileHash -Algorithm sha256 .\SHYAM.jpeg" command in powerShell it works fine. Please help me out.
The output is C:\Windows\SysWOW64\WindowsPowerShell
v1.0\powershell.exeGet-FileHash -Algorithm sha256./C:\wamp64\www\DedupSHYAM.JPEG
Missing a backslash escape in the path to PowerShell, no spaces, wrong folder path, it's not going to work.
Try something like:
<?php
$psPath = "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe";
$fileDir = "C:\\wamp64\\www\\Dedup";
$fileName = "SHYAM.JPEG";
$runCMD = "$psPath -Command \"Get-FileHash -Algorithm SHA256 -Path '$fileDir\\$fileName' | Select-Object -ExpandProperty Hash\"";
$out= shell_exec($runCMD);
echo "<pre>";
print_r($out);
echo "</pre>";
?>
I haven't tried to exec it, but that at least prints a sensible looking command.
Replace the ./ with a space and add a space before the Get-FileHash.
$runCMD = $psPath.'Get-FileHash -Algorithm sha256./'.$psDIR.$psScript;
To
$runCMD = $psPath.' Get-FileHash -Algorithm sha256 '.$psDIR.$psScript;

How to trim silence from audio with ffmpeg in php

My hosting does not have ffmpeg's silenceremove filter, so I'm trying to remove the beginning and end silence of an audio file with silencedetect then cut. I'm trying to do this programmatically with php. At this point, I can't even detect the silence. I've tried:
define('UPLOAD_DIR', $_SERVER['DOCUMENT_ROOT'].'/audiofiles/');
$filein = UPLOAD_DIR . "music.ogg";
$fileout = UPLOAD_DIR . "music_no_silence.ogg";
$fileouttxt = UPLOAD_DIR . "music_log.txt";
$ffmpeg = "/usr/local/ffmpeg/bin/ffmpeg";
I first tried to put the ffmpeg command into a variable and then tried to access the output but was unable:
$output = shell_exec("$ffmpeg -i $filein -af silencedetect=n=-50dB:d=1");
echo $output;
I then tried to create a.txt log of the conversion but the .txt file doesn't show anything about the silencedetect. Furthermore, how would I be able to extract the info that I need programmatically from the .txt log in order to use it in php?
shell_exec("$ffmpeg -i $filein -af silencedetect=n=-50dB:d=1 $fileout 2> $fileouttxt");
I'm basically trying to adapt the answer at https://stackoverflow.com/a/25698675 into php
Edit: Here's kind of a hack way to do it:
define('UPLOAD_DIR', $_SERVER['DOCUMENT_ROOT'].'/audiofiles/');
$filein = UPLOAD_DIR . "music.ogg";
$fileout1 = UPLOAD_DIR . "music_out1.ogg";
$fileout2 = UPLOAD_DIR . "music_out2.ogg";
$ffmpeg = "/usr/local/ffmpeg/bin/ffmpeg";
$command = "$ffmpeg -i $filein -filter_complex silencedetect=n=-10dB:d=0.5 -y $fileout1 2>&1";
$output = array();
exec($command,$output);
$searchword1 = 'silence_end';
$endmatches = array_filter($output, function($var) use ($searchword1) {return preg_match("/\b$searchword1\b/i", $var);});
$endmatches = array_values($endmatches);
$parts1 = explode(" ",$endmatches[0]);
$a = $parts1[4];
$a = $a-.5;
$searchword2 = 'silence_start';
$startmatches = array_filter($output, function($var) use ($searchword2) {return preg_match("/\b$searchword2\b/i", $var);});
$startmatches = array_values($startmatches);
$parts2 = explode(" ",end($startmatches));
$b = $parts2[4];
$b = $b+.5;
$c = $b-$a;
exec("$ffmpeg -i $fileout1 -ss $a -t $c -y $fileout2 && rm $fileout1");
PHP's shell_exec returns the output written to stdout and ffmpeg writes the messages to stderr. If you want to use shell_exec you must redirect the stderr output to stdout using 2>&1.
You also need to specify at least one output file for ffmpeg, in this case /dev/null.
set_time_limit(0);
$input_file = 'music.ogg';
$cmd = "ffmpeg -i {$input_file} -af silencedetect=n=-50dB:d=1 -f null /dev/null 2>&1";
$output = shell_exec($cmd);
if (preg_match_all("/^\[silencedetect\s[^]]+\]\s([^\n]+)/m", $output, $matches)) {
var_export($matches[1]);
}
An alternative is to use proc_open and stream_get_contents.

Using ImageMagick to resize images from command line in PHP

I'm using ImageMagick to generate a small thumbnail of images. convert --version shows ImageMagick 4.2.9 99/09/01.
$output = array();
$cmd = "/opt/RZmagick4/bin/convert data/test.jpg data/small.png"; // works fine
$cmd = "/opt/RZmagick4/bin/convert data/test.jpg -resize 300x200 data/small.jpg"; // does not work
exec($cmd, $output);
echo "<pre>";
print_r($output);
echo "</pre>";
Why does the second $cmd not work? Output for both is Array( ). However, there is just no output image.
You should write this:
$cmd = "/opt/RZmagick4/bin/convert -resize 300x200 data/test.jpg data/small.jpg";

allow php invoked by exec() to write to console

I invoked a php file by exec() doing this :
<?php
$cmd = "php -q nah.php";
exec($cmd);
echo "lalal";
?>
and the nah.php has this :
<?php
echo "yaya";
sleep(3);
?>
It does sleep for 3 seconds but the file can echo out to the command.
How can I do echo the output from nah.php
If you want to capture the output of another process, then you can use backticks
$output=`command line`;
or capture it with exec()
exec('command line', $output);
However, both of these techniques only give the output when the external process has run to completion. If you want to grab the output as it happens, you can use popen (or proc_open for more control), e.g. something like this
$handle = popen('command line 2>&1', 'r');
while (!feof($handle))
{
$read = fread($handle, 2096);
echo $read;
}
pclose($handle);
The 2>&1 at the end of the command line is a handy idiom if running within a shell like bash. It redirects stderr to stdout, so any errors the command generates will be returned to PHP. There are more advanced ways to capture stderr, this just makes it easy.
Change your first script to:
<?php
$cmd = "php -q nah.php";
echo `$cmd`;
echo "lalal";
exec() creates a new console and executes the php file there, it returns the output in an array (One element per line return) that is passed in as a second argument. So if you change your code to:
<?php
$output = array();
$cmd = "php -q nah.php";
exec($cmd, $output);
$lines = count($output);
for($i = 0; $i < $lines; $i++)
echo $output[$i];
echo "lalal";
?>

wscript.shell running file with space in path with PHP

I was trying to use wscript.shell through COM objects with php to pass some cmd commands to cURL library (the DOS version). here is what I use to perform this task:
function windExec($cmd,$mode=''){
// Setup the command to run from "run"
$cmdline = "cmd /C $cmd";
// set-up the output and mode
if ($mode=='FG'){
$outputfile = uniqid(time()) . ".txt";
$cmdline .= " > $outputfile";
$m = true;
}
else $m = false;
// Make a new instance of the COM object
$WshShell = new COM("WScript.Shell");
// Make the command window but dont show it.
$oExec = $WshShell->Run($cmdline, 0, $m);
if ($outputfile){
// Read the tmp file.
$retStr = file_get_contents($outputfile);
// Delete the temp_file.
unlink($outputfile);
}
else $retStr = "";
return $retStr;
}
now when I run this function like:
windExec("\"C:/Documents and Settings/ermac/Desktop/my project/curl\" http://www.google.com/", 'FG');
curl doesn't run because there is a problem with the path. but when I remove the spaces from the path it works great.
windExec("\"C:/curl\" http://www.google.com/", 'FG');
so my question is how can I escape these spaces in wscript.shell commands?
is there anyway I can fix this?
thanks in advance :)
nvm I found a solution:
there:
windExec("cd C:/Documents and Settings/ermac/Desktop/my project/libs & curl.exe -L http://www.google.com/", 'FG');

Categories