article

Friday, December 13, 2019

Upload and Add Watermark to Image using PHP

Upload and Add Watermark to Image using PHP
<!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" />  
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</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>

Related Post