0

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

5
  • 1
    You don't need this. The 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.
    – Sharky
    Nov 28, 2022 at 9:08
  • @Sharky - sorry about that - i've now added my class, its working that way but it's dirty
    – Marco
    Nov 28, 2022 at 9:33
  • 1
    @Marco perhaps consider these cosmetic changes to your "dirty" code: 3v4l.org/vc8Cq Nov 28, 2022 at 23:48
  • @mickmackusa - thanks for your input i'll use your cosmetic changes in my project. Can you post it as answer incl the info from Sharky that its not needed to use the joomlaImage thing - so i can mark it as solution.
    – Marco
    Nov 29, 2022 at 7:12
  • 1
    I think, because my code doesn't actually solve anything, I'd rather @Sharky form my code into an answer. If Sharky declines to post an answer, you may post an answer yourself. Nov 29, 2022 at 7:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.