I am trying to add Watermark of 800x100px to Images which can have any resolution greater than 800x100, i.e. 801x110, 1200x1000, 1300x200, 1920x1080, 4000x2010, etc.
I want to keep the aspect ratio of Watermark and image to 2:50. i.e., if image has 1000px width, then watarmark over image should occupy 20px(1000/50).
Here is my Function :
in helper.php
use Image;
//calling from Controller
public static function addwatermark ($name) {
$thumbnail = Image::make($name);
$imageWidth = $thumbnail->width();
$watermarkWidth = '800px';
$watermarkSource = 'public/img/watermark/watermark.png';
$thumbnail->insert($watermarkSource, 'bottom-left', 0, 0);
$thumbnail->save($name)->destroy();
}
in ImageController.php
$folder = 'public/folder/';
$large = 'myfile.jpeg';
Helper::addwatermark($folder.$large);
I think this should work for you:
public static function addwatermark ($name) {
{
$thumbnail = Image::make($name);
$imageWidth = $thumbnail->width();
$watermarkSource = Image::make(public_path('img/watermark/watermark.png'));
$watermarkSize = round(20 * $imageWidth / 50);
$watermarkSource->resize($watermarkSize, null, function ($constraint) {
$constraint->aspectRatio();
});
$thumbnail->insert($watermarkSource, 'bottom-left', 0, 0);
$thumbnail->save($name)->destroy();
}
If I understand what you mean :
aspect ratio of watermark is 8:1 ( width of watermark is 8 times larger of its height )
and based on aspect ratio 2:50 or 1:25, width of the image is 25 times larger than width of watermark.
for example: if image has 1000px width, the watermark width, should be 40px.
so we also pass the width of image toaddwatermark
function in helper.php.
use Image;
public static function addwatermark ($name, $imageWidth) {
$watermarkWidth = $imageWidth / 25;
$watermarkHeight = $watermarkWidth / 8;
$watermarkSource = 'public/img/watermark/watermark.png';
$watermark= Image::make($name)->resize($watermarkWidth, $watermarkHeight)->insert($watermarkSource, 'bottom-left', 0, 0);
$watermark->save($name)->destroy();
}
if I get it wrong, please let me know, thank you.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.