I want to use Joomla's way to handle images in my component. My component offers a field to select a folder where some images are stored (used as gallery upon the related item). In dev everything works fine but on on staging the Website is currently available via domain.com/sub. The whole Joomla Installation is placed in this "sub" folder on the hosting environement. Now images in this gallery within my component are not working because of a wrong path on staging. But the images from the Joomla Media Field (item image) are working (the path there is correct interpreted by joomla itself - I think this is because of the way the image field stores the metadata and creates the image routes.
Is there a Helper class where i can insert the JPATH to an image and get the "joomla like" relativePath#joomlaImage://local-images/relativePath?width... out off?
I cand find anythin related in the Docs on how about create Image Metadata like the Image field does.
I think this could solve my path issues
Edit: Added Helper Class Code including quick and dirty fix
abstract class GalleryHelper {
public static function getGalleryImages($type, $selectedFolder): array
{
$galleryImages = [];
if($type === 'packages'){
$folderToEquipmentImages = '/images/equipmentmanager/'.$type.'/galleries/';
}else{
$folderToEquipmentImages = '/images/equipmentmanager/'.$type.'/';
}
$imgsPath = JPATH_SITE . $folderToEquipmentImages . $selectedFolder;
if (is_dir($imgsPath))
{
// Allowed filetypes
$allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');
// Also allow filetypes in uppercase
$allowedExtensions = array_merge($allowedExtensions, array_map('strtoupper', $allowedExtensions));
// Build the filter. Will return something like: "jpg|jpeg|png|JPG|JPEG|PNG|gif|GIF"
$filter = implode('|', $allowedExtensions);
$filter = "^.*\.(" . implode('|', $allowedExtensions) . ")$";
// Get the files
$galleryImages = Folder::files($imgsPath, $filter, false, false, $exclude = array('index.html'));
// Prepend the folder path to the filenames
$galleryImages = array_map(function($img) use ($folderToEquipmentImages, $selectedFolder) {
$folderToEquipmentImages = preg_replace('#/#', '', $folderToEquipmentImages, 1);
return \JUri::base() . $folderToEquipmentImages . $selectedFolder . '/' . $img;
}, $galleryImages);
}
return $galleryImages;
}
}
As you can see i had to use the slash at the beginning to find the images in the filestystem and then remove it later in the foreach loop to generate the full URI
joomlaImage
thing is specific to Media component. It would help if you posted the code you're using to build image paths. And also the field you're using to select the folder.