Skip to content

Instantly share code, notes, and snippets.

@sharifbdp
Created April 3, 2016 17:22
Show Gist options
  • Select an option

  • Save sharifbdp/77e161570389c396dccc9f3711f7e31f to your computer and use it in GitHub Desktop.

Select an option

Save sharifbdp/77e161570389c396dccc9f3711f7e31f to your computer and use it in GitHub Desktop.
<?php
function getx($img, $font, $text) {
$image_width = imagesx($img);
$string_width = imagefontwidth($font) * strlen($text);
return $string_x_position = ($image_width - $string_width) / 2;
}
function gety($img, $font, $text) {
$image_height = imagesy($img);
$string_height = imagefontheight($font);
return $string_y_position = ($image_height - $string_height) / 2;
}
function watermark($input_image, $output_image) {
list($width, $height) = getimagesize($input_image);
$imageProperties = imagecreatetruecolor($width, $height);
// get input file extension
$ext = pathinfo($input_image, PATHINFO_EXTENSION);
if($ext == 'png'){
$targetLayer = imagecreatefrompng($input_image);
}
if($ext == 'jpg' || $ext == 'jpeg'){
$targetLayer = imagecreatefromjpeg($input_image);
}
if($ext == 'gif'){
$targetLayer = imagecreatefromgif($input_image);
}
imagecopyresampled($imageProperties, $targetLayer, 0, 0, 0, 0, $width, $height, $width, $height);
$WaterMarkText = 'CONFIDENTIAL';
$watermarkColor = imagecolorallocate($imageProperties, 255, 255, 255); // Font Color
$font = 10;
$xpos = getx($targetLayer, $font, $WaterMarkText);
$ypos = gety($targetLayer, $font, $WaterMarkText);
imagestring($imageProperties, 5, $xpos, $ypos, $WaterMarkText, $watermarkColor);
header('Content-type: image/jpeg');
// Save the image as $output_image
imagejpeg($imageProperties, $output_image);
imagepng($imageProperties);
imagedestroy($targetLayer);
imagedestroy($imageProperties);
}
$input_image = "./input.png"; // Input File
$output_image = "./output.jpg"; // Output File
watermark($input_image, $output_image);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment