标签归档:Imagick

Windows下PHP安装Imagick扩展

查看本机PHP版本

php -v

输出

PHP 7.4.14 (cli) (built: Jan  5 2021 15:11:43) ( NTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

可以看到PHP版本为7.4,NTS(非线程安全),64位

下载扩展DLL

  1. 打开 https://pecl.php.net/package/imagick
  2. 找到最新稳定版本,点击后面的DLL
  3. 再打开的页面中,根据上面的PHP版本,选择要下载的DLL

安装

  1. 将下载的压缩文件下的php_imagick.dll放入PHP目录下的ext子目录下
  2. 将压缩文件下的其他DLL文件放到PHP目录
  3. 修改php目录下的php.ini,添加extension=imagick

Imagick给图片加文字和二维码

    // 初始化画布
    set_time_limit(0);
    $bg_file = Yii::getAlias('@frontend/web/test/wxbg.jpg');
    $image = new \Imagick($bg_file);

    // 写字
    $font_file = Yii::getAlias('@frontend/web/test/msyhbd.ttc');
    $draw = new \ImagickDraw();
    $draw->setFillColor('#FF7F24');
    $draw->setFont($font_file);
    $draw->setFontSize(64);
    $draw->setTextKerning(5);
    $draw->setFontWeight(700);
    $draw->setTextEncoding('UTF-8');
    $metrics = $image->queryFontMetrics($draw, $name);
    $draw->annotation(158, 50 + $metrics['ascender'], $name);
    $image->drawImage($draw);

    // 二维码
    $qr_image = new \Imagick(Yii::getAlias('@frontend/web/test/qr.png'));
    $qr_image->scaleImage(248, 248);
    $image->compositeImage($qr_image, \Imagick::COMPOSITE_COPY, 254, 504);

    // 返回
    $response = Yii::$app->getResponse();
    $response->format = Response::FORMAT_RAW;
    $response->headers->set('Content-Type', 'image/jpeg');
    $response->data = $image->getImageBlob();
    return $response->send();

PHP用Imagck实现图片文字水平以及垂直集中

    $draw = new \ImagickDraw();
    $imagick = new \Imagick();
    $imagick->newImage($width, $height, new \ImagickPixel('none'));
    $draw->setFillColor($color);
    $draw->setFont($font_file);
    $draw->setFontSize($size);
    $draw->setTextEncoding('UTF-8');
    $stringArr = $this->mbStringToArray($text);
    $textHeight = 0;
    $textWidth = 0;
    $charCount = count($stringArr);
    $texts = [];
    foreach ($stringArr as $char) {
        $metrics = $imagick->queryFontMetrics($draw, $char, true);
        $charHeight = $metrics['textHeight'];
        $charWidth = $metrics['textWidth'];
        $textHeight += $charHeight;
        $textWidth += $charWidth;
        $texts[] = ['char' => $char, 'height' => $charHeight, 'width' => $charWidth];
    }

    if ($direction == 2) {
        if ($charCount > 1) {
            $textHeight += ($charCount - 1) * $space;
        }
        $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
        $x = $size / 2;
        if ($textHeight > $height) {
            $offset = 0;
        } else {
            $offset = ($height - $textHeight) / 2;
        }
        $y = $offset;
        foreach ($texts as $c) {
            $y += $c['height'];
            $draw->annotation($x, $y, $c['char']);
            $y += $space;
        }
    } else {
        if ($charCount > 1) {
            $textWidth += ($charCount - 1) * $space;
        }
        $metrics = $imagick->queryFontMetrics($draw, $text, false);
        $y = $metrics['ascender'];
        $draw->setTextAlignment(\Imagick::ALIGN_LEFT);
        if ($align == \Imagick::ALIGN_CENTER) {
            if ($textWidth > $width) {
                $x = 0;
            } else {
                $x = ($width - $textWidth) / 2;
            }
        } elseif ($align == \Imagick::ALIGN_RIGHT) {
            $x = $width - $textWidth;
        } else {
            $x = 0;
        }
        foreach ($texts as $c) {
            $draw->annotation($x, $y, $c['char']);
            $x += $c['width'] + $space;
        }
    }
    $imagick->setImageFormat("png");
    $imagick->drawImage($draw);

    header("Content-Type: image/png");
    echo $imagick->getImageBlob();

PHP生成一个透明背景的PNG图像(GD+Imagick)

使用GD

    header('Content-Type:image/png');
    $width = 300;
    $height = 300;
    $color = 'ff0000';
    $font_file = 'size.ttf';

    $im = imagecreatetruecolor($width, $height);
    $bg = imagecolorallocatealpha($im, 0, 0, 0, 127);
    imagefill($im, 0, 0, $bg);
    imagefttext($im, 33, 0, 150, 150, $color, $font_file, 'test');
    imagesavealpha($im, true);
    imagepng($im);
    imagedestroy($im);

使用imagick

    $draw = new \ImagickDraw();
    $imagick = new \Imagick();
    $imagick->newImage($width, $height, new \ImagickPixel('none'));
    $draw->setFillColor($color);
    $draw->setFont($font_file);
    $draw->setFontSize($size);
    $draw->setTextEncoding('UTF-8');
    $draw->annotation($x, $y, $text);
    header("Content-Type: image/png");
    echo $imagick->getImageBlob();