Laravel 5.2:找不到Imagick类 [英] Laravel 5.2: Class Imagick not found
问题描述
我们正在将PDF页面转换为多个单个图像.我们在stackoverflow中找到了一个代码片段,并将其转换为服务类.我们已经安装了Imagick,它也显示在phpinfo()中.但是,在我们的laravel应用程序5.2版中,出现以下错误.
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.
ReflectionException in Container.php line 798:
Class Imagick does not exist
我们在laravel环境之外测试了我们的代码,它的工作就像一个魅力.没有抛出这样的错误.我们还运行了以下命令来检查Imagick
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
php -i | grep -i imagick
这是输出
/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
一切似乎都正确.它在Laravel外部有效,但在laravel中无效.我不知道怎么了我们必须配置Laravel才能使用Imagick吗?
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?
这是我们正在使用的服务类
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;
}
}
推荐答案
这是对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
如果您使用的是7.2以外的PHP版本或未使用Nginx,则需要进行调整.
If you're using a version of PHP other than 7.2 or aren't using Nginx, you'll need to adjust.
这篇关于Laravel 5.2:找不到Imagick类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!