gevorggalstyan/image2html

PHP Composer 包,用于将图像转换为 HTML。

v1.0.1 2016-09-18 11:03 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:09:25 UTC


README

描述和简单用法

这是一个库,用于帮助将 JPG 和 PNG 图像转换为 HTML。

此库的编写是为了解决电子邮件中的图像问题。有时当电子邮件包含图像时,它们可能会以非常不可预测的方式被阻止。通过使用此库,您可以将图像转换为 HTML 表格或样式属性值。

使用非常简单。

在您想使用文件的 PHP 文件顶部,您指定要使用该库。

<?php

use \GevorgGalstyan\Image2HTML\Converter;

之后,您就可以开始使用转换器了。

$image_converter = new Converter('./images/logo.png');
$image_converter->set_width(256);

$table_html = $image_converter->get_as_table();

echo $table_html;

这将生成一个具有 1px X 1px 单元的表格,代表图像的每个像素。

通常,此方法可以保证成功发送到任何电子邮件客户端和浏览器。但随着图像 HTML 的变长,某些电子邮件服务可能会截断电子邮件,可能需要点击“显示完整消息”按钮。

您还可以获取图像作为样式属性值。

$image_converter = new Converter('./images/logo.png');
$image_converter->set_width(256);

$style_html = $image_converter->get_as_style();

echo '<div style="' . $style_html . '"></div>';

文档

构造函数

__construct 函数有多个参数

public function __construct($file_name,
                            $width = 100,
                            $color_type = Converter::BEST,
                            $pixel_size = 1,
                            $blur = 0,
                            $true_color = TRUE) 
                            {
                            ...
                            }

如您所注意到的,仅需要 $file_name。其他都有默认值。

$color_type 的可接受值包括

    const HEXA = 0;
    const RGBA = 1;
    const BEST = 2;

公共函数

您也可以通过这些函数在创建转换器后设置变量值。以下是它们从库中的蓝图。

public function set_path($path) {...}
public function set_color_type($type) {...}
public function set_width($width) {...}
public function set_pixel_size($size) {...}
public function set_blur($blur) {...}
public function set_true_color($true_color) {...}

所有这些值都有 getter。

通常您需要设置图像的宽度,其余部分保持默认值。这就是为什么宽度是构造函数中的第二个属性。因此,上述示例之一的最短方式将是

$image_converter = new Converter('./images/logo.png', 256);

$table_html = $image_converter->get_as_table();

echo $table_html;