Question

Ok I have an annoying issue. I have some code in a custom helper module that i use to resize images and save a cached copy. Now this does not work. However if i go into mage.php and change the declared variable DS from DIRECTORY_SEPARATOR to / it works perfectly. However then some community modules break who use the standard DS.

Here is my method:

public function resizeImg($fileName, $width, $height, $constrain = false, $ratio = false)
{
    $folderURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
    $imageURL = $folderURL . $fileName;

    $basePath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . $fileName;
    $newPath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . "resized" . DS . $width . DS . $fileName;
    //if width empty then return original size image's URL
    if ($width != '') {
        //if image has already resized then just return URL
        if (file_exists($basePath) && is_file($basePath)/* && !file_exists($newPath)*/) {
            $imageObj = new Varien_Image($basePath);
            $imageObj->constrainOnly($constrain);
            $imageObj->keepAspectRatio($ratio);
            $imageObj->keepFrame(FALSE);
            $imageObj->resize($width, $height);
            $imageObj->save($newPath);
        }
        $resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "resized" . DS . $width . DS . $fileName;
     } else {
        $resizedURL = $imageURL;
     }
     return $resizedURL;
}

And the working app/Mage.php

define('DS', DIRECTORY_SEPARATOR);

Now if i change that DS to '/' it works fine.

I then tried the following:

define('DS2', '/');

Then changes the DS to DS2 in my method, but this also did not work, nor does changing it to a string or variable.

from my searching the Directory selector is defined by the type of server, this should be a linux server and yet only when I use DS myself it breaks and tried to output \ instead of /, everywhere else it correctly outputs /.

The hell am i doing wrong?

Was it helpful?

Solution

You are confusing URLs with directories.

getBaseUrl is for URLs, which will always be separated by /

getBaseDir is for Directories, which will have a variable DS defined by PHP's constant variable DIRECTORY_SEPARATOR - which is what Magento shortens down to DS

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top