Watermark_Lib documentation

# Product information and description

Product information:

Product Name: Watermark_Lib

Product Version: v 1.0

Author: Mohammed Ayman

Email: mohammedayman1070@gmail.com

Product description:

watermark_lib is a lightwight library that performing various types of watermark on many image types

types of image that accepted by watermark_lib:

  1. png
  2. jpg / jpeg
  3. webp
  4. gif
  5. bmp

please be aware that this library works only on the images that stored in your server or your local machine (if you use an Apache server as XAMPP), it does not work on URLs

# Requirements

  • PHP 8.2.7 or higher
  • GdImage version 2.1.0

# Installation

Installing this library is so easy and requires no time just extract the zip file that you already downloaded and then copy the watermark_script folder to your project directory

Warning:

you have to paste the watermark_script folder directly in your project directory, do not paste it in a sub directory of your project.

Alert:

please becarful while editing the source code, it may lead to unexpected behaviour

in your index.php file which is in your main directory of your project paste those two lines below

# Quick start

Create your first watermark on any image in just one line of code, assuming you alredy have watermark_lib integrated succesfuly in your project, if not so please follow the instruction here

just paste the follwing line in your php file

the above code uses WatermarkProducer class to create a text watermark on a given image, by calling the static method static_produce_text_watermark

as you can see the first argument is a string value that defines full path of the source image which we want to put watermark

second argument is a string value that defines the text that you want to use as watermark

the third one is a constant which descripes the type of the watermark, more informations of types of watermark in ‘How does WatermarkProducer work’ section

fifth argument is an array of RGB color which descripes the color of the text

next argument is the angle of the text that used as watermark

last one is the font type of the text.

Warning:

if the above code does not work properly, then check if your IDE automatically called the namespace of the library if not then copy and paste the following line at the top of your script file .

# How does WatermarkProducer work?

WatermarkProducer used to add a watermark on images, It contains four methods: two static methods and two non-static methods.

before explaining the four methods,We need to know about the configuration file of the watermark_lib, where all the constants and font types used by the library are defined.

First is the types of watermark:
  1. MID: is used to position the watermark in the middle of the image.
  2. TOP_LEFT: is used to position the watermark in the top left corner of the image.
  3. TOP_RIGHT: is used to position the watermark in the top right corner of the image.
  4. BOT_LEFT: is used to position the watermark in the bottom left corner of the image.
  5. COVER: is used to create watermark that covers the whole image.
Types of fonts:
  1. REGULAR
  2. BLACK
  3. BLACK_ITALIC
  4. BOLD
  5. BOLD_ITALIC
  6. ITALIC
  7. LIGHT
  8. LIGHT_ITALIC
  9. MEDIUM
  10. MEDIUM_ITALIC
  11. THIN
  12. THIN_ITALIC
Types of default sizes:
  1. CORNER_DEFAULT_SIZE: this is used to automatically caculate the size of the logo watermark in case it's in corner mode
  2. CORNER_LARGE_SIZE: this is used to get the largest possible size of the logo watermark in case it's in corner mode
  3. CORNER_SMALL_SIZE: this is used to get the smallest possible size of the logo watermark in case it's in corner mode
  4. COVER_DEFAULT_SIZE: this is used to automatically caculate the size of the logo watermark in case it's in cover mode

Now after we explained all constants in the configuration file, here is the types of the watermarks that the library can produce:

  1. Logo watermark: in this type of watermark we have two images: the source image where we will add the watermark and the logo image which is the watermark itself
  2. Text watermark: uses a given text as a watermark
WatermarkProducer methods:
  1. static_produce_text_watermark (
    string $path,
    string $text,
    int $type,
    array $color,
    int $angle,
    string $fontType,
    int $fontSize = _DEFAULT,
    int $alpha = 70,
    bool|string $store_path = false
    ): ?string

    this method creating a text watermark statically

    Paramerters:
    • path: it is the full path of the source image
      Warning:
      This parameter takes paths only, it does not support URLs
      Note:
      Types of images accepted by library: jpg/jpeg , png , bmp , gif
    • text: the text that will be used as watermark
      Warning:
      Text length must be at least 3 characters and at most 15 characters.
    • type: it is the type of watermark, choose between constants [COVER ,MID,...]
    • color: it is an array of integers that contains the RGB values as its items.
      Warning:
      the color array values must be in range [0 ,255]
    • angle: it is the angle of the given text, it's recommended to set angle to 0 when using position matermark and 20 when using cover watermark.
    • fontType: it's the font type of the given text
    • fontSize: it's the size of the given text, it's set to the default where the method will calculate the size automatically based on the type of watermark.
    • alpha: to define the transparency of the watermark
    • stor_path: is the path where the produced image will be stored, if it's value is false the produced image will be output directly
  2. static_produce_logo_watermark (
    string $image_path,
    string $logo_path,
    int $type,
    int $size = _DEFAULT,
    bool|string $store_path = false
    ): ?string

    this method will produce a logo watermark statically

    Parameters:
    • path: it is the full path of the source image
    • logo_path: it is the full path of the logo image
    • type: it is the type of watermark, choose between constants [COVER ,MID,...]
    • size: is the size of the logo image and it's set to the default setting where the method will calculate the size automatically based on the type of the watermark
    • stor_path: is the path where the produced image will be stored, if it's value is false the produced image will be output directly
  3. produce_text_watermark
    (
    string $path,
    string $text,
    int $type,
    array $color,
    int $angle,
    string $fontType,
    int $fontSize = _DEFAULT,
    int $alpha = 70,
    bool|string $store_path = false
    ): ?string

    same as the static_produce_text_watermark method, you have to instantiate an object from the WatermarkProducer class to use it

  4. produce_logo_watermark
    (
    string $image_path,
    string $logo_path,
    int $type,
    int $size = _DEFAULT,
    bool|string $store_path = false
    ): ?string

    same as the static_produce_logo_watermark method, you have to instantiate an object from the WatermarkProducer class to use it

Note:
it's recommended to use the WatermarkProducer methods within try-catch clause