Dimitar
Dimitar

Reputation: 1928

How to put a watermark text across the whole image in diagonal position?

I have connected my Google Drive with my website and im getting images in this format: https://googledrive.com/host/{$GoogleID}. What i want to do is to add a text (not image) watermark to all the images im getting from Google Drive. I already tried with:

Both of them dosent work for me or i cant get them to work i dont know why. I will give an example for an image url: https://googledrive.com/host/0B9eVkF94eohMRlBQVENRWE5mc2c

I have also tried the code from this answer, but it dosent work as well. I guess the problem should be that the files i'm getting from Google Drive are not with the file extention and maybe this cause the problem. This is only my guess...

UPDATE: i managed to show the photo on the website, but how to put the text in diagonal possition across all the photo like this enter image description here

Upvotes: 1

Views: 3851

Answers (2)

Dimitar
Dimitar

Reputation: 1928

Thanks to @Durim Jusaj for helping me out with this one and finally i got the answer after a lot of searching and dealing with a lot of problems i will share my knowledge with u guys. So i will post the code and i will explain what is the code doing.

First thing first. Include this function in your file. This function is finding the center of the image. I havent wrote it i found it in the php docs under the comments so maybe it can be done without it or this function can be modified to be better written.

function imagettftext_cr(&$im, $size, $angle, $x, $y, $color, $fontfile, $text)
{
    // retrieve boundingbox
    $bbox = imagettfbbox($size, $angle, $fontfile, $text);

    // calculate deviation
    $dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0;         // deviation left-right
    $dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0;        // deviation top-bottom

    // new pivotpoint
    $px = $x-$dx;
    $py = $y-$dy;

    return imagettftext($im, $size, $angle, $px, $py, $color, $fontfile, $text);
}

Load the image you want to apply the watermark to. I'm using Google Drive to extract my photos so thats why im using the file_get_contents, if you are the case like mine then use this, BUT otherwise use what is common and if your picture have extension like .jpg or .png you can use imagecreatefromjpeg or imagecreatefrompng. So it should be something like that $im = imagecreatefromjpeg('photo.jpeg'); for example.

$im = imagecreatefromstring(file_get_contents("https://drive.google.com/uc?id=" . $v['GoogleID']));

After that we need to calculate the position of watermark so we want the watermark to be in the center and to auto adjust its size based on the size of the photo. So on the first like we store all the attributes of the image to array. Second and third lines we calculate where is the center of the image (for me dividing it by 2.2 worked great, you can change this if u want to adjust the position. Last line is the size of the watermark according to the size of the image. This is very important as the watermark will be small or very big if u have images with different sizes.

list($width, $height, $type, $attr) = getimagesize("https://drive.google.com/uc?id=" . $v['GoogleID']);
$width = $width / 2.2;
$height = $height / 2.2;
$size = ($width + $height) / 4;

Set the content-type

header('Content-Type: image/png');

Create the image. I dont know why it should be done twice, but im following the php manual.

$im = imagecreatefromstring(file_get_contents("https://drive.google.com/uc?id=" . $v['GoogleID']));

Create some colors. So, if you want your watermark to be transparent (like mine) you have to use imagecolorallocatealpha, otherwise use imagecolorallocate. Last parameter is the transparency. You can google every function name for more info from the php doc.

$black = imagecolorallocatealpha($im, 0, 0, 0, 100);

The text to draw. Simple as that. Write what you want your text to be.

$text = 'nima.bg';

Replace path by your own font path. Put a font in your server so the code can take it (if you dont have already).

$font = 'fonts/ParsekCyrillic.ttf';

Adding all together. Basically you are creating the final image with the water mark with this function. The magic ;)

imagettftext_cr($im, $size, 20, $width, $height, $black, $font, $text);

Now this is the tricky part! If you want to just display the image without saving it to your server you can simply copy and paste this code, but the website will load very slow if you have more then 2-3 images. So, what i suggest you to do it to save the final images to your server and then display it. I know you will have duplicates, but this is the best choice i think.

ob_start();
imagepng($im);
$image = ob_get_contents();
ob_end_clean();
imagedestroy($im);

echo "<img src='data:image/png;base64," . base64_encode($image) . "'>";
}

OR you can save the images to your server and then display them which is much much faster. The best thing you can do is to process all the images create them with the watermark and then display them (so you have them ready to be show when a visitor visit your website).

imagepng($im, 'photo_stamp.png');
imagedestroy($im);

And the final result for me was that. enter image description here

UPDATE: As of 2017 you can extract the image using this link 'https://drive.google.com/uc?id=(GoogleID)' or you can just use the 'webContentLink' property of the Google_DriveFile Object (it will give you the same link).

Upvotes: 1

Durim Jusaj
Durim Jusaj

Reputation: 81

try to read image with file_get_contents or fopen and create image from string.

$im = imagecreatefromstring(file_get_contents("image url"));

and then use this example: http://php.net/manual/en/image.examples.merged-watermark.php

Upvotes: 1

Related Questions