标签归档: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();