Laravel 5.2:找不到类 Imagick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37833614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.2: Class Imagick not found
提问by IamGhale
We are converting PDF pages to multiple single images. We found a code snippet in stackoverflow and converted it to a service class. We have Imagick installed and it shows up in phpinfo() as well. However, in our laravel application, version 5.2, we are getting following error.
我们正在将 PDF 页面转换为多个单个图像。我们在 stackoverflow 中找到了一段代码,并将其转换为服务类。我们已经安装了 Imagick,它也显示在 phpinfo() 中。但是,在我们的 Laravel 应用程序 5.2 版中,我们收到以下错误。
ReflectionException in Container.php line 798:
Class Imagick does not exist
We tested our code outside laravel environment and it's working like a charm. No such error is thrown. We also ran following command to check Imagick
我们在 laravel 环境之外测试了我们的代码,它的运行非常棒。不会抛出这样的错误。我们还运行以下命令来检查 Imagick
php -i | grep -i imagick
and this is the output
这是输出
/etc/php5/cli/conf.d/20-imagick.ini,
imagick
imagick module => enabled
imagick module version => 3.4.3RC1
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator
Imagick compiled with ImageMagick version => ImageMagick 6.7.7-10 2016-06-01 Q16 http://www.imagemagick.org
Imagick using ImageMagick library version => ImageMagick 6.7.7-10 2016-06-01 Q16 http://www.imagemagick.org
imagick.locale_fix => 0 => 0
imagick.progress_monitor => 0 => 0
imagick.skip_version_check => 0 => 0
Everything seems right. It works outside Laravel but not in laravel. I got no idea what's wrong. Do we have to configure Laravel to use Imagick?
一切似乎都对。它在 Laravel 之外有效,但在 Laravel 中无效。我不知道出了什么问题。我们是否必须配置 Laravel 才能使用 Imagick?
Here is our service class that we are using
这是我们正在使用的服务类
<?php
namespace App\Services\Utilities;
use Imagick;
class PdfToImageService
{
/**
* Destination folder where images will be saved
* @var string
*/
protected $destination = 'images/users/';
/**
* Injecting dependencies
*
* @param Imagick $imagick
*/
function __construct(Imagic $imagick)
{
$this->imagick = $imagick;
}
/**
* Convert pdf having multiple pages to multiple single images
*
* 1. Strip document extension
* 2. Convert this document
* 3. Set background color and flatten. It Prevents black background on objects with transparency
* 4. Set image resolution
* 5. Determine number of pages
* 6. Compress Image Quality
* 7. Generate images from each pdf page
* 8. Destroy current imagick session
*
* @param string $fileName
* @return array $convertedImageNames
*/
public function createImages($fileName)
{
$fileName = basename($fileName);
$this->imagick->readImage($fileName);
$this->imagick->setImageBackgroundColor('white');
$this->imagick->setResolution(300,300);
$this->imagick->setImageCompressionQuality(100);
$convertedImageNames = $this->generateImageFromPDFPage(
$fileName, $this->imagick->getNumberImages()
);
$this->imagick->destroy();
return $convertedImageNames;
}
/**
* Loop throught each pdf pages and convert it to image.
* A. Set iterator postion
* B. Set image format
* C. Write Images to destination folder
*
* @param string $fileName
* @param integer $noOfPages
* @return array
*/
private function generateImageFromPDFPage($fileName, $noOfPages)
{
for($i = 0;$i < $noOfPages; $i++) {
$this->imagick->setIteratorIndex($i);
$this->imagick->setImageFormat('jpeg');
$this->imagick->writeImage($this->destination.$fileName.'-'.$i.'.jpg');
$convertedImageNames[$i] = $fileName.'-'.$i.'.jpg';
}
return $convertedImageNames;
}
}
回答by Ryan
This is what worked for me for Laravel 5.7 on Homestead:
这对我在 Homestead 上的 Laravel 5.7 有用:
sudo apt-get update && sudo apt-get install -y imagemagick php-imagick && sudo service php7.2-fpm restart && sudo service nginx restart
If you're using a version of PHP other than 7.2 or aren't using Nginx, you'll need to adjust.
如果您使用的是 7.2 以外的 PHP 版本或未使用 Nginx,则需要进行调整。
回答by Sishir Pokhrel
Can you try editing php.ini located in
cd /etc/php5/apache2/php.ini
if your linux is ubuntu ?
如果您的 linux 是 ubuntu,您可以尝试编辑位于
cd /etc/php5/apache2/php.ini 中的php.ini
吗?
cat php.ini | grep extension=imagick.so
if there is the results of search, then you might get this
;extension=imagick.so
You remove this semicolon ;
and if not any results,
如果有搜索结果,那么你可能会得到这个
;extension=imagick.so
你去掉这个分号;如果没有任何结果,
echo "extension=imagick.so" >> /etc/php5/apache2/php.ini
And finally
最后
sudo /etc/init.d/apahce2 restart
It works with this code
它适用于此代码
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Imagick;
class GuestController extends BaseController {
public $imagic;
public function __construct(){
$this->imagic = new Imagick();
}
public function test(){
return get_class_methods($this->imagic);
}
}
回答by Luigi Lopez
Try this:
尝试这个:
- Dowload the extension here (the tarball): http://pecl.php.net/package/imagick
- 在此处下载扩展程序(tarball):http: //pecl.php.net/package/imagick
(Lets suppose you use the version: 3.4.3)
(假设您使用版本:3.4.3)
Go to the extension directory with the terminal Example
cd /Downloads/
Extract the tarball
tar xf imagick-3.4.3.tgz
Go to the new extracted directory
cd imagick-3.4.3.tgz
Phpize the library (is base written in C)
phpize
Run
./configure
Run
make && make test && make install
Go to your php.ini (ussualy in Ubuntu located at /etc/php/7.x/cli/php.ini) and add this line:
extension=imagick.so
使用终端示例转到扩展目录
cd /Downloads/
提取压缩包
tar xf imagick-3.4.3.tgz
转到新提取的目录
cd imagick-3.4.3.tgz
Phpize 库(是用 C 编写的基础)
phpize
跑
./configure
跑
make && make test && make install
转到您的 php.ini(通常在位于 /etc/php/7.x/cli/php.ini 的 Ubuntu 中)并添加以下行:
extension=imagick.so
That's it. Update php
就是这样。更新php
service php7.2-fpm restart
and the server (apache, nginx or other).
和服务器(apache、nginx 或其他)。