0

I have this type of link in my tag a and i want to keep the image link express in this way

Mage::getBaseUrl('media').'catalog/product/H/G/'.$simple_product->getSku().".jpg

i would use resize() function, for example in this mode:

'(Mage::getBaseUrl('media').'catalog/product/H/G/'.$simple_product->getSku().".jpg)->resize(null,450)'

obviously this does not work.

How can i do?

Thanks

0

You can resize image like this :

<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())->resize('500', '500'); ?>
  • Hi man. I just tried this but with my written image link the function -> resize () does not work. Url of image that i want use is write in this way: Mage::getBaseUrl('media').'catalog/product/H/G/'.$simple_product->getSku().".jpg – Gabriel91 Apr 24 '18 at 12:16
  • This is working code to resize base image, but it does not answer the question. – sv3n Apr 24 '18 at 16:51
0

You would have to create a new function to support this. Magento can't natively resize any image, it just contains functions to resize particular images throughout the website such as image thumbnails. Something like this would work:

// Set image and folder URL
$folderURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
$imageURL = $folderURL . $fileName;

// Set paths
$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 . $fileName;

// Create instance of Varien_Image
//$basePath - origin file location
$imageObj = new Varien_Image($basePath);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(FALSE);
$imageObj->keepFrame(FALSE);
//$width, $height - sizes you need (Note: when keepAspectRatio(TRUE), height would be ignored)
$imageObj->resize($width, $height);
//$newPath - name of resized image
$imageObj->save($newPath);

// Create a URL of the image
$resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "resized" . DS . $fileName;

// Function to resize the image
public function resizeImg($fileName, $width, $height = '')
{
    $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 . $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(TRUE);
            $imageObj->keepAspectRatio(FALSE);
            $imageObj->keepFrame(FALSE);
            $imageObj->resize($width, $height);
            $imageObj->save($newPath);
        }
        $resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "resized" . DS . $fileName;
     } else {
        $resizedURL = $imageURL;
     }
     return $resizedURL;
}

You could then call the new resizeImg function within your img src:

<img src="<?php echo resizeImg('https://url.com/image.jpg', '100', '100');?>" alt="" />

You can find full details of this function here.

Alternatively, you could try the below code that was written by Amit Bera:

$image = new Varien_Image('/full/fs/path/to/image.jpg');

// you cannot use method chaining with Varien_Image
$image->constrainOnly(false);
$image->keepFrame(true);
// avoid black borders by setting background colour
$image->backgroundColor(array(255,255,255));
$image->keepAspectRatio(true);
$image->resize(216, 139);
$image->save('/full/fs/path/to/save/to.jpg');

Details of his answer can be seen here.

  • Thanks for this but is very long. Any other solution for resize my string? – Gabriel91 Apr 24 '18 at 14:08
  • Unfortunately, Magento does not natively offer a facility to resize any image. They have functions in place to resize product images specifically. You need to utilize the Varien_Image class, so maybe this will help: stackoverflow.com/a/13874174/2053918. I've updated my answer that has a shorter function. I've not tested it myself, but it was written by a very knowledgeable Magento user :) – Liam McArthur Apr 24 '18 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.