Skip to main content

Components & Helpers API Reference

This document provides documentation for components and helpers used throughout the Yoneily system.

Components

Components are reusable pieces of controller logic that can be shared across multiple controllers.

PasswordHelperComponent

A utility component for generating random passwords. Location: app/controllers/components/password_helper.php

Methods

generatePassword($length = 8)
Generates a random password from a predefined set of characters.
length
integer
default:"8"
Length of the password to generate
Character Set: 0123456789bcdfghjkmnpqrstvwxyz Algorithm:
  1. Uses mt_rand() for random character selection
  2. Ensures no duplicate characters in the password
  3. Continues until desired length is reached
Returns: string - The generated password Example Usage:
// In a controller
$this->PasswordHelper->generatePassword(10);
// Returns: "3k5m9p2x7q"
Use Cases:
  • Password recovery functionality
  • Initial password generation for new users
  • Temporary password creation

phpThumb Component

Image manipulation and thumbnail generation component. Location: app/controllers/components/phpThumb.php Note: This is a third-party library (27,563 bytes) used throughout the application for advanced image processing. Features:
  • Image resizing
  • Thumbnail generation
  • Format conversion
  • Quality adjustment
Usage Context: Used in controllers for processing uploaded images before saving them to different directories (thumbnails, normal size, slider size, etc.).

Helpers

Helpers are used in views to format and present data to users.

ImageHelper

Automatically resizes images and generates properly formatted HTML image tags. Location: app/views/helpers/image.php
Version: 1.1
Authors: Josh Hundley, Jorge Orpinel

Properties

Methods

resize(path,path, width, height,height, aspect = true, htmlAttributes=array(),htmlAttributes = array(), return = false)
Automatically resizes an image and returns a formatted IMG tag.
path
string
required
Path to the image file, relative to the webroot/img/ directory
width
integer
required
Width of the returned image in pixels
height
integer
required
Height of the returned image in pixels
aspect
boolean
default:"true"
Whether to maintain aspect ratio during resize
htmlAttributes
array
default:"[]"
Array of HTML attributes for the <img> tag (e.g., ['alt' => 'Description', 'class' => 'thumbnail'])
return
boolean
default:"false"
Whether to return the value or output it directly
Returns: mixed - Either string HTML or echoes the value Supported Image Types:
  1. GIF
  2. JPEG
  3. PNG
  4. SWF
  5. PSD
  6. WBMP
Features:
  1. Automatic Caching
    • Checks if resized image already exists in cache
    • Verifies cache is up-to-date with source file
    • Only regenerates if needed
  2. Aspect Ratio Preservation
    • Automatically adjusts dimensions when $aspect = true
    • Calculates proper width or height to maintain proportions
    • Prevents image distortion
  3. Smart Resizing
    • Uses imagecreatetruecolor() and imagecopyresampled() for high quality
    • Falls back to imagecreate() and imagecopyresized() if needed
    • Handles both upscaling and downscaling
  4. Cache Management
    • Stores resized images in uploads/resized/ directory
    • Names cached files as {width}x{height}_{originalname}
    • Automatically updates cache when source file changes
Example Usage:
// In a view file
<?php echo $this->Image->resize('products/laptop.jpg', 300, 200); ?>
// Outputs: <img src="/uploads/resized/300x200_laptop.jpg" alt="thumb" />

// With custom attributes
<?php echo $this->Image->resize(
    'products/laptop.jpg', 
    300, 
    200, 
    true,
    ['alt' => 'Laptop Computer', 'class' => 'product-image']
); ?>
// Outputs: <img src="/uploads/resized/300x200_laptop.jpg" alt="Laptop Computer" class="product-image" />

// Without aspect ratio
<?php echo $this->Image->resize('banner.jpg', 1200, 400, false); ?>
// Outputs: <img src="/uploads/resized/1200x400_banner.jpg" alt="thumb" />
Algorithm Details:
  1. Path Resolution:
    $fullpath = ROOT/APP_DIR/WEBROOT_DIR/$theme/uploads/
    
  2. Aspect Ratio Calculation:
    if (($size[1]/$height) > ($size[0]/$width))
        $width = ceil(($size[0]/$size[1]) * $height);
    else
        $height = ceil($width / ($size[0]/$size[1]));
    
  3. Cache File Naming:
    $cachefile = "{width}x{height}_" . basename($path);
    
  4. Freshness Check:
    if (filemtime($cachefile) < filemtime($source))
        // Regenerate cache
    
Performance Optimization:
  • Only processes images when cache is missing or outdated
  • Reuses cached images for repeat requests
  • Efficient dimension calculations to minimize processing
Error Handling:
  • Returns null if source image doesn’t exist
  • Validates image with getimagesize()
  • Handles missing cache directory gracefully
Use Cases:
  1. Product Thumbnails:
    $this->Image->resize($product['Gallery']['thumbnails'], 124, 124);
    
  2. Responsive Images:
    // Mobile
    $this->Image->resize($image, 480, 320);
    
    // Desktop
    $this->Image->resize($image, 1920, 1080);
    
  3. Gallery Previews:
    foreach ($images as $img) {
        echo $this->Image->resize($img['path'], 200, 150, true, [
            'class' => 'gallery-thumb',
            'alt' => $img['title']
        ]);
    }
    

Integration with SimpleImage

The controllers use the SimpleImage class (imported from Vendor) for additional image processing:

SimpleImage Usage in Controllers

Common Operations:

1. Loading Image

$image = new SimpleImage();
$image->load($original_file_path);

2. Resizing by Width

if($image->getWidth() > 450) {
    $image->resizeToWidth(450);
}

3. Resizing by Height

if($image->getHeight() > 280) {
    $image->resizeToHeight(280);
}

4. Saving Processed Image

$image->save($output_path);

Standard Image Sizes

Promotions (Promos)

  • Thumbnail: 450×280 pixels
  • Slider: 779×280 pixels

Products (Galleries)

  • Thumbnail: 124×124 pixels
  • Normal: 600×480 pixels

Stores (Locales)

  • Thumbnail: 96×72 pixels
  • Slider: 600×480 pixels
  • Location: 600×480 pixels

Videos

  • Thumbnail: 96×72 pixels

Helper Integration Examples

Using ImageHelper with Database Fields

// In a view file displaying products
<div class="product-grid">
    <?php foreach ($products as $product): ?>
        <div class="product-item">
            <?php echo $this->Image->resize(
                'galeria/thumbnails/' . $product['Gallery']['thumbnails'],
                124,
                124,
                true,
                [
                    'alt' => $product['Gallery']['texto_galeria'],
                    'class' => 'product-thumbnail'
                ]
            ); ?>
            <h3><?php echo h($product['Gallery']['texto_galeria']); ?></h3>
            <p>$<?php echo h($product['Gallery']['precio']); ?></p>
        </div>
    <?php endforeach; ?>
</div>

Dynamic Sizing Based on Context

// Different sizes for different contexts
<?php if ($context == 'list'): ?>
    <?php echo $this->Image->resize($image, 100, 100); ?>
<?php elseif ($context == 'detail'): ?>
    <?php echo $this->Image->resize($image, 600, 480); ?>
<?php else: ?>
    <?php echo $this->Image->resize($image, 300, 200); ?>
<?php endif; ?>

Best Practices

For Components

  1. PasswordHelper Usage:
    // Always specify length for sensitive operations
    $tempPassword = $this->PasswordHelper->generatePassword(12);
    
    // Send via email, never display on screen
    $this->_sendPasswordEmail($user, $tempPassword);
    
  2. Error Handling:
    if (!$newPassword = $this->PasswordHelper->generatePassword(10)) {
        $this->Session->setFlash('Error generating password');
        return false;
    }
    

For Helpers

  1. Always Provide Alt Text:
    $this->Image->resize($path, 300, 200, true, [
        'alt' => 'Descriptive text for accessibility'
    ]);
    
  2. Consistent Sizing:
    // Define constants for standard sizes
    define('THUMB_WIDTH', 124);
    define('THUMB_HEIGHT', 124);
    
    $this->Image->resize($image, THUMB_WIDTH, THUMB_HEIGHT);
    
  3. Performance Considerations:
    // Cache-friendly: use consistent dimensions
    $this->Image->resize($image, 300, 200);  // Good
    
    // Avoid: random dimensions create many cache files
    $this->Image->resize($image, 301, 199);  // Bad
    

Security Considerations

PasswordHelper

  • Generated passwords exclude ambiguous characters (0/O, 1/l, etc.)
  • Uses cryptographically strong mt_rand()
  • Ensures uniqueness within generated password

ImageHelper

  • Validates file existence before processing
  • Uses getimagesize() to verify image integrity
  • Sanitizes file paths to prevent directory traversal
  • Only processes whitelisted image types

File Upload Security

When using with MeioUpload behavior:
'allowed_mime' => array(
    'image/gif', 
    'image/jpeg', 
    'image/pjpeg', 
    'image/png'
),
'allowed_ext' => array(
    '.jpg', '.jpeg', '.png', '.gif'
)

Build docs developers (and LLMs) love