How to run cron job with PHP with Imagick? - php

I have a php script that uses imagick to get image colors among other things.
php /home/username/public_html/cron.php
I would get this error on the scheduled run
[30-Apr-2013 00:00:02] PHP Fatal error: Class 'Imagick' not found
in /home/username/public_html/cron.php on line 113
A sample of the php code:
try {
$image = new Imagick($filename);
$image->scaleImage(1,1);
$pixel = $image->getImagePixelColor(1, 1);
$color = $pixel->getColor();
$image_r = $color['r'];
$image_g = $color['g'];
$image_b = $color['b'];
$image->destroy();
} catch (ImagickException $e) {
// something went wrong, handle the problem
$image_r = 0;
$image_g = 0;
$image_b = 0;
}
Do I need set up some environmental variable so it knows where to look for imagick?
It works fine if I invoke the php script from the browser. Hopefully I can move this script out of the public_html when I have it working so I don't get a third party calling it.
thanks.

You need to set path of your imagemagick something like this used to work for me
imagemagickpath = /usr/bin/imagemagick
in some config file
and then use that imagemagickpath

Assuming it works in non cron environment, I notice your script is in the web root. how about using wget instead:
wget http://localhost/cron.php

Related

How to schedule a task that runs php using window task scheduler?

I've been trying to run a php file using window task scheduler. I've tried using the .bat file, but it won't work.
Here's what I've done:
I've created a task "Download Image"
I've created a .bat file, and the content is:
"C:\xampp\php\php.exe" -f "D:\server\newxml\download.php";
The php file that I want to run is:
ini_set('max_execution_time', 300);
$doc = simplexml_load_file('xml_edit_feeds.xml');
foreach ($doc->xpath("//item") as $item) {
$name = $item->productname;
$realname = preg_replace('/\s/', '',$name);
$url = $item->thumbnail_url;
$img = 'D:/server/newxml/imagethumbnail/'.$realname.'.png';
$filename = 'D:server/newxml/imagethumbnail/'.$realname.'.png';
file_put_contents($img, file_get_contents($url));
}
?>
This is the screen shoot of the windows task scheduler:
Is there something wrong from what I did? Thank you for your help
I didn't try this before, but I think you should use a browser exe program to open your webpage instead of php.exe.
Eureka! I got the answer to this, so, I what I've done is:
Set the Program/Script to: C:\xampp\php\php.exe
Set the arguments to: D:\path\to\php.php (In my case D:\server\newxml\download.php)
Set the start-in to the folder that contains download.php (php file that you want to run). In my case: D:\server\newxml\

Starting external programme from PHP

I am running in a Windows10 environment. My php script writes a pdf file and I simply want to have the file open up in the pdf viewer that I specify. I am using the "runAsynchronously" function given in the PHP manual and I have tried many variations. I have no problem getting the process to run in the background - it appears every time in my TaskManager process listing, but no window appears - what am I doing wrong? If I double click the link file that has been written it works fine. It is nothing to do with the path to the executable or the filename - I can replace the pdf viewer with "notepad.exe" and the $file with a suitable text file - the same thing happens, notepad appears as a process, but not as a window, and the link works fine.
Here are some code snippets
$cmd = "C:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe";
runAsynchronously($cmd, $file, 7, null, true);
function runAsynchronously($path, $arguments, $windowstyle=1, $lnkfile=null, $exec=true) {
$tmp = (is_null($lnkfile)) ? 'C:\temp\temp.lnk' : $lnkfile;
try {
if(file_exists($tmp)) { unlink($tmp); }
$WshShell = new COM("WScript.Shell");
$oShellLink = $WshShell->CreateShortcut($tmp);
$oShellLink->TargetPath = $path;
$oShellLink->Arguments = $arguments;
$oShellLink->WorkingDirectory = dirname($path);
$oShellLink->WindowStyle = 1;
$oShellLink->Save();
$waitforcompletion = false;
if($exec) {
// Run kicks off the process in the background, but no window gets opened
$oExec = $WshShell->Run($tmp, $windowstyle, $waitforcompletion);
unlink($tmp);
} // if not executed link is left available for manual running
unset($WshShell,$oShellLink,$oExec);
} catch(Exception $ex) {
print $ex->getMessage();
}
}
If opening in the browser a pdf or downloading it solves your problem, try a class called PDFMerger:
https://github.com/clegginabox/pdf-merger
It uses an API called setasign FPDI and FPDF (https://packagist.org/packages/setasign/fpdi-fpdf#p-456)
I tested it here, because it also interested me and worked ok. (Wamp in Windows 8.1)
I put everything together(classes and folders of fpdi and fpdf) in a folder called pdfmerger because I was having some difficulty with namespaces and my code worked ok, but I know this is not the best way to include the classes - the example in GitHub page must help if you have problems with classes not finding classes):
<?php
include "pdfmerger/fpdf.php";
include "pdfmerger/fpdi.php";
include 'pdfmerger/PDFMerger.php';
$pdf = new PDFMerger();
//generate a pdf with the pages 1,2 and 3 and send to browser
$pdf->addPDF('originalpdfsavedintheserver.pdf', '1,2,3')
->merge('browser', 'nameinbrowser.pdf');
// REPLACE 'browser' WITH 'file', 'download', 'string', or 'browser' for output options
If you use the option 'download' you can choose the program that will open the pdf file in your windows settings(Choose default program to open pdfs):
https://www.cnet.com/how-to/how-to-set-default-programs-in-windows-10/

is it possible to run assimp commandline tool with the php

I used following command to convert the 3d models with the assimp Assimp, and it is working fine on Windows:
assimp2json seaknight.obj seaknight.json
I need to know how can I run this command from the PHP? I know that there re functions to run the shell execution from PHP, but it didn't work and I don't get any error.
PHP code is used is follows.
system("D:\assimp2json-2.0-win32\Release\assimp2json.exe assimp2json seaknight.obj seaknight.json");
and another one is
$old_path = getcwd();
chdir('D:\assimp2json-2.0-win32\Release');
$output = shell_exec('assimp2json.exe assimp2json seaknight.obj seaknight.json');
chdir($old_path);
Found it myself
working code is below
$old_path = getcwd();
chdir('D:\assimp2json-2.0-win32\Release');
$output = shell_exec('assimp2json monster.blend monster.json');
chdir($old_path);
no need to include the .exe, after remove it the command worked

Gettin error when running simple php script

I'm trying to run this simple script:
<?php
$PHP_PATH = "c:\Program Files (x86)\PHP\\";
$CLIENTPATH = dirname(__FILE__)."\Client.php";
$SERVER = "http://localhost:8080/mobile/Server";
$handle = popen("C:\WINDOWS\system32\cmd /C start ".$PHP_PATH." -f ".$CLIENTPATH." ".$SERVER, 'r');
?>
But I always get this Windows messagebox error:
Windows cannot find c:\program. Please make sure you typed the name correctly and then try again.
Searching on google I also find this thread about this error, but the meassures are I litlle drastic I guess.
So the problem its in my code ? Or there can be something else wrong ?
Thanks.
You need to escape the whitespaces in $PHP_PATH = "c:\Program Files (x86)\PHP\\";!

Doc to PDF with PHP + Openoffice

I am trying to follow a tutorial on converting doc to pdf using openoffice. I have the following code:
<?php
set_time_limit(0);
function MakePropertyValue($name, $value,$osm){
$oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$oStruct->Name = $name;
$oStruct->Value = $value;
return $oStruct;
}
function word2pdf($doc_url, $output_url){
// Invoke the OpenOffice.org service manager
$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
// Set the application to remain hidden to avoid flashing the document onscreen
$args = array(MakePropertyValue("Hidden",true,$osm));
// Launch the desktop
$top = $osm->createInstance("com.sun.star.frame.Desktop");
// Load the .doc file, and pass in the "Hidden" property from above
$oWriterDoc = $top->loadComponentFromURL($doc_url,"_blank", 0, $args);
// Set up the arguments for the PDF output
$export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
// Write out the PDF
$oWriterDoc->storeToURL($output_url,$export_args);
$oWriterDoc->close(true);
}
$output_dir = './';
$doc_file = './test.docx';
$pdf_file = 'DpmR5Reqv1.20.pdf';
$output_file = $output_dir . $pdf_file;
$doc_file = 'file:///' . $doc_file;
$output_file = 'file:///' . $output_file;
word2pdf($doc_file,$output_file);
?>
I get the error:
Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `com.sun.star.ServiceManager' in C:\wamp\www\Projects\doc_to_pdf\index.php on line 11
( ! ) com_exception: Failed to create COM object `com.sun.star.ServiceManager': Invalid syntax in C:\wamp\www\Projects\doc_to_pdf\index.php on line 11
Ive tried to what this tutorial suggests: http://puno.ayun.web.id/2009/08/php-ooo-in-microsoft-windows-environment/ But no luck. Any idea what I can do? I am running this under wamp and it will be ran under wamp in production.
You have to have OpenOffice setup to run as a service on that machine. To simply convert an odt to a pdf you can use pyodconverter. They also explain how to setup the local OpenOffice service:
http://www.artofsolving.com/opensource/pyodconverter
I'm using this technique in a script I wrote and have an article for here:
http://codeuniversity.com/scripts/scr1
Please install Open Office in your Directory. OpenOffice setup to run as a service on that machine.
it is much easier to use headless libreoffice and a php wrapper class like https://github.com/ncjoes/office-converter.
of course you have to install libreoffice and you must have full control of your webserver.

Categories