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
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> </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> </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 "\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
Cat artworks
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
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-03