本文整理匯總了PHP中Imagick類的典型用法代碼示例。如果您正苦於以下問題:PHP Imagick類的具體用法?PHP Imagick怎麽用?PHP Imagick使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Imagick類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: wordWrapAnnotation
function wordWrapAnnotation(\Imagick $imagick, $draw, $text, $maxWidth)
{
$words = explode(" ", $text);
$lines = array();
$i = 0;
$lineHeight = 0;
while ($i < count($words)) {
$currentLine = $words[$i];
if ($i + 1 >= count($words)) {
$lines[] = $currentLine;
break;
}
//Check to see if we can add another word to this line
$metrics = $imagick->queryFontMetrics($draw, $currentLine . ' ' . $words[$i + 1]);
while ($metrics['textWidth'] <= $maxWidth) {
//If so, do it and keep doing it!
$currentLine .= ' ' . $words[++$i];
if ($i + 1 >= count($words)) {
break;
}
$metrics = $imagick->queryFontMetrics($draw, $currentLine . ' ' . $words[$i + 1]);
}
//We can't add the next word to this line, so loop to the next line
$lines[] = $currentLine;
$i++;
//Finally, update line height
if ($metrics['textHeight'] > $lineHeight) {
$lineHeight = $metrics['textHeight'];
}
}
return array($lines, $lineHeight);
}
示例2: IMAGEN_tipo_normal
function IMAGEN_tipo_normal()
{
$escalado = 'IMG/i/m/' . $_GET['ancho'] . '_' . $_GET['alto'] . '_' . $_GET['sha1'];
$origen = 'IMG/i/' . $_GET['sha1'];
$ancho = $_GET['ancho'];
$alto = $_GET['alto'];
if (@($ancho * $alto) > 562500) {
die('La imagen solicitada excede el límite de este servicio');
}
if (!file_exists($escalado)) {
$im = new Imagick($origen);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(85);
$im->setImageFormat('jpeg');
$im->stripImage();
$im->despeckleImage();
$im->sharpenImage(0.5, 1);
//$im->reduceNoiseImage(0);
$im->setInterlaceScheme(Imagick::INTERLACE_PLANE);
$im->resizeImage($ancho, $alto, imagick::FILTER_LANCZOS, 1);
$im->writeImage($escalado);
$im->destroy();
}
$im = new Imagick($escalado);
$output = $im->getimageblob();
$outputtype = $im->getFormat();
$im->destroy();
header("Content-type: {$outputtype}");
header("Content-length: " . filesize($escalado));
echo $output;
}
示例3: renderOriginalImage
function renderOriginalImage()
{
$imagick = new \Imagick(realpath("./images/chair.jpeg"));
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
return;
}
示例4: resizeTo
public function resizeTo($width, $height)
{
if (osc_use_imagick()) {
$bg = new Imagick();
$bg->newImage($width, $height, 'white');
$this->im->thumbnailImage($width, $height, true);
$geometry = $this->im->getImageGeometry();
$x = ($width - $geometry['width']) / 2;
$y = ($height - $geometry['height']) / 2;
$bg->compositeImage($this->im, imagick::COMPOSITE_OVER, $x, $y);
$this->im = $bg;
} else {
$w = imagesx($this->im);
$h = imagesy($this->im);
if ($w / $h >= $width / $height) {
//$newW = $width;
$newW = $w > $width ? $width : $w;
$newH = $h * ($newW / $w);
} else {
//$newH = $height;
$newH = $h > $height ? $height : $h;
$newW = $w * ($newH / $h);
}
$newIm = imagecreatetruecolor($width, $height);
//$newW, $newH);
imagealphablending($newIm, false);
$colorTransparent = imagecolorallocatealpha($newIm, 255, 255, 255, 127);
imagefill($newIm, 0, 0, $colorTransparent);
imagesavealpha($newIm, true);
imagecopyresampled($newIm, $this->im, ($width - $newW) / 2, ($height - $newH) / 2, 0, 0, $newW, $newH, $w, $h);
imagedestroy($this->im);
$this->im = $newIm;
}
return $this;
}
示例5: compareFile
function compareFile($injector, $functionFullname, $filename, $fileExtension)
{
$newFilename = $filename . "new1";
generateCompare($injector, $functionFullname, $newFilename);
//echo "Comparing $filename to $newFilename \n";
echo ".";
$imagickSrc = new Imagick($filename . $fileExtension);
$imagickNew = new Imagick($newFilename . $fileExtension);
// const LAYERMETHOD_UNDEFINED = 0;
// const LAYERMETHOD_COALESCE = 1;
// const LAYERMETHOD_COMPAREANY = 2;
// const LAYERMETHOD_COMPARECLEAR = 3;
// const LAYERMETHOD_COMPAREOVERLAY = 4;
// const LAYERMETHOD_DISPOSE = 5;
// const LAYERMETHOD_OPTIMIZE = 6;
// const LAYERMETHOD_OPTIMIZEPLUS = 8;
// const LAYERMETHOD_OPTIMIZETRANS = 9;
// const LAYERMETHOD_COMPOSITE = 12;
// const LAYERMETHOD_OPTIMIZEIMAGE = 7;
// const LAYERMETHOD_REMOVEDUPS = 10;
// const LAYERMETHOD_REMOVEZERO = 11;
// const LAYERMETHOD_TRIMBOUNDS = 12;
$result = $imagickSrc->compareImages($imagickNew, \Imagick::LAYERMETHOD_COMPAREANY);
list($imagickDifference, $distance) = $result;
if ($distance > 0.01) {
echo "Differrence detected in {$functionFullname} \n";
/** @var $imagickDifference Imagick */
$imagickDifference->writeimage($filename . "diff" . $fileExtension);
}
}
示例6: writeText
function writeText($text)
{
if ($this->printer == null) {
throw new LogicException("Not attached to a printer.");
}
if ($text == null) {
return;
}
$text = trim($text, "\n");
/* Create Imagick objects */
$image = new \Imagick();
$draw = new \ImagickDraw();
$color = new \ImagickPixel('#000000');
$background = new \ImagickPixel('white');
/* Create annotation */
//$draw -> setFont('Arial');// (not necessary?)
$draw->setFontSize(24);
// Size 21 looks good for FONT B
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$metrics = $image->queryFontMetrics($draw, $text);
$draw->annotation(0, $metrics['ascender'], $text);
/* Create image & draw annotation on it */
$image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
$image->setImageFormat('png');
$image->drawImage($draw);
//$image -> writeImage("test.png");
/* Save image */
$escposImage = new EscposImage();
$escposImage->readImageFromImagick($image);
$size = Printer::IMG_DEFAULT;
$this->printer->bitImage($escposImage, $size);
}
示例7: save
protected function save($path)
{
$pathinfo = pathinfo($path);
if (!file_exists($pathinfo['dirname'])) {
mkdir($pathinfo['dirname'], 0777, true);
}
try {
$image = new \Imagick($this->source);
$image->thumbnailImage($this->width, $this->height, true);
if ($this->color) {
$thumb = $image;
$image = new \Imagick();
$image->newImage($this->width, $this->height, $this->color, $pathinfo['extension']);
$size = $thumb->getImageGeometry();
$x = ($this->width - $size['width']) / 2;
$y = ($this->height - $size['height']) / 2;
$image->compositeImage($thumb, \imagick::COMPOSITE_OVER, $x, $y);
$thumb->destroy();
}
$image->writeImage($path);
$image->destroy();
} catch (\Exception $e) {
}
return $this;
}
示例8: getThumbnail
/**
* {@inheritDoc}
*/
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
try {
$svg = new \Imagick();
$svg->setBackgroundColor(new \ImagickPixel('transparent'));
$content = stream_get_contents($fileview->fopen($path, 'r'));
if (substr($content, 0, 5) !== '<?xml') {
$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
}
// Do not parse SVG files with references
if (stripos($content, 'xlink:href') !== false) {
return false;
}
$svg->readImageBlob($content);
$svg->setImageFormat('png32');
} catch (\Exception $e) {
\OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::ERROR);
return false;
}
//new image object
$image = new \OC_Image();
$image->loadFromData($svg);
//check if image object is valid
if ($image->valid()) {
$image->scaleDownToFit($maxX, $maxY);
return $image;
}
return false;
}
示例9: GetDriverVersion
/**
* Info about driver's version
*
* @param string $sDriver
*
* @return bool
*/
public function GetDriverVersion($sDriver)
{
$sVersion = false;
$sDriver = strtolower($sDriver);
if (isset($this->aDrivers[$sDriver])) {
if ($this->aDrivers[$sDriver] == 'Imagick') {
if (class_exists('Imagick')) {
$img = new \Imagick();
$aInfo = $img->getVersion();
$sVersion = $aInfo['versionString'];
if (preg_match('/\\w+\\s\\d+\\.[\\d\\.\\-]+/', $sVersion, $aMatches)) {
$sVersion = $aMatches[0];
}
}
} elseif ($this->aDrivers[$sDriver] == 'Gmagick') {
if (class_exists('Gmagick')) {
$aInfo = Gmagick::getVersion();
$sVersion = $aInfo['versionString'];
if (preg_match('/\\w+\\s\\d+\\.[\\d\\.\\-]+/', $sVersion, $aMatches)) {
$sVersion = $aMatches[0];
}
}
} else {
if (function_exists('gd_info')) {
$aInfo = gd_info();
$sVersion = $aInfo['GD Version'];
if (preg_match('/\\d+\\.[\\d\\.]+/', $sVersion, $aMatches)) {
$sVersion = $aMatches[0];
}
}
}
}
return $sVersion;
}
示例10: convert
/**
* Tries to converts the <b>$input</b> image into the <b>$output</b> format.
*
* @param string $input The input file.
* @param string $output The output file.
* @return void
*/
public static function convert($input, $output)
{
$inputType = strtolower(pathinfo($input, PATHINFO_EXTENSION));
$outputType = strtolower(pathinfo($output, PATHINFO_EXTENSION));
// Check for output file without extension and reuse input type
if ($outputType === '') {
$outputType = $inputType;
$output .= ".{$outputType}";
}
if ($inputType === 'svg') {
self::prepareSvg($input);
}
if ($inputType === $outputType) {
file_put_contents($output, file_get_contents($input));
} elseif (extension_loaded('imagick') === true) {
$imagick = new \Imagick($input);
$imagick->setImageFormat($outputType);
$imagick->writeImage($output);
// The following code is not testable when imagick is installed
// @codeCoverageIgnoreStart
} elseif (self::hasImagickConvert() === true) {
$input = escapeshellarg($input);
$output = escapeshellarg($output);
system("convert {$input} {$output}");
} else {
$fallback = substr($output, 0, -strlen($outputType)) . $inputType;
echo "WARNING: Cannot generate image of type '{$outputType}'. This", " feature needs either the\n pecl/imagick extension or", " the ImageMagick cli tool 'convert'.\n\n", "Writing alternative image:\n{$fallback}\n\n";
file_put_contents($fallback, file_get_contents($input));
}
// @codeCoverageIgnoreEnd
}
示例11: createThumbnails
/**
*
*
*
* Data comes in following format
*
* array(
* <fileId> => <pdfFilePath>
* );
*
* @param type $data
* @return int
*/
public function createThumbnails($data)
{
return NULL;
if (!extension_loaded('imagick')) {
}
//die();
$imagick = new \Imagick();
foreach ($data as $fileId => $filePath) {
if (is_file($filePath)) {
dump($filePath . ' exists');
}
}
die;
if (!file_exists($pdfName)) {
return 0;
}
$pdf = new \Imagick();
$pdf->readImage($pdfName);
$pages = count($pdf);
$pages = $pages ? $pages + 1 : 0;
foreach ($pdf as $index => $image) {
$image->setcolorspace(\Imagick::COLORSPACE_RGB);
$image->setCompression(\Imagick::COMPRESSION_JPEG);
$image->setCompressionQuality(90);
$image->setResolution(600, 600);
$image->setImageFormat('jpeg');
// $image->resizeImage(405, 574, \Imagick::FILTER_LANCZOS, 1 , TRUE);
$image->writeImage($folder . '/' . ($index + 1) . '.jpg');
}
$pdf->destroy();
$pages = count(glob($folder . '/*.jpg'));
return $pages;
}
示例12: loadSVG
protected function loadSVG($image)
{
$this->log('Load SVG image', 2);
if (!class_exists('Imagick')) {
$this->log('Fail', 2);
throw new ImageMagickNotAvailableException();
}
$content = @file_get_contents($image);
if (!is_string($content)) {
$this->log('Fail', 2);
throw new InvalidImageException(sprintf('Could not load <info>%s</info>, could not read resource.', $image));
}
$image = new \Imagick();
$image->readImageBlob($content);
$width = $image->getImageWidth();
$height = $image->getImageHeight();
if (310 > $width || 310 > $height) {
$this->log('Fail', 2);
throw new InvalidImageException(sprintf('Source image must be at least <info>%s</info>,' . ' actual size is: <info>%s</info>', '310x310', $width . 'x' . $height));
}
$image->setImageFormat('png24');
$this->gd = @imagecreatefromstring((string) $image);
$image->destroy();
if (!is_resource($this->gd)) {
$this->log('Fail', 2);
throw new InvalidImageException(sprintf('Could not load <info>%s</info>, ImageMagick could not convert' . ' the resource to a valid GD compatible image.', $image));
}
$this->log('Success', 2);
return $this;
}
示例13: createAvatarAutomatically
/**
* アバター自動生成処理
*
* @param Model $model ビヘイビア呼び出し元モデル
* @param array $user ユーザデータ配列
* @return mixed On success Model::$data, false on failure
* @throws InternalErrorException
*/
public function createAvatarAutomatically(Model $model, $user)
{
//imagickdraw オブジェクトを作成します
$draw = new ImagickDraw();
//文字色のセット
$draw->setfillcolor('white');
//フォントサイズを 160 に設定します
$draw->setFontSize(140);
//テキストを追加します
$draw->setFont(CakePlugin::path($model->plugin) . 'webroot' . DS . 'fonts' . DS . 'ipaexg.ttf');
$draw->annotation(19, 143, mb_substr(mb_convert_kana($user['User']['handlename'], 'KVA'), 0, 1));
//新しいキャンバスオブジェクトを作成する
$canvas = new Imagick();
//ランダムで背景色を指定する
$red1 = strtolower(dechex(mt_rand(3, 12)));
$red2 = strtolower(dechex(mt_rand(0, 15)));
$green1 = strtolower(dechex(mt_rand(3, 12)));
$green2 = strtolower(dechex(mt_rand(0, 15)));
$blue1 = strtolower(dechex(mt_rand(3, 12)));
$blue2 = strtolower(dechex(mt_rand(0, 15)));
$canvas->newImage(179, 179, '#' . $red1 . $red2 . $green1 . $green2 . $blue1 . $blue2);
//ImagickDraw をキャンバス上に描畫します
$canvas->drawImage($draw);
//フォーマットを PNG に設定します
$canvas->setImageFormat('png');
App::uses('TemporaryFolder', 'Files.Utility');
$folder = new TemporaryFolder();
$filePath = $folder->path . DS . Security::hash($user['User']['handlename'], 'md5') . '.png';
$canvas->writeImages($filePath, true);
return $filePath;
}
示例14: apply
/**
* Applies the effect.
*/
public function apply()
{
if (!method_exists($this->_image->imagick, 'polaroidImage') || !method_exists($this->_image->imagick, 'trimImage')) {
throw new Horde_Image_Exception('Your version of Imagick is not compiled against a recent enough ImageMagick library to use the PolaroidImage effect.');
}
try {
// This determines the color of the underlying shadow.
$this->_image->imagick->setImageBackgroundColor(new ImagickPixel($this->_params['shadowcolor']));
$this->_image->imagick->polaroidImage(new ImagickDraw(), $this->_params['angle']);
// We need to create a new image to composite the polaroid over.
// (yes, even if it's a transparent background evidently)
$size = $this->_image->getDimensions();
$imk = new Imagick();
$imk->newImage($size['width'], $size['height'], $this->_params['background']);
$imk->setImageFormat($this->_image->getType());
$result = $imk->compositeImage($this->_image->imagick, Imagick::COMPOSITE_OVER, 0, 0);
$this->_image->imagick->clear();
$this->_image->imagick->addImage($imk);
} catch (ImagickPixelException $e) {
throw new Horde_Image_Exception($e);
} catch (ImagickDrawException $e) {
throw new Horde_Image_Exception($e);
} catch (ImagickException $e) {
throw new Horde_Image_Exception($e);
}
$imk->destroy();
}
示例15: renderImage
public function renderImage()
{
//Create a ImagickDraw object to draw into.
$draw = new \ImagickDraw();
$darkColor = new \ImagickPixel('brown');
//http://www.imagemagick.org/Usage/compose/#compose_terms
$draw->setStrokeColor($darkColor);
$draw->setFillColor('white');
$draw->setStrokeWidth(2);
$draw->setFontSize(72);
$draw->setStrokeOpacity(1);
$draw->setStrokeColor($darkColor);
$draw->setStrokeWidth(2);
$draw->setFont("../fonts/CANDY.TTF");
$draw->setFontSize(140);
$draw->setFillColor('none');
$draw->rectangle(0, 0, 1000, 300);
$draw->setFillColor('white');
$draw->annotation(50, 180, "Lorem Ipsum!");
$imagick = new \Imagick(realpath("images/TestImage.jpg"));
$draw->composite(\Imagick::COMPOSITE_MULTIPLY, -500, -200, 2000, 600, $imagick);
//Create an image object which the draw commands can be rendered into
$imagick = new \Imagick();
$imagick->newImage(1000, 300, "SteelBlue2");
$imagick->setImageFormat("png");
//Render the draw commands in the ImagickDraw object
//into the image.
$imagick->drawImage($draw);
//Send the image to the browser
header("Content-Type: image/png");
echo $imagick->getImageBlob();
}