We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    If you want to add watermark to the specific product image programmatically, then use below script.

    <?php 
    /*
    * @ custom script to add watermark image on the product image that has -f in the product image name
    */
    use MagentoFrameworkAppBootstrap;
    
    require __DIR__ . '/../app/bootstrap.php';
     
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
     
    $state = $objectManager->get('MagentoFrameworkAppState');
    $state->setAreaCode('frontend');
     
    try
    {   
        $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
        $categoryFactory = $objectManager->get('MagentoCatalogModelCategoryFactory');
        $category = $categoryFactory->create()->load(4); //Parent category
        
        /*
        * @ $cateid array of category IDs
        */
    
        $cateid = $category->getAllChildren(true); // get all subcategory id
    
        $cateinstance = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');
        $allcategoryproduct = $cateinstance->create()->addAttributeToSelect('*')->addCategoriesFilter(['in' => $cateid]);
    
        $fileSystem = $objectManager->create('MagentoFrameworkFilesystem');
        $mediaPath = $fileSystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath();    
    
        $productimages = array();
        foreach ($allcategoryproduct as $products) {
    
            $product = $objectManager->create('MagentoCatalogModelProduct')->load($products->getEntityId());
    
            $productimages = $product->getMediaGalleryImages();
            
    
            if(!empty($productimages)){             
                foreach($productimages as $productimage)
                {   
                    $imgname = substr(strrchr(rtrim($productimage['file'], '/'), '/'), 1); // Get specific image name
                    if(strpos($imgname, '-f') !== false){
                        $fullpath = $mediaPath.'catalog/product'.$productimage['file'];
                        if(file_exists($fullpath)){
                            watermark_image($fullpath, dirname(__FILE__)'/watermark-f.png', $fullpath);
                            echo $productimage['file']."n";
                        }
                    
                    }
                    
                }
            }
        }    
     
    }
    catch (Exception $e)
    {
        echo $e->getMessage();
        exit;
    }
    
    /*
    * @ watermark_image function for merge watermark image to the product image
    */
    function watermark_image($target, $wtrmrk_file, $newcopy) {
        $watermark = imagecreatefrompng($wtrmrk_file);
        imagealphablending($watermark, false);
        imagesavealpha($watermark, true);
        $img = imagecreatefromjpeg($target);
        $img_w = imagesx($img);
        $img_h = imagesy($img);
        $wtrmrk_w = imagesx($watermark);
        $wtrmrk_h = imagesy($watermark);
        
        imagecopy($img, $watermark, $img_w - $wtrmrk_w, $img_h - $wtrmrk_h, 0, 0, $wtrmrk_w, $wtrmrk_h);
    
        imagejpeg($img, $newcopy, 100);
        imagedestroy($img);
        imagedestroy($watermark);
    }
    
    ?>

     

    Fill the below form if you need any Magento relate help/advise/consulting.

    With Only Agency that provides a 24/7 emergency support.

      Get a Free Quote