Php exec command pdftoimage not work - php

I have problem with my code.
My Code look like this:
$destinationFolder = $destinationRootFolder . '/';
// mkdir($destinationFolder,777);
$options = $this->buildOptions($saveAsJpeg, $inputPdf, $destinationFolder);
print_r($options);
// exit;
try {
$command = "/usr/bin/pdfimages ".$options[0]." ".$options[1]." ".$options[2];
echo $command;
// exit;
shell_exec($command);
exec($command);
// $command;
// echo $r;
} catch (ExecutionFailureException $e) {
throw new RuntimeException('PdfImages was unable to extract images', $e->getCode(), $e);
}
code entered first command before it executes it. When the copy command to the console everything works well but does not create php files png.
edit
root#mat-K50AB:~# php -a
Interactive mode enabled
php > ls
php > exec("/usr/bin/pdfimages -png /path/pdf/file.pdf /tmp/savefile/")
php > shell_exec("/usr/bin/pdfimages -png /path/pdf/file.pdf /tmp/savefile/")
php >
It also does not work

It sounds like the apache does not have permissions to run it, A few things to check
1) ( if CentOS/RHEL ) Is selinux stoping it, TO temporarly disable it
setenforce 0
Perminetly allow it ( Replace /usr/bin/pdfimages with all files that need access )
chcon -v --type=httpd_sys_content_t /usr/bin/pdfimages
2) Not executible by apache, Try
chmod +x /usr/bin/pdfimages
If nether of thoughs work, What os is your server running?

Related

Can't Use ImageMagick (convert) command in PHP

I want to edit images using ImageMagick and PHP on my website.
I'm on Linux (Raspbian OS) and have PHP and ImageMagick installed.
I'm trying to do something similar to: `convert test.png test.webp` using PHP. All work in the Terminal.
What I've tried:
putenv("PATH=/usr/local/bin:/usr/bin:/bin");
Full command path "/usr/bin/convert test.png test.webp" (Work in Terminal)
All of the PHP CMD commands including: exec() shell_exec() system() passthru()
(All of the output of those were empty, but simple commands like echo $PWD worked)
Images Full Path "convert /home/pi/www/test.png /home/pi/www/test.webp" (And all of the above variations)
Any help in hugely appreciated!
Use 2>&1 in php shell_exec command and try this code;
$file = "/home/pi/www/test.png";
$dest = "/home/pi/www/test.webp";
$result = '';
$file_extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if(extension_loaded("cwebp") || extension_loaded('gif2webp')) {
if($file_extension=="png" or $file_extension=="jpg" or $file_extension=="jpeg"){
$result = shell_exec("cwebp -q 80 ".escapeshellarg($file)." -o ".escapeshellarg($dest) . " 2>&1");
}
if($file_extension=="gif"){
$result = shell_exec("gif2webp -q 80 ".escapeshellarg($file)." -o ".escapeshellarg($dest) . " 2>&1");
}
} else if(extension_loaded("gmagick") || extension_loaded('imagick')) {
if($file_extension=="png" or $file_extension=="jpg" or $file_extension=="jpeg" or $file_extension=="gif"){
$result = shell_exec("convert ".escapeshellarg($file)." -quality 80 -define webp:lossless=true ".escapeshellarg($dest) . " 2>&1");
}
} else {
die("Does not have any webp library!");
}
echo $result;
I don't know your version of Imagick, some arguments may vary by version, but it might give some idea for your code.
You have also an imagick extension in PHP: https://www.php.net/manual/fr/book.imagick.php

Use PHP exec shell (codesign) resign IPA on Mac

I wrote some code to package channel IPA and resign IPA with shell. When I run this shell in command line it works ok.
Main shell code:
#!/bin/bash
name=$1
needchannel=$2
appName="$name.app"
ipa="$name.ipa"
plistName="${name}_${needchannel}.plist"
# echo "${1} ${2} $ipa"
if [ ! -f "./Payload" ];then
# echo "not exit Payload"
unzip $ipa
chmod -R 0777 Payload
else
echo "exit Payload"
fi
cd Payload
cd $appName
ipaPath="${name}_${needchannel}.ipa"
/usr/libexec/PlistBuddy -c "set Channel $needchannel" "Channel.plist"
cd ../..
/usr/bin/codesign -f -s "iPhone Distribution: xxxx, ooo" --entitlements entitlements.plist "Payload/${appName}"
zip -rq "./app/${ipaPath}" "Payload"
PHP code:
<?php
$webid = $_GET["webid"];
$appfinder = "./app/";
$filename = "Channel_".$webid.".ipa";
if (!file_exists($appfinder.$filename))
{
$shell = "sh produceApp.sh Channel $webid";
system($shell, $status);
if( $status ){
echo "produceApp error";
var_dump($status);
return;
}
}
?>
I don't know why the shell run success in command line and fail in PHP.
Can anybody help me.
Everything works except codesign.
ps:i write the shell code in php,run directly,system() return 1;like this:
<?php
$shell = "/usr/bin/codesign -f -s \"iPhone Distribution: xxx, ooo\" --entitlements entitlements.plist \"Payload/Channel.app\"";
echo "$shell";
echo "<br>";
system($shell, $status);
print_r($status);
?>
the chrome show shell is:
/usr/bin/codesign -f -s "iPhone Distribution: xxx, ooo" --entitlements entitlementds.plist "Payload/Channel.app"
and function system() return $status is 1..
web show result of system run codesign...
The problem is in path! Log the path when you run php and correct it.

exec pdfimages in PHP

I have full root access through VPS (CentOS).
In Unix shell I am able to extract images with following command:
pdfimages -j xyz.pdf images
But I am unable to execute the command through PHP
exec ("pdfimages -j xyz.pdf images");
xpdf is installed. Also I have checked that // Exec function exists; // Exec is not disabled; // Safe Mode is not on.. by using following code:
$exec_enabled =
function_exists('exec') &&
!in_array('exec', array_map('trim', explode(', ', ini_get('disable_functions')))) &&
strtolower(ini_get('safe_mode')) != 1;
if($exec_enabled) { echo "enabled"; }
The following is however getting executed properly:
exec("ls -1 *.php", $output);
foreach ($output as &$tmp){
echo "$tmp<br>";
}
What am I doing wrong? Where is the issue?
You need to specify the path like below , this will save each image in the folder of your PDF.
'image' is just the file name ..
$path = 'path/to/your/folder/';
$pdf = $path.'xyz.pdf';
$command = 'pdfimages -j '.$pdf.' '.$path.'image';
exec($command);

shell script works fully in terminal but not when run by php

I have a shell script that uses unoconv and then pdftk. when i run the script through the command line it works exactly how i want it to. When i use shell_exec($cmd) in php with the same exact command it runs the script (i know because of the echo's in the script) but it looks like it does not use unoconv (and therefore cannot use pdftk). Any idea on how to troubleshoot this problem? here some code:
if(isset($_FILES["file"]["name"]) && !empty($_FILES["file"]["name"])){
$fname = $_FILES["file"]["name"];
$tmp_name = $_FILES["file"]["tmp_name"];
$dir = "powerpoints/".$name."/";
$ispdf = "1";
$output = shell_exec('mkdir '.$dir);
chmod($dir, 0777);
echo $output;
if(move_uploaded_file($tmp_name, $dir.$fname)){
chmod($dir.$fname, 0777);
$cmd = 'importppt.sh '.$name.' '.str_replace(".ppt", "", $fname);
echo "\n".$cmd;
$output = shell_exec($cmd);
echo $output;
}else{
$message = "move_uploaded_file() Failed";
}
and here is the shell script
#!/bin/bash
echo $1 ' is the argument:' $2 ' is the second '
STRING="/var/www/html/devclassroomproject/powerpoints/"
echo $STRING$1/$2'.ppt '
unoconv $STRING$1/$2'.ppt'
pdftk $STRING$1/$2'.pdf' burst output $STRING$1/$1'_%2d.pdf'
This is what is printed from echos:
importppt.sh pptest pptestpptest is the argument: pptest is the second
/var/www/html/devclassroomproject/powerpoints/pptest/pptest.ppt
edit:
to decipher my debugging
the command: "importppt.sh pptest pptest"
importppt.sh being the shell script; pptest is the first and second argument
printed by the first echo in the shell script: "pptest is the argument: pptest is the second"
printed by the second echo in the script verifying the complete path of the pdf which does exist: "/var/www/html/devclassroomproject/powerpoints/pptest/pptest.ppt"
sorry for the confusion
Found out what was wrong. the answer is here
http://johnparsons.net/index.php/2013/08/05/how-to-keep-unoconv-apache-from-making-you-sad/
basically you have to set up a home directory for the user for apache2 as www-data and change the path to the shell in the passwd file
he doesnt mention it but the changes will not work unless you restart apache

CutyCapt with php exec

I'am using CutyCapt on my CentOS.
It works fine via terminal but it doesn't work via php exec function.
I've started xvfb by command in terminal:
Xvfb :99 -screen 0 1024x768x24
And I'am trying to do a screenshot by php script:
exec("DISPLAY=:99 /path/to/cutycapt --url=<some url> --out=<path/to/output>");
It doesn't show any errors but there is no output file (output directory has chmod 777)
Can somebody help me?
Thanks
UPD:
Maybe it is better somehow to allow executing of Xvfb by Apache?
I've managed to get CutyCapt to run successfully using the php at the end. The $_parameters are passed via AJAX to the php script. Hope this helps...
case 'Output_Chart': {
// We always create the .png. We use the ImageMagick convert (IMC) command to make .pdfs
if ($_Suffix == 'pdf') {
$IMC = ";convert -page 735x850 '$_PathOut/$_ChartName.png' '$_PathOut/$_ChartName.pdf'";
} else {
$IMC = '';
}
// Prepare the query string for the CutyCapt URL
$sQuery_Pattern = '?Path=%s&iDL=%d';
$sQuery = sprintf($sQuery_Pattern, $_Path, $_iDL);
// Prepare CutyCapt's command and parameters (NB: query string and out parameter are enclosed in aposts for the shell)
$sCC_Cmd = '/var/www/LF/Includes/CutyCapt';
$sCC_URL = "http://localhost/LF/LFPrint.html'$sQuery'"; // Note: Inner apostrophes
$sCC_Out = "'$_PathOut/$_ChartName.png'";
$sCC_Pattern = ' --url=%s --out=%s --delay=%d --min-width=%d';
$sCC_Options = sprintf($sCC_Pattern, $sCC_URL, $sCC_Out, $_Delay, $_MinWidth);
//$sCC_CmdLine = $sCC_Cmd . $sCC_Options . " 2> CutyCapt.err.txt";
$sCC_CmdLine = $sCC_Cmd . $sCC_Options . " 2> /dev/null";
// Prepare the final command line with xvfb-run, CutyCapt, and the URL?QueryString
$sCC_CmdLine = 'xvfb-run --auto-servernum --server-args="-screen 0, 800x1000x24" ' . $sCC_CmdLine . $IMC;
exec( $sCC_CmdLine, $aOutput = array(), $ret);
// Wait for and then return the results. sCC_CmdLine and aOutput are just for debugging
echo json_encode(array("ret" => $ret, "cmd" => $sCC_CmdLine, "Output" => $aOutput));
break;
}

Categories