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); //ΠΡΠ²ΠΎΠ΄ΠΈΠΌ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ
Imagine\Imagelibrary, and byresizemethod (I donβt think this is important) - XYZ