-
-
Save Lukas238/8aa91fe0757bc973ccad to your computer and use it in GitHub Desktop.
PHP+GD Watermark
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 curPageURL() { | |
| $pageURL = 'http'; | |
| if ( isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ) { $pageURL .= "s"; } | |
| $pageURL .= "://"; | |
| $pageURL .= $_SERVER["SERVER_NAME"]; | |
| return $pageURL; | |
| } | |
| $result_image = false; | |
| $step = 1; | |
| if( isset($_POST['submit']) && $_FILES['captured_image']['size'] != 0 ){ | |
| /* | |
| * PHP function to image-watermark an image | |
| * http://salman-w.blogspot.com/2008/11/watermark-your-images-with-another.html | |
| * | |
| * Writes the given watermark image on the specified image | |
| * and saves the result as another image | |
| */ | |
| define('WATERMARK_OVERLAY_IMAGE', 'inc/ghosts.png'); | |
| define('WATERMARK_OVERLAY_OPACITY', 50); | |
| define('WATERMARK_OUTPUT_QUALITY', 90); | |
| function create_watermark($source_file_path, $output_file_path) | |
| { | |
| list($source_width, $source_height, $source_type) = getimagesize($source_file_path); | |
| if ($source_type === NULL) { | |
| return false; | |
| } | |
| switch ($source_type) { | |
| case IMAGETYPE_GIF: | |
| $source_gd_image = imagecreatefromgif($source_file_path); | |
| break; | |
| case IMAGETYPE_JPEG: | |
| $source_gd_image = imagecreatefromjpeg($source_file_path); | |
| break; | |
| case IMAGETYPE_PNG: | |
| $source_gd_image = imagecreatefrompng($source_file_path); | |
| break; | |
| default: | |
| return false; | |
| } | |
| $overlay_gd_image = imagecreatefrompng(WATERMARK_OVERLAY_IMAGE); | |
| $overlay_width = imagesx($overlay_gd_image); | |
| $overlay_height = imagesy($overlay_gd_image); | |
| //imagecopymerge( | |
| imagecopy( | |
| $source_gd_image, | |
| $overlay_gd_image, | |
| $source_width - $overlay_width, | |
| $source_height - $overlay_height, | |
| 0, | |
| 0, | |
| $overlay_width, | |
| $overlay_height | |
| ); | |
| imagejpeg($source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY); | |
| imagedestroy($source_gd_image); | |
| imagedestroy($overlay_gd_image); | |
| } | |
| /* | |
| * Uploaded file processing function | |
| */ | |
| define('UPLOADED_IMAGE_DESTINATION', 'tmp/'); | |
| define('PROCESSED_IMAGE_DESTINATION', 'uploads/'); | |
| function process_image_upload($Field) | |
| { | |
| $temp_file_path = $_FILES[$Field]['tmp_name']; | |
| $temp_file_name = basename($_FILES[$Field]['tmp_name']); | |
| list(, , $temp_type) = getimagesize($temp_file_path); | |
| if ($temp_type === NULL) { | |
| return false; | |
| } | |
| switch ($temp_type) { | |
| case IMAGETYPE_GIF: | |
| break; | |
| case IMAGETYPE_JPEG: | |
| break; | |
| case IMAGETYPE_PNG: | |
| break; | |
| default: | |
| return false; | |
| } | |
| $uploaded_file_path = UPLOADED_IMAGE_DESTINATION . $temp_file_name; | |
| $processed_file_path = PROCESSED_IMAGE_DESTINATION . preg_replace('/\\.[^\\.]+$/', '.jpg', $temp_file_name); | |
| move_uploaded_file($temp_file_path, $uploaded_file_path); | |
| $result = create_watermark($uploaded_file_path, $processed_file_path); | |
| if ($result === false) { | |
| return false; | |
| } else { | |
| return array($uploaded_file_path, $processed_file_path); | |
| } | |
| } | |
| /* | |
| * Here is how to call the function(s) | |
| */ | |
| $result = process_image_upload('captured_image'); | |
| if ($result === false) { | |
| echo '<br>An error occurred during file processing.'; | |
| } else { | |
| $step = 2; | |
| $result_image = $result[1]; | |
| } | |
| } | |
| ?> | |
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Document</title> | |
| <!-- STYLES --> | |
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" type="text/css"> | |
| <style> | |
| body{ | |
| padding: 2em; | |
| } | |
| #wrapper{ | |
| width: 800px; | |
| max-width: 100%; | |
| margin: 0 auto; | |
| } | |
| .betterInputFile{ | |
| max-width: 400px; | |
| } | |
| .betterInputFile input[type="text"]{ | |
| display: none; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="wrapper"> | |
| <?php if( $step == 1){ ?> | |
| <div id="step-1"> | |
| <form action="image2.php" method="post" enctype="multipart/form-data" > | |
| <input type="file" accept="image/*" id="capture" capture="camera" name="captured_image"> | |
| <button type="submit" class="btn" name="submit">Enviar</button> | |
| </form> | |
| </div> | |
| <?php }else if( $step == 2){ ?> | |
| <div id="step-2"> | |
| <img src="<?php echo $result_image; ?>" style="max-width: 90%;" alt=""> | |
| <br /> | |
| <a class="btn btn-primary" href="whatsapp://send?text=http://10.226.111.23/test31/<?php echo $result_image; ?>">Share this image</a> | |
| <hr> | |
| <a class="btn btn-success" href="http://10.226.111.23/test31/image2.php">Crea una nueva imagen</a> | |
| </div> | |
| <?php } ?> | |
| </div> | |
| <!-- SCRIPTS --> | |
| <script src="bower_components\jquery\dist\jquery.min.js"></script> | |
| <script src="bower_components\better-input-file\dist\betterInputFileButton.min.js"></script> | |
| <script> | |
| $(document).ready(function(){ | |
| $('input:file').betterInputFile(); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment