Created
April 3, 2016 17:22
-
-
Save sharifbdp/77e161570389c396dccc9f3711f7e31f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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