Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 1, 2024 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save code-boxx/d1bf23748b52a960bfe2078fe0f99948 to your computer and use it in GitHub Desktop.
Save code-boxx/d1bf23748b52a960bfe2078fe0f99948 to your computer and use it in GitHub Desktop.
PHP add watermark to image

PHP ADD WATERMARK TO IMAGE

https://code-boxx.com/add-watermark-php/

IMAGES

source watermark

NOTES

  1. Make sure that the GD extension is enabled in php.iniextension=gd (or gd2 prior to PHP8).
  2. For the text watermarks, change the font file $fontFile to your own.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (X) QUICK FUNCTION REFERENCE
// imagecolorallocatealpha (IMAGE, RED, GREEN, BLUE, OPACITY)
// imagettftext (IMAGE, SIZE, ANGLE, X, Y, COLOR, FONT, TEXT)
// imagepng (IMAGE, FILE)
// (A) CREATE IMAGE OBJECT
$source = "source.png";
$target = "demoA.png";
$img = imagecreatefrompng($source);
// (B) WRITE TEXT TO IMAGE (TOP LEFT CORNER)
$txt = "Copyright";
$fontFile = "C:/Windows/Fonts/arial.ttf"; // CHANGE TO YOUR OWN
$fontSize = 24;
$fontColor = imagecolorallocatealpha($img, 255, 255, 255, 0);
$posX = 0; $posY = 24;
imagettftext($img, $fontSize, 0, $posX, $posY, $fontColor, $fontFile, $txt);
// (C) SAVE TO FILE
imagepng($img, $target);
echo "Saved to $target";
<?php
// (X) QUICK FUNCTION REFERENCE
/* imagecopy (
* DESTINATION, SOURCE,
* DESTINATION-X, DESTINATION-Y,
* SOURCE-X, SOURCE-Y,
* SOURCE-WITDTH, SOURCE-HEIGHT
* )
*/
// (A) CREATE IMAGE OBJECTS
$sourceS = "source.png";
$sourceW = "watermark.png";
$target = "demoB.png";
$imgS = imagecreatefrompng($sourceS);
$imgW = imagecreatefrompng($sourceW);
// (B) APPLY WATERMARK (TOP-LEFT CORNER)
$posX = 0; $posY = 0;
imagecopy(
$imgS, $imgW, // copy watermark onto source image
$posX, $posY, // place watermark at top left corner
0, 0, imagesx($imgW), imagesY($imgW) // copy watermark image without clipping
);
// (C) SAVE TO FILE
imagepng($imgS, $target);
echo "Saved to $target";
<?php
// (A) CREATE IMAGE OBJECT
$source = "source.png";
$target = "demoC.png";
$img = imagecreatefrompng($source);
// (B) WRITE TEXT TO IMAGE (CENTER)
// (B1) TEXT & FONT
$txt = "Copyright";
$fontFile = "C:/Windows/Fonts/arial.ttf"; // CHANGE TO YOUR OWN
$fontSize = 24;
$fontColor = imagecolorallocatealpha($img, 0, 255, 255, 0);
// (B2) SOURCE IMAGE DIMENSIONS
$widthS = imagesx($img);
$heightS = imagesy($img);
// (B3) TEXT BOX DIMENSIONS
$sizeT = imagettfbbox($fontSize, 0, $fontFile, $txt);
$widthT = max([$sizeT[2], $sizeT[4]]) - min([$sizeT[0], $sizeT[6]]);
$heightT = max([$sizeT[5], $sizeT[7]]) - min([$sizeT[1], $sizeT[3]]);
// (B4) CALCULATE CENTER POSITION
$posX = CEIL(($widthS - $widthT) / 2);
$posY = CEIL(($heightS - $heightT) / 2);
if ($posX < 0 || $posY < 0) { exit("Text is too long"); } // optional
// (C) WRITE TEXT TO IMAGE & SAVE
imagettftext($img, $fontSize, 0, $posX, $posY, $fontColor, $fontFile, $txt);
// (D) SAVE TO FILE
imagepng($img, $target);
echo "Saved to $target";
<?php
// (A) CREATE IMAGE OBJECT
$sourceS = "source.png";
$sourceW = "watermark.png";
$target = "demoD.png";
$imgS = imagecreatefrompng($sourceS);
$imgW = imagecreatefrompng($sourceW);
// (B) POSITION CALCULATIONS
// (B1) SOURCE & WATERMARK DIMENSIONS
$widthS = imagesx($imgS);
$heightS = imagesY($imgS);
$widthW = imagesx($imgW);
$heightW = imagesY($imgW);
// (B2) CENTER POSITION
$posX = CEIL(($widthS - $widthW) / 2);
$posY = CEIL(($heightS - $heightW) / 2);
if ($posX < 0 || $posY < 0) { exit("Watermark image is too large"); } // optional
// (C) APPLY WATERMARK
imagecopy(
$imgS, $imgW, // copy watermark onto source image
$posX, $posY, // place watermark at center
0, 0, $widthW, $heightW // copy watermark image without clipping
);
// (D) SAVE TO FILE
imagepng($imgS, $target);
echo "Saved to $target";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment