简体   繁体   中英

Add a logo water mark diagonally to an image using PHP

I am trying to add an image watermark to a base image using PHP. I have written the code below to achieve it and am placing horizontally the watermark image centred on the base image.

<?php
header('content-type: image/jpeg');
$poster_img = dirname(__FILE__) . '/base.jpeg';
$water_img = dirname(__FILE__) . '/watermark.png';
list($poster_width, $poster_height) = getimagesize($poster_img);
list($water_width, $water_height) = getimagesize($water_img);
$posterimage = imagecreatefromjpeg($poster_img); // base image
$watermark = imagecreatefrompng($water_img); // company logo
$x = ($poster_width - $water_width) / 2;
$y = ($poster_height - $water_height) / 2;
imagecopy($posterimage, $watermark, $x, $y, 0, 0, $water_width, $water_height);
imagejpeg($posterimage, 'watermarked_image.jpg'); 
imagedestroy($posterimage);
imagedestroy($watermark); 
?>

我的代码的结果

However, I am looking for a solution to place the watermark 45 degrees inclined to the based image(centred and diagonally to the image). Please refer the image below

预期的

I have found solutions to place text watermark diagonally. Does anyone know how to place image watermark diagonally? Thanks in advance!

you propably want to use imagarotate() on your watermak image

https://www.php.net/manual/en/function.imagerotate.php

<?php
header('content-type: image/jpeg');
$poster_img = dirname(__FILE__) . '/base.jpeg';
$water_img = dirname(__FILE__) . '/watermark.png';
list($poster_width, $poster_height) = getimagesize($poster_img);
list($water_width, $water_height) = getimagesize($water_img);
$posterimage = imagecreatefromjpeg($poster_img); // base image
$watermark = imagecreatefrompng($water_img); // company logo
$x = ($poster_width - $water_width) / 2;
$y = ($poster_height - $water_height) / 2;
imagerotate(45, $watermark);
imagecopy($posterimage, $watermark, $x, $y, 0, 0, $water_width, $water_height);
imagejpeg($posterimage, 'watermarked_image.jpg'); 
imagedestroy($posterimage);
imagedestroy($watermark); 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM