There are two images:
photo.png (500x500px) - the image on which you need to impose a watermark (watermark)
watermark.png (240x80px) - watermark

I change the size of watermark.png so that it photo.png no more than 30% of the area in width from photo.png , and not more than 30% in height.
I know how to get interest from the square, do something like this:

 $watermarkWidth = $photoWidth / 100 * 30; // 30% ΠΎΡ‚ photo.png $watermarkHeight = $photoHeight / 100 * 30; // 30% ΠΎΡ‚ photo.png 

But I don’t know how to ensure that the watermark.png not flattened after resizing, and doesn’t stretch too much, i.e. I need such sizes $watermarkWidth and $watermarkHeight at which watermark.png will be proportional to the original size, and at the same time the new size should be as close as possible to 30% of the size of photo.png

  • how is resize done? - Enikeyschik
  • @ Enikeik resize is performed by means of Imagine\Image library, and by resize method (I don’t think this is important) - XYZ
  • Re-read the question. What is the problem - in maintaining the proportions or in that the final size was as close as possible to 30% of the area? - Enikeyschik

2 answers 2

As I understood the task: you need to add a watermark to the original image, while changing its size relative to the original, without losing its proportionality.


Error (to the point)

Your mistake was that you tried to stretch the image of your watermark on 2 axes at once (x and y) . As I understand, the original images will be of different sizes, and the likelihood that 30% of both the width and the height will give you a proportional watermark is extremely small.


Decision

Do not chase after two hares and build your code on 2 axes. It is better to take only one and change the size of the watermark by leaning on it, after which we calculate the coefficient of this change and, while already running on it, work with the opposite axis.


Comment

In the examples below, the GD library functions will be used. For them to work, it must be present in your php. About GD can be found here: http://php.net/manual/ru/image.setup.php


Example

We get the image we need and the basis for the watermark.

 $im = imagecreatefromjpeg('1.jpg'); //ОсновноС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ $wm_source = imagecreatefrompng('2.png'); //Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ 

Find out the width and height of images of interest to us.

 $im_width = imagesx($im); //Π¨ΠΈΡ€ΠΈΠ½Π° изобраТСния $im_height = imagesy($im); //Высота изобраТСния $wm_source_width = imagesx($wm_source); //Π¨ΠΈΡ€ΠΈΠ½Π° основы Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ $wm_source_height = imagesy($wm_source); //Высота основы Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ 

And now the fun part. We calculate the new width for our watermark, relative to the width of the original image. It will be exactly 30% of the width of the original image.

 $wm_width = ($im_width / 100) * 30; //Π¨ΠΈΡ€ΠΈΠ½Π° Ρ€Π΅Π·Π°ΠΉΠ·Π½ΡƒΡ‚ΠΎΠΉ Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ (30% ΠΎΡ‚ изобраТСния) 

After that, we calculate the coefficient of change of the width of our watermark, dividing its size (obtained slightly higher) by the size of its base.

 $co = $wm_width / $wm_source_width; //ΠšΠΎΡΡ„Ρ„ΠΈΡ†ΠΈΠ΅Π½Ρ‚ измСнСния 

And already with the help of this coefficient, we calculate the height of our watermark.

 $wm_height = $wm_source_height * $co; //Высота Π²Π°Ρ‚Π΅Ρ€ΠΌΠΈΡ€ΠΊΠΈ 

Thus, we could not lose the proportionality of the watermark and meet the limit of 30% of the image. To put it in a simple way: we just took and changed the height of the watermark exactly by the same amount and changed its width, without losing proportionality there.

After all, if you multiply 2 numbers by the same thing, their relationship will not change, and this is proportionality.

Now let us indicate where our watermark will be located, in this case the lower left corner of the original image is taken. (no one bothers you choose another)

 $wm_x = $im_width - $wm_width; //ΠŸΠΎΠ·ΠΈΡ†ΠΈΡ x Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ $wm_y = $im_height - $wm_height; //ΠŸΠΎΠ·ΠΈΡ†ΠΈΡ y Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ 

Create the image itself for our watermark. It is worth noting that $ wm_source is a template, the basis, the original image of our watermark, while $ wm will be what we add to the image we need.

 $wm = imagecreatetruecolor($wm_width,$wm_height); //Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ Π½ΠΎΠ²ΡƒΡŽ Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΡƒ 

Change the size of the watermark, with the parameters that we defined earlier.

 imagecopyresampled($wm,$wm_source,0,0,0,0,$wm_width,$wm_height,$wm_source_width,$wm_source_height); //ИзмСняСм Ρ€Π°Π·ΠΌΠ΅Ρ€ Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ 

We insert the watermark on the image we need.

 imagecopy($im,$wm,$wm_x,$wm_y,0,0,$wm_width,$wm_height); //ВставляСм Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΡƒ 

Conclusion

That's all, I hope I helped in solving your problem. Below I will attach the full program code so you can run it without any problems. This is my first answer to StackOverflow, and I could be mistaken somewhere or ignore something, if this is true, then point out my mistakes, please.


Full program code

 header('Content-Type: image/png'); $im = imagecreatefromjpeg('1.jpg'); //ОсновноС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ $wm_source = imagecreatefrompng('2.png'); //Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ $im_width = imagesx($im); //Π¨ΠΈΡ€ΠΈΠ½Π° изобраТСния $im_height = imagesy($im); //Высота измСниния $wm_source_width = imagesx($wm_source); //Π¨ΠΈΡ€ΠΈΠ½Π° основы Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ $wm_source_height = imagesy($wm_source); //Высота основы Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ $wm_width = ($im_width / 100) * 30; //Π¨ΠΈΡ€ΠΈΠ½Π° Ρ€Π΅Π·Π°ΠΉΠ·Π½ΡƒΡ‚ΠΎΠΉ Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ (30% ΠΎΡ‚ изобраТСния) $co = $wm_width / $wm_source_width; //ΠšΠΎΠΎΡ„ΠΈΡ†Π΅Π½Ρ‚ измСнСния $wm_height = $wm_source_height * $co; //Высота Π²Π°Ρ‚Π΅Ρ€ΠΌΠΈΡ€ΠΊΠΈ $wm_x = $im_width - $wm_width; //ΠŸΠΎΠ·ΠΈΡ†ΠΈΡ x Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ $wm_y = $im_height - $wm_height; //ΠŸΠΎΠ·ΠΈΡ†ΠΈΡ y Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ $wm = imagecreatetruecolor($wm_width,$wm_height); //Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ Π½ΠΎΠ²ΡƒΡŽ Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΡƒ imagecopyresampled($wm,$wm_source,0,0,0,0,$wm_width,$wm_height,$wm_source_width,$wm_source_height); //ИзмСняСм Ρ€Π°Π·ΠΌΠ΅Ρ€ Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ imagecopy($im,$wm,$wm_x,$wm_y,0,0,$wm_width,$wm_height); //ВставляСм Π²Π°Ρ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΡƒ imagepng($im); //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ 

    Well, two options: Resize with distortion

     $watermarkWidth = $photoWidth / 100 * 30; // 30% ΠΎΡ‚ photo.png $watermarkHeight = $photoHeight / 100 * 30; // 30% ΠΎΡ‚ photo.png 

    Resize by minimum parameter

     $i = $watermarkWidth/$watermarkHeight; // ΡΠΎΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΠ΅ сторон Π²ΠΎΡ‚Π΅Ρ€ΠΌΠ°Ρ€ΠΊΠΈ if($photoWidth<$photoHeight){ $watermarkWidth = $photoWidth / 100 * 30; // 30% ΠΎΡ‚ photo. $watermarkHeight = $watermarkWidth/$i; }else{ $watermarkHeight = $photoHeight / 100 * 30; // 30% ΠΎΡ‚ photo. $watermarkWidth = $watermarkHeight*$i; } 

    In the first case, the watermark will be distorted depending on the image size, while in the second it will occupy 30% of the width or height