Magento: Create Watermark Image
It’s very very easy to create watermark image with Magento code. The Varien_Image class does the magic.
The function watermark creates watermark on the image.
Here is the code:
$mainImage = Mage::getBaseDir('media') . DS . 'test' . DS . 'image.jpg';
$watermarkImage = Mage::getBaseDir('media') . DS . 'test' . DS . 'watermark.jpg';
$image = new Varien_Image($mainImage);
// set watermark image width
$image->setWatermarkWidth(100);
// set watermark image height
$image->setWatermarkHeigth(100);
// create watermark image
// watermark($watermarkImage, $positionX=0, $positionY=0, $watermarkImageOpacity=30, $repeat=false)
$image->watermark($watermarkImage);
// save the watermark image
$image->save(Mage::getBaseDir('media'). DS . 'test' . DS . 'new.jpg');
Code Explanation
In the code above, you can see that I have an image named image.jpg inside media/test/ directory.
I have a watermark image named watermark.jpg.
I have set the watermark image width and height to 100px. You can omit these two functions if you don’t want to resize the watermark image.
The new image with watermark is saved inside media/test/ directory as new.jpg.
Hope this helps and thanks for reading.
Related posts:
- Magento: Get width height of image using Varien_Image class
- Magento: Crop image
- Magento: Rotate image
- Magento: Custom function to resize image proportionally
- jQuery: Grey out background and preview image as popup
- Magento: Resize Image
- Magento: Get height and width of Image
- jQuery: Preview Image with Zoom Effect
- jQuery: Preview Image with Tooltip Effect
- Magento: Create CMS Page & Static Block programmatically
