esc-company/view-transformer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

esc-company/view-transformer

Composer 安装命令:

composer require esc-company/view-transformer

包简介

View Transformer make profile image or name if client's information is empty.

README 文档

README

code-style run-tests Packagist Version Packagist Downloads Packagist Dependency Version Packagist Stars License: CC BY-NC-ND 4.0 Packagist License

A PHP library for generating pet (dog/cat) avatar images and nicknames. Provides consistent images and nicknames based on user IDs, and includes WordPress content transformation features.

Features

  • 4,080 dog/cat nicknames (breed names + descriptive adjectives)
  • 80 dog avatar images
  • 41 cat avatar images
  • 3 image sizes supported (large, medium, small)
  • Transform WordPress content to HTML (YouTube embed, caption tag processing)
  • Laravel Blade support
  • Deterministic algorithm (same ID always returns same result)

Installation

composer require cable8mm/view-transformer

Quick Start

use Cable8mm\ViewTransformer\PrettyProfile;

// Generate nickname
$nickname = PrettyProfile::getInstance()->nickname(12345);
// Output: "Happy Poodle" (example)

// Get dog image URL
$dogImage = PrettyProfile::getInstance()->dog(12345);
// Output: "https://cabinet-pets.palgle.com/avatars/dog/61.png"

// Get cat image URL (medium size)
$catImage = PrettyProfile::getInstance()->cat(12345, 'medium');
// Output: "https://cabinet-pets.palgle.com/avatars/cat/medium/13.png"

API Reference

PrettyProfile

Generates nicknames and pet image URLs based on user ID.

Methods

nickname(int $id): string

Generates a nickname from user ID.

Parameters:

  • $id (int): User ID (must be >= 1)

Returns: Adjective + breed name combination (e.g., "Ordinary Nebelung")

Throws: InvalidArgumentException - If ID is 0 or negative

Example:

echo PrettyProfile::getInstance()->nickname(1);
//=> Ordinary Nebelung

echo PrettyProfile::getInstance()->nickname(2);
//=> Sexy Norwegian Forest
cat(int $id, ?string $size = null): string

Returns a cat image URL.

Parameters:

  • $id (int): User ID (must be >= 1)
  • $size (string|null): 'large', 'medium', 'small', or null (original size)

Returns: Image URL

Throws: InvalidArgumentException - If ID is invalid or size is not valid

Example:

echo PrettyProfile::getInstance()->cat(1);
//=> https://cabinet-pets.palgle.com/avatars/cat/1.png

echo PrettyProfile::getInstance()->cat(1, 'medium');
//=> https://cabinet-pets.palgle.com/avatars/cat/medium/1.png
dog(int $id, ?string $size = null): string

Returns a dog image URL.

Parameters:

  • $id (int): User ID (must be >= 1)
  • $size (string|null): 'large', 'medium', 'small', or null (original size)

Returns: Image URL

Throws: InvalidArgumentException - If ID is invalid or size is not valid

Example:

echo PrettyProfile::getInstance()->dog(1);
//=> https://cabinet-pets.palgle.com/avatars/dog/1.png

echo PrettyProfile::getInstance()->dog(1, 'large');
//=> https://cabinet-pets.palgle.com/avatars/dog/large/1.png
cats(?string $size = null): array

Returns array of all cat image URLs.

Parameters:

  • $size (string|null): 'large', 'medium', 'small', or null (original size)

Returns: Array of image URLs (41 items)

Example:

$cats = PrettyProfile::getInstance()->cats();
// Array of 41 image URLs

$cats = PrettyProfile::getInstance()->cats('medium');
// Array of 41 medium-sized image URLs
dogs(?string $size = null): array

Returns array of all dog image URLs.

Parameters:

  • $size (string|null): 'large', 'medium', 'small', or null (original size)

Returns: Array of image URLs (80 items)

Example:

$dogs = PrettyProfile::getInstance()->dogs();
// Array of 80 image URLs

$dogs = PrettyProfile::getInstance()->dogs('small');
// Array of 80 small-sized image URLs
profileImage(int $id, ?string $image = null, string $animal = 'dog'): string

Helper method for Laravel Blade templates.

Parameters:

  • $id (int): User ID
  • $image (string|null): Custom image URL (used if provided)
  • $animal (string): 'dog' or 'cat' (default: 'dog')

Returns: Image URL

Throws: InvalidArgumentException - If animal is not 'dog' or 'cat'

Example:

{{ PrettyProfile::profileImage(4123, animal: 'dog') }}
{{-- Output: https://cabinet-pets.palgle.com/avatars/dog/43.png --}}

{{ PrettyProfile::profileImage(4123, animal: 'cat') }}
{{-- Output: https://cabinet-pets.palgle.com/avatars/cat/10.png --}}

{{ PrettyProfile::profileImage(4123, image: 'https://example.com/custom.png') }}
{{-- Output: https://example.com/custom.png --}}
backgroundImage(?string $background_image = null): string

Returns a background image URL.

Parameters:

  • $background_image (string|null): Custom background image URL

Returns: Background image URL

Example:

echo PrettyProfile::backgroundImage();
//=> https://cabinet-pets.palgle.com/bg/bg-1.png

echo PrettyProfile::backgroundImage('https://example.com/bg.png');
//=> https://example.com/bg.png

WordBinder

Transforms WordPress content to HTML.

Methods

view(string $content): string

Transforms WordPress shortcodes to HTML.

Supported features:

  • Extract only images from [caption] tags
  • Convert [embed] tags to YouTube iframes
  • Remove empty paragraphs (<p>&nbsp;</p>)

Parameters:

  • $content (string): WordPress content

Returns: Transformed HTML

Example:

use Cable8mm\ViewTransformer\WordBinder;

// YouTube embed transformation
$content = '[embed]https://www.youtube.com/watch?v=XsJ9GFGkEOk[/embed]';
echo WordBinder::view($content);
//=> <iframe width="100%" height="100%" src="https://www.youtube.com/embed/XsJ9GFGkEOk" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

// Extract image from caption tag
$content = '[caption width=80]<img src="image.jpg" />[/caption]';
echo WordBinder::view($content);
//=> <img src="image.jpg" />

// Remove empty paragraphs
$content = '<p>Content</p><p>&nbsp;</p><p>More</p>';
echo WordBinder::view($content);
//=> <p>Content</p><p>More</p>
addBanner(string $content, string $bannerHtml, int $nthPTag = 0): string

Inserts banner HTML after the nth <p> tag in HTML content.

Parameters:

  • $content (string): HTML content
  • $bannerHtml (string): Banner HTML to insert
  • $nthPTag (int): Insert position (0 = at the beginning, default: 0)

Returns: HTML with banner inserted

Example:

$banner = '<a href="https://example.com">Banner</a>';
$content = '<p>Para1</p><p>Para2</p><p>Para3</p>';

// Insert banner at the beginning
echo WordBinder::addBanner($content, $banner, 0);
//=> <a href="https://example.com">Banner</a><p>Para1</p><p>Para2</p><p>Para3</p>

// Insert banner after 2nd paragraph
echo WordBinder::addBanner($content, $banner, 2);
//=> <p>Para1</p><p>Para2</p><a href="https://example.com">Banner</a><p>Para3</p>

// If n exceeds number of <p> tags, insert at beginning
echo WordBinder::addBanner($content, $banner, 10);
//=> <a href="https://example.com">Banner</a><p>Para1</p><p>Para2</p><p>Para3</p>

Usage Examples

Generate User Profile Images

use Cable8mm\ViewTransformer\PrettyProfile;

$userId = 393939;

// Generate nickname
$nickname = PrettyProfile::getInstance()->nickname($userId);
echo $nickname; // "Ordinary Nebelung"

// Get dog profile image
$dogImage = PrettyProfile::getInstance()->dog($userId);
echo $dogImage; // "https://cabinet-pets.palgle.com/avatars/dog/64.png"

// Get cat profile image (medium size)
$catImage = PrettyProfile::getInstance()->cat($userId, 'medium');
echo $catImage; // "https://cabinet-pets.palgle.com/avatars/cat/medium/10.png"

Get All Images

use Cable8mm\ViewTransformer\PrettyProfile;

// Get all dog images (original size)
$allDogs = PrettyProfile::getInstance()->dogs();

// Get all cat images (medium size)
$allCatsMedium = PrettyProfile::getInstance()->cats('medium');

// Generate preview
foreach ($allDogs as $index => $url) {
    echo "![Dog $index]($url)\n";
}

Laravel Blade Integration

{{-- Basic usage --}}
<img src="{{ PrettyProfile::profileImage($user->id, animal: 'dog') }}" alt="Profile">

{{-- Cat image --}}
<img src="{{ PrettyProfile::profileImage($user->id, animal: 'cat') }}" alt="Profile">

{{-- Custom image (takes priority) --}}
<img src="{{ PrettyProfile::profileImage($user->id, image: $user->custom_avatar) }}" alt="Profile">

WordPress Content Transformation

use Cable8mm\ViewTransformer\WordBinder;

// WordPress content with YouTube video
$wordpressContent = '
    <p>First paragraph</p>
    [embed]https://www.youtube.com/watch?v=abc123&t=30[/embed]
    <p>Second paragraph</p>
';

$html = WordBinder::view($wordpressContent);
// YouTube embed is converted to iframe

// Insert banner ad
$banner = '<div class="ad-banner">Advertisement</div>';
$contentWithBanner = WordBinder::addBanner($html, $banner, 1);
// Banner inserted after first paragraph

Exception Handling

use Cable8mm\ViewTransformer\PrettyProfile;

try {
    // Invalid ID (0 or negative)
    PrettyProfile::getInstance()->cat(0);
} catch (\InvalidArgumentException $e) {
    echo $e->getMessage();
    // "The value must be over 0, so a value of 0 is not valid."
}

try {
    // Invalid size
    PrettyProfile::getInstance()->dog(1, 'huge');
} catch (\InvalidArgumentException $e) {
    echo $e->getMessage();
    // "The value must be "large" or "medium" or "small", so a value of "huge" is not valid."
}

try {
    // Invalid animal
    PrettyProfile::profileImage(1, animal: 'rabbit');
} catch (\InvalidArgumentException $e) {
    echo $e->getMessage();
    // "The value must be dog or cat. rabbit is not valid."
}

Algorithm

Image Selection Algorithm

Uses the remainder of dividing user ID by the number of images. This ensures:

  • Deterministic: Same ID always returns the same image
  • Uniform distribution: If IDs are evenly distributed, images are evenly distributed
  • Unlimited: Works correctly even if ID exceeds image count
// Example: Dog images (80 total)
$id = 827342;
$imageNumber = ($id - 1) % 80 + 1; // 62
// User #827342 always uses image #62

Nickname Generation Algorithm

Selects prefix (adjective) and suffix (breed name) independently using modular arithmetic.

// Example: ID 1
$prefixIndex = (1 - 1) % 40; // 0 → First adjective
$nicknameIndex = (1 - 1) % 66; // 0 → First breed name
// Result: "Ordinary Nebelung" (example)

Preview

Dog artworks

Dog 1 Dog 2 Dog 3 Dog 4 Dog 5 Dog 6 Dog 7 Dog 8 Dog 9 Dog 10 Dog 11 Dog 12 Dog 13 Dog 14 Dog 15 Dog 16 Dog 17 Dog 18 Dog 19 Dog 20 Dog 21 Dog 22 Dog 23 Dog 24 Dog 25 Dog 26 Dog 27 Dog 28 Dog 29 Dog 30 Dog 31 Dog 32 Dog 33 Dog 34 Dog 35 Dog 36 Dog 37 Dog 38 Dog 39 Dog 40 Dog 41 Dog 42 Dog 43 Dog 44 Dog 45 Dog 46 Dog 47 Dog 48 Dog 49 Dog 50 Dog 51 Dog 52 Dog 53 Dog 54 Dog 55 Dog 56 Dog 57 Dog 58 Dog 59 Dog 60 Dog 61 Dog 62 Dog 63 Dog 64 Dog 65 Dog 66 Dog 67 Dog 68 Dog 69 Dog 70 Dog 71 Dog 72 Dog 73 Dog 74 Dog 75 Dog 76 Dog 77 Dog 78 Dog 79 Dog 80

Cat artworks

Cat 1 Cat 2 Cat 3 Cat 4 Cat 5 Cat 6 Cat 7 Cat 8 Cat 9 Cat 10 Cat 11 Cat 12 Cat 13 Cat 14 Cat 15 Cat 16 Cat 17 Cat 18 Cat 19 Cat 20 Cat 21 Cat 22 Cat 23 Cat 24 Cat 25 Cat 26 Cat 27 Cat 28 Cat 29 Cat 30 Cat 31 Cat 32 Cat 33 Cat 34 Cat 35 Cat 36 Cat 37 Cat 38 Cat 39 Cat 40 Cat 41

Formatting

composer lint
# Modify all files to comply with the PSR-12.

composer inspect
# Inspect all files to ensure compliance with PSR-12.

Test

composer test

License

The View Transformer project is open-sourced software licensed under the MIT license.

Artworks © 2020 by Samgu Lee is licensed under CC BY-NC-ND 4.0. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/

统计信息

  • 总下载量: 335
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固