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();

发表评论