So I'm currently using "Imagick" extension for a symfony project, but I get this error:
ClassNotFoundException: "Attempted to load class "Imagick" from the global namespace. Did you forget a "use" statement?"
Strangely, it's working fine in all native php script, but not working in symfony project!
I did some research and I found that I need to enable imagick for CLI also .. but I didn't find any method explain how to make it.
So, I verified Imagick installation by this code snippet:
<?php
header('Content-type: image/jpeg');
$image = new imagick("C:/wamp64/www/test/image.jpg");
$image->thumbnailImage(100,0);
echo $image;
and I can see the image loaded correctly.
Also, when I run this script from the browser:
<?php
if (extension_loaded('imagick')){
echo 'imagick is installed';
} else {
echo 'imagick not installed';
}
?>
I get :
imagick is installed
But when I execute this file from command-line interface, I get :
imagick not installed
In the Symfony project I get this error log:
Uncaught PHP Exception Symfony\Component\Debug\Exception\ClassNotFoundException: "Attempted to load class "Imagick" from the global namespace. Did you forget a "use" statement?"
Symfony function
private function createThumbnail($path, $dest, $width, $height)
{
$im = new \Imagick();
$im->pingImage($path);
$im->readImage($path);
$im->thumbnailImage($width, $height);
$white=new \Imagick();
$white->newImage($width, $height, "white");
$white->compositeImage($im, \Imagick::COMPOSITE_OVER, 0, 0);
$white->setImageFormat('jpg');
$white->writeImage($dest);
$im->destroy();
$white->destroy();
}
Environment
PHP Version => 7.1.9
System => Windows 10
Server => Wamp64
Symfony version => 3.1
Compiler => MSVC14 (Visual C++ 2015)
Architecture => x64
imagick module version => 3.4.3
ImageMagick version => ImageMagick 6.9.3-7 Q16 x64 2016-03-27
The complete ImageMagick/Symfony installation steps are the following (here for Windows):
Download the package from https://pecl.php.net/package/imagick/3.5.1/windows
Extract from php_imagick-….zip the php_imagick.dll file, and save it to the ext directory of your PHP installation
Extract the dependencies
• For PHP 5.x/7.x: Extract from ImageMagick-….zip the DLL files located in the bin folder that start with CORE_RL or IM_MOD_RL or FILTER, and save them to the PHP root directory (where you have php.exe), or to a directory in your PATH variable:
• 17 files CORE_RL*.dll
• 126 files IM_MOD_RL*.dll
• 1 files FILTER*.dll
• For PHP 8.x: Extract from php_imagick-….zip the DLL files that start with CORE_RL or IM_MOD_RL or FILTER, and save them to the PHP root directory (where you have php.exe), or to a directory in your PATH variable
Add this line to your php.ini file: extension=php_imagick.dll
Restart the Apache/NGINX Windows service (if applicable)
Check if the install is correct through phpinfo() for which a specific imagick block has been added
Don’t forget to add use Imagick; to your php file
I found the solution, I put the answer for someone who have the same issue:
you should add the following lines
;on Windows:
extension=php_imagick.dll
;on UNIX:
extension = imagick.so
to C:\wamp64\bin\php\phpx.x.x\php.ini
not to C:\wamp64\bin\apache\apachex.x.x\bin\php.ini
because the first php.ini is the file used by PHP CLI, so by the Symfony Local Web Server.
Happy coding!
Related
I'm trying and failing to use tesseract php. I get this error:
Fatal error: Uncaught thiagoalessio\TesseractOCR\TesseractNotFoundException: Error! The command "tesseract" was not found. Make sure you have Tesseract OCR installed on your system: https://github.com/tesseract-ocr/tesseract
The current $PATH is C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Users\Peppe\AppData\Local\Microsoft\WindowsApps;C:\Users\Peppe\AppData\Roaming\Composer\vendor\bin in C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php:48 Stack trace: #0
C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\TesseractOCR.php(26): thiagoalessio\TesseractOCR\FriendlyErrors::checkTesseractPresence('tesseract') #1 C:\xampp\htdocs\index.php(7): thiagoalessio\TesseractOCR\TesseractOCR->run() #2 {main} thrown in C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php on line 48
I'm using windows 10 with xampp installed in C:\xampp. php version 7.4
I installed tesseract.exe from https://github.com/UB-Mannheim/tesseract/wiki both x32 and x64
I used composer to install the https://github.com/thiagoalessio/tesseract-ocr-for-php and it gave no error.
<?php
require_once 'C:\Users\Peppe\vendor\autoload.php';
use thiagoalessio\TesseractOCR\TesseractOCR;
$ocr = new TesseractOCR("caption.jpg");
$content = $ocr->run();
echo $content;
?>
finally, the caption.jpg is in the htdocs folder, the main folder and same folder as index.php where the aforementioned code appears.
any solutions?
I found the answer:
the $PATH variable is taken from Windows. To set it right search for "system environment variables", click "environment variables", edit PATH and add the tesseract software folder, for example C:\Program Files\Tesseract-OCR
In my case I have to add new variable tesseract with full path C:\Program Files\Tesseract-OCR\tesseract.exe as showing in below screenshot
To install TESSERACT OCR and use it in PHP
You must perform the following steps:
(in my explanation, I assume you are on windows)
url src = https://tesseract-ocr.github.io/tessdoc/Installation.html
1- Download the TESSERACT software from URL:
https://github.com/UB-Mannheim/tesseract/wiki
2- From your composer, you must install:
url: https://packagist.org/packages/thiagoalessio/tesseract_ocr
composer require thiagoalessio/tesseract_ocr
3- Then, it is important to do things in the following order:
Your TESSERACT software (step 1) is installed in a directory
which will certainly be (it's up to you to check in your system)
folder = C:\Program Files\Tesseract-OCR
It is absolutely necessary to add this folder in the windows environment variables at the PATH level (user and system)
4- Then you restart your web environment (WAMP, LARAGON ...)
5- Verification that TESSERACT is operational.
Go to your CMD and write:
tesseract --version
Reply somethink like that :
tesseract v5.3.0.20221222
leptonica-1.78.0
libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.3) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.3.0
Found AVX2
Found AVX
Found FMA
Found SSE4.1
Found libarchive 3.5.0 zlib/1.2.11 liblzma/5.2.3 bz2lib/1.0.6 liblz4/1.7.5 libzstd/1.4.5
Found libcurl/7.77.0-DEV Schannel zlib/1.2.11 zstd/1.4.5 libidn2/2.0.4 nghttp2/1.31.0
6- Operation test from a PHP script
uses an image on a white background with text
<?php
$app_class = 'thiagoalessio\TesseractOCR\TesseractOCR';
$file = 'C:/Users/admin/Desktop/im2.jpg';
$class = new ReflectionClass($app_class);
$infoImg = $class->newInstanceArgs();
$infoImg->lang('eng','jpn','spa','fr');
$infoImg->image($file);
$infoImg->withoutTempFiles();
$resultat = $infoImg->run();
var_dump($resultat);
?>
When i try using the GD Library in PHP 7.4.6 on my Windows PC, it shows me this error:
Fatal error: Uncaught Error: Call to undefined function imagecreate() in C:\Users\user\Desktop\Site\index.php:11 Stack trace: #0 {main} thrown in C:\Users\user\Desktop\Site\index.php on line 11
The line 11 is:
$image = imagecreate(200,20);
The full PHP code is:
<?php
$image = imagecreate(200,20);
$background = imagecolorallocate($image,0,0,0);
$foreground = imagecolorallocate($image, 255,255,255);
$imagestring($image,5,5,1,"Test",$foreground);
$header("Content-type: image/jpeg");
$imagejpeg($image);
?>
I tried fixing this by following this guide, and i uncommented the GD line, but it still gives me this error.
Thanks in advance.
Do a php -m to see PHP modules. If you can't find gd there then you need to install and enable that extension. There a lot of tutorials in internet to doing so
I had the same problem with apache and GD.
See https://stackoverflow.com/a/72565820/9630486
The problem was in php.ini path for apache. By default it is not using php.ini from php directory. You need to define PHPIniDir in httpd file or place copy of php.ini to apache folder.
Go to php.ini file search this
;extension=gd
Remove ; then restart the server
Try this code:
<?php
phpinfo();
Search for "GD Support" in the resulting output. If you don't see it or it's not enabled, you need to enable gd in php.ini - search for:
;extension=php_gd2.dll
Remove the comment in front of it, save the ini file, and restart IIS/Apache/Nginx/whatever server you're running.
The php function yaml_emit_file() is not working. I have installed and included the php_yaml.dll in my php.ini file restarted the server but still when I use this function, I get this error (when I run composer):
Call to undefined function RS\composer\yaml_emit_file()
Okay so a little about the background:
PHP version 7.1.7 & Composer version 1.5.1
I am using this function in a ScriptHandler.php file which is invoked when Composer is run. In this script I have a function buildModuleList which is called on post-update-cmd event of Composer. Everything else in the code is working fine.
I am in doubt that maybe I am using this function in wrong context or something like that.
Here is the code snippet where I am using yaml_emit_file() (Providing this just for reference, tell me if am using it the wrong way!):
if (!$fs->exists($moduleListFile)) {
$fs->touch($root.'/profiles/thunder/modulelist.yml');
$fs->chmod($root . '/profiles/thunder/modulelist.yml', 0666);
if(!empty($moduleList)){
$createyml= yaml_emit_file($moduleListFile, $moduleList);
if (!$createyml){
$io->writeError('<error>Cannot create modulelist.yml</error>');
}
}
$io->write('Success: Created new modulelist.yml', $newline= TRUE);
}
else{
$fs->file_put_contents($moduleListFile, $installedPackage, FILE_APPEND);
$io->write('Success: Module entry in modulelist.yml', $newline= TRUE);
}
i hope this help someone i test it on
Windows 10
XAMPP v3.2.4
PHP 8.0.2
Download the latest YAML DLL Package from : https://pecl.php.net/package/yaml
Unzip the files
Move the php_yaml.dll file to xampp/php/ext folder
Open your php.ini in xampp/php add a new line extension=php_yaml.dll, save and exit
Restart your XAMPP Servers
make a PHP file put into
<?php
phpinfo();
?>
Search the string yaml on the page. If it says Enabled then your YAML Extension is working.
I use php5.5 on my webserver. Now I want to use pthreads. Here's my php config: http://dd19010.kasserver.com/infophp.php55
After implementing this code.....
<?php
class AsyncOperation extends Thread
{
public function __construct($threadId)
{
$this->threadId = $threadId;
}
public function run()
{
printf("T %s: Sleeping 3sec\n", $this->threadId);
sleep(3);
printf("T %s: Hello World\n", $this->threadId);
}
}
$start = microtime(true);
for ($i = 1; $i <= 5; $i++) {
$t[$i] = new AsyncOperation($i);
$t[$i]->start();
}
echo microtime(true) - $start . "\n";
echo "end\n";
?>
... the problem is this very error: Fatal error: Class 'Thread' not found in.
Do I have to include some include_once or something similar to make it work?
What do I have to do??
Hi I encountered this problem and managed to solve it.
First, consider the VC version of your PHP and the VC version of extension. In mine I attached the extension pthreads.dll with version VC14 from http://windows.php.net/downloads/pecl/releases/pthreads/ but my PHP VC version is VC11. Look for the lower version to match with the VC version of your PHP.
Second, maybe you missed the step #3 at PHP page. It states that you need to copy the pthreadVC2.dll to different folder. Here's the full instruction.
Find out what is your 'PHP Extension Build' version by using phpinfo(). You can use this - http://localhost/?phpinfo=1
Download the pthreads that matches your php version (32 bit or 64 bit) and php extension build (currently used VC11). Use this link for download - http://windows.php.net/downloads/pecl/releases/pthreads/
Extract the zip -
Move php_pthreads.dll to the 'bin\php\ext\' directory.
Move pthreadVC2.dll to the 'bin\php\' directory.
Move pthreadVC2.dll to the 'bin\apache\bin' directory.
Move pthreadVC2.dll to the 'C:\windows\system32' directory.
Open php\php.ini and add
extension=php_pthreads.dll
Reference: https://secure.php.net/manual/en/pthreads.installation.php
1) Create one php file
phpinfo(); --> Run
Example: Info
PHP Version: 5.6.31
Compiler: MSVC11 (Visual C++ 2012)
Architecture: x64
2)Go to website:
http://windows.php.net/downloads/pecl/releases/pthreads/
Example 2.0.9 file
Compiler:VC11
Architecture:x64
php_pthreads-2.0.9-5.6-ts-vc11-x64.zip download.
3)Extract php_pthreads.dll and pthreadVC2.dll.
wamp\bin\php\php5.6.31\ext\ --> copy php_pthreads.dll
wamp\bin\php\php5.6.31\ --> copy pthreadVC2.dll
wamp\bin\apache\apache2.4.27\bin --> copy pthreadVC2.dll
4)Now edit php.ini
wamp\bin\apache\apache2.4.27\bin\php.ini\ --> Add extension=php_pthreads.dll
wamp\bin\php\php5.6.31\php.ini\ --> Add extension=php_pthreads.dll
5)Now Restart Wamp
Your phpinfo shows that you have php with thread safety disabled. You need to install a version of php that is thread safe to use pthreads. This may or may not fix your current issue though.
You may need to copy the pthreadsVC2.dll into the bin directory of your web service as well.
/etc/php55/fpm/
You're looking for the folder with php.ini in it.
Make sure the php.ini file has the line added:
extension=php_pthreads.dll
I am using WAMP and found that the pthreadVC2.dll should go to the Apache folder instead:
C:\wamp\bin\apache\apache2.4.9\bin
Unlike what is written in README.md, you don't need to have it in the PHP folder, but the php_pthreads.dll should still go to:
C:\wamp\bin\php\php5.5.12\ext
After this, search in this file:
C:\wamp\bin\apache\apache2.4.9\bin\php.ini
For ;extension=php_pgsql.dll and add extension=php_pthreads.dll in a new line after it (yes, it's the bin\php.ini in the Apache folder, not the one in the PHP folder).
Exit WAMP and start it again. You should now see in WAMP menu under PHP > PHP extensions, the new php_pthreads extension.
I think you need to include the extensions int he php.ini file, because I can't see it in the config. You can see that each library has its own section like MySQL, but there isn't such for the threads. I haven't used threads ever but that should be a good place to start from.
By default Threads are not implemented in PHP, and according to your phpinfo it does not seem to be loaded. Check out the PHP manual on how to set-up/configure the module.
We have a Windows 2008 server, this morning when we came to the office, we saw that mysql is not working
We are getting this error for a simple connect function
Fatal error: Call to undefined function mysql_connect()
but the strange thing is that mysql extensions are enabled in php.ini file.
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo_mysql.dll
According to phpinfo() , php using the ini file in C:\PHP and the DLL files exist in the ext folder.
I cannot find a way to solve or at least to find out what is causing this.
I would be grateful if someone could give me some advice
Thanks in advance
for debugging purposes try
if ( !function_exists('mysql_connect') ) {
echo '<pre>mysql extension loaded: ', extension_loaded('mysql') ? 'yes':'no', "\r\n";
$cf = get_cfg_var('cfg_file_path');
echo 'ini file: ', $cf, "\r\n";
if ( !$cf || !file_exists($cf) ) {
echo "no config file\r\n";
}
else {
echo "mysql config options:\r\n";
$mc = array_filter( file($cf), function($e) { return false!==stripos($e, 'mysql') && false!==stripos($e, 'extension'); });
echo join("", $mc);
}
die('no function mysql_connect</pre>');
}
the output should be something like
<pre>mysql extension loaded: no
ini file: C:\Develop\php\php.ini
mysql config options:
;extension=php_mysql.dll
;extension=php_mysqli.dll
extension=php_pdo_mysql.dll
no function mysql_connect</pre>
which indicates that the line extension=php_mysql.dll has been commented out (in my php.ini).
In case this script shows that the php_mysql.dll indeed should have been loaded try to increase the log level of both php and your webserver and check the log file. Maybe windows couldn't load the dll because it depends on another dll which isn't present (in the correct version) anymore. E.g. the php_mysql.dll in "my" ext directory depends on MSVCR110.DLL which is present because I have Visual Studio installed, otherwise installing the Visual Studio 2012 redistributables would be required. Tools like Dependency Walker can show you which dlls are required to run an application and/or dll.
Or it could be that the php_mysql.dll implements a different API version than your php core. E.g. if you have PHP 5.4 installed but the php_mysql.dll is for php 5.1 you will see an API magic key error in the log file.
Restart Your WAMP Or XAMPP Server. You can check with Contral Panel of XAMPP Or WAMP