1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | <!DOCTYPE html> <html> <head> <title>Upload and Add Watermark to Image using PHP</title> <link rel= "stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> </head> <body> <style> .gallery{ width: 100%; text-align: center; } h5{ color: #545353; font-size: 18px; text-transform: uppercase; background: #edeaea; padding: 7px 5px 4px 5px; margin-top: 20px; } .gallery img{ max-width: 100%; padding: 10px; } </style> <?php // Path configuration $targetDir = "uploads/" ; $watermarkImagePath = 'img/tutorial101-blogspot-logo.png' ; $statusMsg = '' ; $showwatermark = '' ; if (isset( $_POST [ "submit" ])){ if (! empty ( $_FILES [ "file" ][ "name" ])){ // File upload path $fileName = basename ( $_FILES [ "file" ][ "name" ]); $targetFilePath = $targetDir . $fileName ; $fileType = pathinfo ( $targetFilePath ,PATHINFO_EXTENSION); // Allow certain file formats $allowTypes = array ( 'jpg' , 'png' , 'jpeg' ); if (in_array( $fileType , $allowTypes )){ // Upload file to the server if (move_uploaded_file( $_FILES [ "file" ][ "tmp_name" ], $targetFilePath )){ // Load the stamp and the photo to apply the watermark to $watermarkImg = imagecreatefrompng( $watermarkImagePath ); switch ( $fileType ){ case 'jpg' : $im = imagecreatefromjpeg( $targetFilePath ); break ; case 'jpeg' : $im = imagecreatefromjpeg( $targetFilePath ); break ; case 'png' : $im = imagecreatefrompng( $targetFilePath ); break ; default : $im = imagecreatefromjpeg( $targetFilePath ); } // Set the margins for the watermark $marge_right = 10; $marge_bottom = 10; // Get the height/width of the watermark image $sx = imagesx( $watermarkImg ); $sy = imagesy( $watermarkImg ); // Copy the watermark image onto our photo using the margin offsets and // the photo width to calculate the positioning of the watermark. imagecopy( $im , $watermarkImg , imagesx( $im ) - $sx - $marge_right , imagesy( $im ) - $sy - $marge_bottom , 0, 0, imagesx( $watermarkImg ), imagesy( $watermarkImg )); // Save image and free memory imagepng( $im , $targetFilePath ); imagedestroy( $im ); if ( file_exists ( $targetFilePath )){ $statusMsg = "The image with watermark has been uploaded successfully." ; $showwatermark = '<h5>Watermarked Image</h5> <div class = "gallery" > <img src= "uploads/'.$fileName.'" alt= "" > </div>'; } else { $statusMsg = "Image upload failed, please try again." ; } } else { $statusMsg = "Sorry, there was an error uploading your file." ; } } else { $statusMsg = 'Sorry, only JPG, JPEG, and PNG files are allowed to upload.' ; } } else { $statusMsg = 'Please select a file to upload.' ; } } ?> <div class = "container" > <h2>Upload and Add Watermark to Image using PHP</h2> <div class = "row" > <div class = "col-lg-12" > <div> <!-- Image upload form --> <form action= "" method= "post" enctype= "multipart/form-data" class = "form-inline" > <div class = "form-group" > <label for = "file" >Select Image File to Upload:</label> <input type= "file" name= "file" class = "form-control" > </div> <input type= "submit" name= "submit" value= "Upload" class = "btn btn-success" > </form> <?php echo "<p><h4>" . $statusMsg . "</h4></p>" ; echo $showwatermark ; ?> </div> </div> </div> </div> </body> </html> |
article
Friday, December 13, 2019
Upload and Add Watermark to Image using PHP
Upload and Add Watermark to Image using PHP