1

all. I'm trying to add custom watermark to product image with next code:

$this->helper('catalog/image')
    ->init($_product, 'small_image')
    ->resize(174)
    ->watermark($imagefile);

but it still displays a product small image resized without any modification. Any ideas?

  • So I guess the build in watermark in System > Configuration > General: Design - Product Image Watermarks won't work for you? Anyway. Could you be more specific on where you add this code? – Yiorgos Moschovitis Dec 4 '14 at 14:52
  • yes, default watermark feature doesn't fit my needs. I have manufacturers logos, and i want to add watermark to products images. I insert this code to src of an image in products list. Like this: <img height="174" width="174" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(174)->watermark($imagefile); ?> alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /> – Styopchik Dec 4 '14 at 15:20
2

finnaly, i figured out, how to put custom watermark to product image:

public function getWatermarkedImage(Mage_Catalog_Model_Product $product){

    $product->load('media_gallery');
    if ($product->getImage() && $product->getImage() != 'no_selection'){
        $basePath = Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath();
        $image = Mage::getModel('catalog/product_image');
        $image->setDestinationSubdir('watermarks');

        if(!file_exists($basePath . '/'. $product->getImage())) {
            return Mage::getBaseUrl('media') . '/catalog/product/placeholder/default/no-image.png';
        }
        $image->setBaseFile($product->getImage());
        //$processor = new Varien_Image($image->getBaseFile());
        //$image->setImageProcessor($processor);

        if (!file_exists($image->getNewFile())){

            $stamp = Mage::getModel('amshopby/value')->load($product->getManufacturer(),'option_id')->getImgMedium();
            $logoStamp = 'logo.png';
            if (!file_exists($basePath . '/watermark/default/' . $stamp)){
                if(!file_exists($basePath . '/watermark/default/')){
                    mkdir($basePath . '/watermark/default/', 0744, true);
                }
                copy(Mage::getBaseDir('media') . '/amshopby/' . $stamp, $basePath . '/watermark/default/' . $stamp);
            }
            try{

                $image->setWidth(600)->setKeepAspectRatio(true)->resize();
                $image->setWatermarkImageOpacity(100);
                if (file_exists($basePath . '/watermark/default/' . $logoStamp)){
                    $image->setWatermark($logoStamp, 'top-left', null, 170, 93, 100);
                }
                $image->setWatermark($stamp, 'bottom-right', null, 200, 129, 100);
                $image->saveFile();
            }
            catch (Exception $e) {
                Mage::log($e->getMessage());
                return false;
            }
        }
        return $image->getUrl();


    }
    else return Mage::getBaseUrl('media') . '/catalog/product/placeholder/default/no-image.png';

}

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.