I'm trying to access Windows SAPI5 or Text to speech (TTS) using PHP. The standard approach is to create a COM object for "SAPI.SpVoice", then get the installed voices.
Sample PHP code:
<?php
$obj = new COM('SAPI.SpVoice');
$voices = $obj->GetVoices;
$count = $voices->Count;
print $count; #prints "1"
Unfortunately the output returned from PHP's COM object is incorrect because I have 5 voices installed on my system, but PHP only returns 1.
So, just to check if this a PHP specific issue, I wrote the same code in Perl 5.8 (strawberry).
Sample Perl code:
#!/usr/bin/perl
use Win32::OLE;
my $obj = Win32::OLE->new('SAPI.SpVoice');
my $voices = $obj->GetVoices;
my $count = $voices->Count;
print $count; #print "5" which is correct.
So the perl code correctly returns that I have 5 TTS voices on my system, but PHP returns only 1?
Is this a bug or am I doing something wrong? What could be the possible cause of this?
P.S. I've tried this on two different computers and results are the same.
I figured this after some trial error. It looks like if I use the 32-bit version of PHP then I get the correct results (5 voices). But since I had installed the 64-bit version by default I only get 1 voice.
I think the TTS voices are mostly 32 bit (like those installed on my system) and so when running with a 64-bit php.exe it only returns 64-bit voices. With 32 php.exe it returns all voices.
Posting this as answer in case someone faces a similar issue in future.
While converting from one timezone to another the following codes works well with PHP v7.0(dev) but throws an error when executed in PHP v5.4. How can I make it compatible?
//---CODE------------------------------------------------------------
$time_object = new DateTime('2011-04-19 17:45', new DateTimeZone('+0530'));
$time_object->setTimezone(new DateTimeZone('Europe/London'));
$LondonDateTime = $time_object->format('Y-m-d H:i:s');
echo $LondonDateTime;
Error in PHP v5.4
//---ERROR WITH PHP v5.4--------------------------------------
DateTimeZone::__construct(): Unknown or bad timezone (+0530)
It's down to this PHP bug, which was fixed in PHP 5.5.10. You should be able to use this (admittedly quite ugly) workaround:
DateTime::createFromFormat('O', '+05:30')->getTimezone();
I'd like to use imagemagick to make a montage from existing photos on my server. I am having a terrible time getting any code to work properly. I've tried using the few examples I've found by searching, but it's not working.
http://us1.php.net/imagick outlines the library beautifully, but there are no montage examples. I'd just need a proper example to get me off on the right foot and I' can usually take it from there.
Does anyone know of a good resource to learn imagick for PHP?
Here is a working example of montageImage. The example does a color analysis of a photo. To make sure it works properly, you will need to make sure:
You have a valid "test.png".
Imagick::clone has been deprecated as of imagick 3.1.0 in favour of using the clone keyword. So if you have imagick 3.1.0, you will need to change lines 13 and 17.
$bright = $average->clone();
to
$bright = clone $average;
and
$dark = $average->clone();
to
$dark = clone $average;
Good luck.
Did you start with the basic example?
http://us1.php.net/manual/en/imagick.examples-1.php
I am writing a wrapper library for WIM files (long story), but I have been having difficulty with the dates encoded in it. The format is:
[creationtime] => Array
(
[highpart] => 0x01CA0446
[lowpart] => 0x8E44DCAF
)
This format is inside the WIM file XML.
I have tried every date decoding technique I can think of!
Any ideas would be much appreciated.
The only things I have to go on are things related to Long Integers and WIN32 DateTime... But no luck so far.
liCreationTime is defined as LARGE_INTEGER in the WIM spec (I downloaded from MS)
This looked to me like it would be similar (if not the same) as a timestamp returned by QueryPerformanceCounter - which thankfully makes things very simple!
I did this using a calculator and command line PHP, you'll have to implement the code yourself.
Take your high and low parts and turn them into a single 64bit value.
0x01CA04468E44DCAF -> 128920240414579887
Divide that by 100000000
1289202404
Use it as the input to date()
php -r "var_dump(date('Y-m-d H:i:s', 1289202404));"
string(19) "2010-11-08 07:46:44"
Edit:
Or maybe I just got lucky.
If it is the number of nanoseconds since 1601, as other answers seem to indicate, you can use a slightly different calculation.
php -r "var_dump(date('Y-m-d H:i:s', (128920240414579887/100000000)-41651963));"
string(19) "2009-07-14 05:47:21"
I believe they might be something to do with the c/c++ FILETIME structure
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx
I relogin to my server in dreamhost and test some scripts.And I found I couldn't use str_split. Message of Undefined function was given.I checked the version in the server and its PHP Version is 5.2.12.And I just wonder which version is required?Thanks.
Testcode:
<?php
$arr = str_split("lsdjflsdjflsdjflsdjfl");
print_r($arr);
?>
Message:
Fatal error: Call to undefined function: str_split() in /test.php on line 3
Edit #Justin Johnson
I checked the server's system directory,and I found there are two versions of PHP in Dreamhost.In user's webroot,file will be parsed by PHP5 and that's why I got php 5.2.12 by putting a phpinfo.php in the webroot.And if php files are ran in command line directly using php test.php,another php version which is 4.x worked.That's the reason I got an error.When I use
/usr/local/php5/bin/php test.php
Everything is fine.
Rather than use str_split, it's usually much easier to iterate through the characters of the string directly:
$s="abc";
$i=0;
while(isset($s[$i])) {
echo $s[$i++]." ";
}
see?
First off: The PHP documentation will always say what version is required for every function on that function's documentation page directly under the function name.
It is possible that an .htaccess file is somewhere in your path and is causing a previous version (<5) of PHP to be used. To double (or triple) check to make sure that you are running in the proper PHP version, place this code above the line where you call str_split
echo "version:", phpversion(),
"<br/>\nstr_split exists? ",
function_exists("str_split") ? "true" : "false";
However, as shown by Col. Shrapnel, it is not necessary to convert a string to an array of individual characters in order to iterate over the characters of that string. Strings can also be iterated over using traditional iteration methods, thus making the call to str_split unnecessary and wasteful (unless you need to segment the string into fixed length chunks, e.g.: str_split($s, 3))
foreach ( str_split($s) as $c ) {
// do something with character $c
}
can be replaced by
$s = "lsdjflsdjflsdjflsdjfl";
for ( $i=0; isset($s[$i]); ++$i ) {
// do something with character $s[$i]." ";
}
which is equally, if not more clear.
According to dreamhost wiki, you need to switch to php5 manually from control panel, if you created your domain before 2008 sept.
http://wiki.dreamhost.com/Installing_PHP5#Using_DreamHost.27s_PHP_5
PHP 5 was added to all plans by
DreamHost as of June 2005. As of
September 2008, support for PHP4 was
discontinued, so you can no longer
switch back to PHP 4 from PHP 5 from
the panel.
If you haven't switched to PHP 5 yet,
you can do this in the Control Panel.
But, again, you will not be able to
switch back to PHP 4 after switching
to PHP 5.
Here's how to switch from PHP 4 to PHP
5:
Log into the DreamHost Control Panel.
Click Domains, then Manage Domains.
Click the wrench icon next to the domain you want to activate PHP 5
on (under the Web Hosting column).
Select PHP 5.x.x from the dropdown menu.
Click Change fully hosted settings now! at the bottom of the
section.
Repeat steps 3-5 for each additional domain you want to
activate.
you could also check your php version with
<?php
phpinfo();
?>
The version required is PHP 5 or later. So theoretically your program should work.
If you can't get str_split to work, just use a string as an array:
$stuff = "abcdefghijkl";
echo $stuff[3];
will produce
d
This method is fastest, anyway. I don't know if it suits your needs, but if it does, I hope it helps!
Could be anything in your code. How do we know its not a 10 line script or 2000 line script?
You can use preg_split() to split an array into single characters, but it will return an extra empty string at the begining and the end.
$a = preg_split("//","abcdefg");
echo json_encode($a);
prints:
["","a","b","c","d","e","f","g",""]