Skip to content

The `WaterMark` class is a PHP-based tool for adding dynamic watermarks and custom text to images. It supports resizing, positioning, and configuring watermarks and text, making it easy to create visually appealing images. With support for JPEG and PNG formats, the class offers flexibility in output and efficient memory management.

License

Notifications You must be signed in to change notification settings

salarizadi/php_watermark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Overview

The WaterMark class allows you to apply watermarks or custom text to images. It provides options for setting the position, size, and various configurations for both watermarks and text.


Usage

1. Initializing the Class

First, import the WaterMark class and instantiate it:

require_once 'WaterMark.php';

$wm = new WaterMark();

2. Setting the Base Image

To apply a watermark or text, you need to load the base image first:

$wm->setImage('path/to/image.jpg');

Parameters:

  • filename (string): Path to the image file on which you want to apply the watermark or text.

3. Adding a Watermark to the Image

To add a watermark, use the addMark method:

$wm->addMark('path/to/watermark.png', 0.2, 'bottom_right');

Parameters:

  • filename (string): Path to the watermark image.
  • size (float): A relative size for scaling the watermark (e.g., 0.2 for 20% of the base image width).
  • position (mixed): Either a predefined position like 'top_left', or a custom position in the form of an array [x, y] (e.g., [100, 50]).

4. Adding Text to the Image

To add custom text to the image, use the pasteText method:

$wm->pasteText('Sample Text', [
    'font' => 'path/to/font.ttf',
    'size' => 30,
    'color' => '#ffffff',
    'opacity' => 50,
    'position' => [100, 200]
]);

Parameters:

  • text (string): The text you want to add to the image.
  • config (array): Configuration options for the text.
    • font (string): Path to the TrueType font file.
    • size (int): Font size.
    • color (string): Text color in hexadecimal format (e.g., #ffffff).
    • opacity (int): Opacity level for the text (0-127, where 0 is fully opaque and 127 is fully transparent).
    • position (mixed): Either a predefined position or a custom position in the form of an array [x, y].

5. Exporting the Watermarked Image

After applying the watermark or text, you can export the final image using the export method:

$wm->export('output_image', 'jpg', 90);

Parameters:

  • filename (string): Name of the output file (without the extension).
  • format (string): File format, either 'jpg', 'jpeg', or 'png'. Default is 'jpg'.
  • quality (int): The quality of the exported image:
    • For JPEG: Value ranges from 1 to 100 (default is 100).
    • For PNG: Value ranges from 0 (no compression) to 9 (maximum compression). Default is 0.

Note: The quality parameter is optional and only relevant to the selected format. JPEG uses values from 1 to 100 for quality, while PNG uses 0 to 9 for compression.


6. Freeing Up Memory

After you're done processing the image, use the freeMemory method to release the allocated resources:

$wm->freeMemory();

Example Usage

Here’s a complete example of how to use the WaterMark class:

require_once 'WaterMark.php';

$wm = new WaterMark();

// Set the base image
$wm->setImage('image.jpg');

// Add a watermark at a custom position
$wm->addMark('watermark.png', 0.15, [300, 200]);

// Add text at a specific position
$wm->pasteText('Hello World', [
    'font' => 'path/to/font.ttf',
    'size' => 40,
    'color' => '#ffffff',
    'opacity' => 50,
    'position' => [100, 300]
]);

// Export the final watermarked image with quality settings
$wm->export('watermarked_image', 'jpg', 90);

// Free up resources
$wm->freeMemory();

Predefined Positions

You can use the following predefined positions for both watermarks and text:

  • 'top_left'
  • 'top_center'
  • 'top_right'
  • 'center'
  • 'middle_left'
  • 'middle_right'
  • 'bottom_left'
  • 'bottom_center'
  • 'bottom_right'

Alternatively, you can pass an array [x, y] for a custom position.


License

This class is open-source and free to use. Contributions are welcome!

About

The `WaterMark` class is a PHP-based tool for adding dynamic watermarks and custom text to images. It supports resizing, positioning, and configuring watermarks and text, making it easy to create visually appealing images. With support for JPEG and PNG formats, the class offers flexibility in output and efficient memory management.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages