Laravel 干预图像添加带有字体的文本显示错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36173360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:27:59  来源:igfitidea点击:

Laravel intervention image adding text with font is showing error

imagelaravellaravel-5

提问by Jijesh Cherrai

I'm using Intervention package in Laravel5 for Inserting a text in image, and its is working well. But if I add font(.ttf) it shows the following error. The directory is correct.

我在 Laravel5 中使用干预包在图像中插入文本,它运行良好。但是如果我添加 font(.ttf) 它会显示以下错误。目录是正确的。

NotSupportedException in Font.php line 30: Internal GD font () not available. Use only 1-5.

Font.php 第 30 行中的 NotSupportedException:内部 GD 字体 () 不可用。仅使用 1-5 个。

        $image = Image::make(public_path('/userimage/') . "user.png");
        $image->text('text string', 10, 5, function($font) {
            $font->file(asset('/web/fonts/OpenSans-Regular.ttf'));
            $font->size(50);
            $font->color('#fdf6e3');
            $font->angle(45);
        });

回答by Robert C.

  1. The GD library does not support custom fonts. You are supposed to use Imagick. Go to config/image.php and edit the drivervalue to 'imagick'(after you actually install the extension for it):

    'driver' => 'imagick'

  2. asset('/web/fonts/OpenSans-Regular.ttf')returns an URL. You have to provide a path. Use public_path('web/fonts/OpenSans-Regular.ttf')instead.

  1. GD 库不支持自定义字体。你应该使用 Imagick。转到 config/image.php 并将驱动程序值编辑为“imagick”(在您实际安装扩展后):

    '司机' => 'imagick'

  2. asset('/web/fonts/OpenSans-Regular.ttf')返回一个URL。你必须提供一个路径。使用public_path('web/fonts/OpenSans-Regular.ttf')来代替。

回答by serpentow

Use base_path()instead of asset().

使用base_path()代替asset()

Example:

例子:

$font->file(base_path('public/web/fonts/OpenSans-Regular.ttf'));

回答by bandzi

Try this: $font->file(public_path('fontnameExample.ttf'));

试试这个: $font->file(public_path('fontnameExample.ttf'));

回答by neochief

I would do this:

我会这样做:

  • Try removing leading slash in path.
  • Run dd(asset('/web/fonts/OpenSans-Regular.ttf'))and see if it matches expected path.
  • 尝试删除路径中的前导斜杠。
  • 运行dd(asset('/web/fonts/OpenSans-Regular.ttf'))并查看它是否与预期路径匹配。

回答by Niraj Shah

Two things you can try:

您可以尝试两件事:

  • Remove the forward slash in the path to font file: $font->file(asset('web/fonts/OpenSans-Regular.ttf'));
  • Make sure the font file is readable by Laravel / Apache, run chmod 755 path/to/web/fonts/OpenSans-Regular.ttfjust in case.
  • 删除字体文件路径中的正斜杠: $font->file(asset('web/fonts/OpenSans-Regular.ttf'));
  • 确保 Laravel / Apache 可以读取字体文件,chmod 755 path/to/web/fonts/OpenSans-Regular.ttf以防万一。