Images helper

The images_helper.php file provides helper functions to handle image paths efficiently. These functions ensure that images are correctly resized and assigned default values when necessary.


addSizeToPath()

Purpose:

This function appends a given size string before the file extension in an image path. It is useful for handling different image resolutions without modifying the original filenames.

Parameters:

  • $path (string) – The original image file path.

  • $size (string) – The size to be appended before the file extension (e.g., "150x150").

Usage:

$imagePath = "/uploads/images/sample.jpg";
$resizedPath = addSizeToPath($imagePath, "150x150");

echo $resizedPath; 
// Output: "/uploads/images/sample_150x150.jpg"

img_square()

Purpose:

This function generates a square image URL (150x150) if the input contains a valid path. If no path is provided, it returns a default placeholder image.

Parameters:

  • $item (array/object) – An object or array containing:

    • path (string) – The original image path.

    • resize (string, optional) – The resize option indicating a required transformation.

Usage:

$item = ['path' => '/uploads/images/sample.jpg', 'resize' => '150x150'];

echo img_square($item); 
// Output: "/uploads/images/sample_150x150.jpg"

If the input does not contain a valid path:

$item = ['path' => '', 'resize' => ''];
echo img_square($item);
// Output: "/uploads/assets/150x150.webp"

img_vertical()

Purpose:

Similar to img_square, this function generates a vertically resized image URL (333x500). If no valid image is found, it returns a default placeholder image.

Parameters:

  • $item (array/object) – An object or array containing:

    • path (string) – The original image path.

    • resize (string, optional) – The resize option indicating a required transformation.

Usage:

$item = (object) ['path' => '/uploads/images/sample.jpg', 'resize' => '333x500'];
echo img_vertical($item); 
// Output: "/uploads/images/sample_333x500.jpg"

If the input does not contain a valid path:

$item = ['path' => '', 'resize' => ''];
echo img_vertical($item);
// Output: "/uploads/assets/333x500.webp" 

Last updated

Was this helpful?