Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6902067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:45:08+00:00 2026-05-27T07:45:08+00:00

Hi I am integrating the watermark code http://koivi.com/php-gd-image-watermark/ This script works well. But I

  • 0

Hi I am integrating the watermark code http://koivi.com/php-gd-image-watermark/

This script works well. But I am making a poster with user upload image on the background.
I need a small tweak in the output. I have only one image in the watermark directory.

My watermark image is 500px*725px. So I need the output to be croped according to the watermark image size.
The current output produced is according to the user uploaded image size ratio.

Here is the code:

<?php
/*
KOIVI GD Image Watermarker for PHP Copyright (C) 2004 Justin Koivisto
Version 2.0
Last Modified: 12/9/2004
*/

    ob_start();

    $disp_width_max=150;                    // used when displaying watermark choices
    $disp_height_max=80;                    // used when displaying watermark choices
    $edgePadding=15;                        // used when placing the watermark near an edge
    $quality=100;                           // used when generating the final image
    $default_watermark='Sample-trans.png';  // the default image to use if no watermark was chosen
    
    if(isset($_POST['process'])){
        // an image has been posted, let's get to the nitty-gritty
        if(isset($_FILES['watermarkee']) && $_FILES['watermarkee']['error']==0){
        
            // be sure that the other options we need have some kind of value
            if(!isset($_POST['save_as'])) $_POST['save_as']='jpeg';
            if(!isset($_POST['v_position'])) $_POST['v_position']='center';
            if(!isset($_POST['h_position'])) $_POST['h_position']='center';
            if(!isset($_POST['wm_size'])) $_POST['wm_size']='1';
            if(!isset($_POST['watermark'])) $_POST['']=$default_watermark;
        
            // file upload success
            $size=getimagesize($_FILES['watermarkee']['tmp_name']);
            if($size[2]==2 || $size[2]==3){
                // it was a JPEG or PNG image, so we're OK so far
                
                $original=$_FILES['watermarkee']['tmp_name'];
                $target_name=date('YmdHis').'_'.
                    // if you change this regex, be sure to change it in generated-images.php:26
                    preg_replace('`[^a-z0-9-_.]`i','',$_FILES['watermarkee']['name']);
                $target=dirname(__FILE__).'/results/'.$target_name;
                $watermark=dirname(__FILE__).'/watermarks/'.$_POST['watermark'];
                $wmTarget=$watermark.'.tmp';

                $origInfo = getimagesize($original); 
                $origWidth = $origInfo[0]; 
                $origHeight = $origInfo[1]; 

                $waterMarkInfo = getimagesize($watermark);
                $waterMarkWidth = $waterMarkInfo[0];
                $waterMarkHeight = $waterMarkInfo[1];
        
                // watermark sizing info
                if($_POST['wm_size']=='larger'){
                    $placementX=0;
                    $placementY=0;
                    $_POST['h_position']='center';
                    $_POST['v_position']='center';
                    $waterMarkDestWidth=$waterMarkWidth;
                    $waterMarkDestHeight=$waterMarkHeight;
                    
                    // both of the watermark dimensions need to be 5% more than the original image...
                    // adjust width first.
                    if($waterMarkWidth > $origWidth*1.05 && $waterMarkHeight > $origHeight*1.05){
                        // both are already larger than the original by at least 5%...
                        // we need to make the watermark *smaller* for this one.
                        
                        // where is the largest difference?
                        $wdiff=$waterMarkDestWidth - $origWidth;
                        $hdiff=$waterMarkDestHeight - $origHeight;
                        if($wdiff > $hdiff){
                            // the width has the largest difference - get percentage
                            $sizer=($wdiff/$waterMarkDestWidth)-0.05;
                        }else{
                            $sizer=($hdiff/$waterMarkDestHeight)-0.05;
                        }
                        $waterMarkDestWidth-=$waterMarkDestWidth * $sizer;
                        $waterMarkDestHeight-=$waterMarkDestHeight * $sizer;
                    }else{
                        // the watermark will need to be enlarged for this one
                        
                        // where is the largest difference?
                        $wdiff=$origWidth - $waterMarkDestWidth;
                        $hdiff=$origHeight - $waterMarkDestHeight;
                        if($wdiff > $hdiff){
                            // the width has the largest difference - get percentage
                            $sizer=($wdiff/$waterMarkDestWidth)+0.05;
                        }else{
                            $sizer=($hdiff/$waterMarkDestHeight)+0.05;
                        }
                        $waterMarkDestWidth+=$waterMarkDestWidth * $sizer;
                        $waterMarkDestHeight+=$waterMarkDestHeight * $sizer;
                    }
                }else{
                    $waterMarkDestWidth=round($origWidth * floatval($_POST['wm_size']));
                    $waterMarkDestHeight=round($origHeight * floatval($_POST['wm_size']));
                    if($_POST['wm_size']==1){
                        $waterMarkDestWidth-=2*$edgePadding;
                        $waterMarkDestHeight-=2*$edgePadding;
                    }
                }

                // OK, we have what size we want the watermark to be, time to scale the watermark image
                resize_png_image($watermark,$waterMarkDestWidth,$waterMarkDestHeight,$wmTarget);
                
                // get the size info for this watermark.
                $wmInfo=getimagesize($wmTarget);
                $waterMarkDestWidth=$wmInfo[0];
                $waterMarkDestHeight=$wmInfo[1];

                $differenceX = $origWidth - $waterMarkDestWidth;
                $differenceY = $origHeight - $waterMarkDestHeight;

                // where to place the watermark?
                switch($_POST['h_position']){
                    // find the X coord for placement
                    case 'left':
                        $placementX = $edgePadding;
                        break;
                    case 'center':
                        $placementX =  round($differenceX / 2);
                        break;
                    case 'right':
                        $placementX = $origWidth - $waterMarkDestWidth - $edgePadding;
                        break;
                }

                switch($_POST['v_position']){
                    // find the Y coord for placement
                    case 'top':
                        $placementY = $edgePadding;
                        break;
                    case 'center':
                        $placementY =  round($differenceY / 2);
                        break;
                    case 'bottom':
                        $placementY = $origHeight - $waterMarkDestHeight - $edgePadding;
                        break;
                }
       
                if($size[2]==3)
                    $resultImage = imagecreatefrompng($original);
                else
                    $resultImage = imagecreatefromjpeg($original);
                imagealphablending($resultImage, TRUE);
        
                $finalWaterMarkImage = imagecreatefrompng($wmTarget);
                $finalWaterMarkWidth = imagesx($finalWaterMarkImage);
                $finalWaterMarkHeight = imagesy($finalWaterMarkImage);
        
                imagecopy($resultImage,
                          $finalWaterMarkImage,
                          $placementX,
                          $placementY,
                          0,
                          0,
                          $finalWaterMarkWidth,
                          $finalWaterMarkHeight
                );
                
                if($size[2]==3){
                    imagealphablending($resultImage,FALSE);
                    imagesavealpha($resultImage,TRUE);
                    imagepng($resultImage,$target,$quality);
                }else{
                    imagejpeg($resultImage,$target,$quality); 
                }

                imagedestroy($resultImage);
                imagedestroy($finalWaterMarkImage);

                // display resulting image for download
?>
   <div>
    <h1>Watermarked Image</h1>
    <p>
     <a href="<?php echo $_SERVER['PHP_SELF'] ?>">Back To Form</a> <a href="generated-images.php">View Previously Watermarked Images</a>
     <a href="/php-gd-image-watermark.zip">Source Code Download</a>
    </p>
    <p>
     Below is the resulting watermarked image based on your input on the submission form. To save the image, right-click
     (control click on a Mac) and select the option similar to "Save Image As..." You can then save the image to your harddrive
     when prompted for a save location.
    </p>
    <p>
     Watermarked images are also saved on this system. You can view and/or delete watermarked images
     <a href="generated-images.php">here</a>.
    </p>
    <p align="center">
     <img src="results/<?php echo $target_name ?>?id=<?php echo md5(time()) ?>" alt="" />
    </p>
    <hr>
   </div>
<?php
                unlink($wmTarget);
            }else{
?>
   <div>
    <h1>Watermarked Image</h1>
    <p class="errorMsg">The image uploaded was not a JPEG or PNG image.</p>
   </div>
<?php
            }
        }else{
?>
   <div>
    <h1>Watermarked Image</h1>
    <p class="errorMsg">Unable to upload the image to watermark.</p>
   </div>
<?php
        }
    }else{
        // no image posted, show form for options
?>
   <div>
    <h1>Watermark A JPEG or PNG Image</h1>
    <p>
     <a href="generated-images.php">View Previously Watermarked Images</a>
     <a href="/php-gd-image-watermark.zip">Source Code Download</a>
    </p>
    <p>
      If you are uploading a PNG-24 image that has alpha transparency and are saving it as a PNG,
      the transparency will be saved with the watermarked version of the image. However, if the original
      image is a PNG-8 with transparency, you may get unexpected results.
    </p>
    <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>">
     <table cellpadding="2" cellspacing="0" border="0">
      <tr>
       <th width="50%">
        Image to WaterMark:
        <p>(72dpi, RGB JPEG/PNG)</p>
       </th>
       <td width="50%">
        <input type="file" name="watermarkee" />
       </td>
      </tr>
      <tr>
       <th valign="top">
        Choose Watermark:<br />
        <p>
         Watermark images are located in the &quot;watermarks&quot; subdirectory. All images are PNG-24 with alpha
         transparency. These images were created using Adobe Photoshop&reg; by setting the image's opacity to 30% and
         using the "Save For Web" option to save the file.
        </p>
        <p>
         The watermark images shown are not displayed at their true size, they are actually quite a bit larger so that
         the quality of the watermark will not deteriorate as quickly when watermarking larger images.
        </p>
       </th>
       <td valign="top">
        <table cellpadding="2" cellspacing="0" border="0">
<?php
        $dir=dirname(__FILE__).'/watermarks';
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                $i=0;
                $watermarks=array();
                while (($file = readdir($dh)) !== FALSE) {
                    if(!preg_match('`\.png$`',$file)) continue;
                    $watermarks[]=$file;
                }
                closedir($dh);
                
                // now sort the array according to file name
                asort($watermarks);
                
                foreach($watermarks as $file){
                    // restrain display width
                    $size=getimagesize($dir.'/'.$file);
                    if($size[0] > $disp_width_max && $disp_width_max){
                        $reduction=$disp_width_max / $size[0];
                        $size[0]=$size[0]*$reduction;
                        $size[1]=$size[1]*$reduction;
                        $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);
                        
                        // also restrain the height after a resize
                        if($size[1] > $disp_height_max && $disp_height_max){
                            $reduction=$disp_height_max / $size[1];
                            $size[0]=$size[0]*$reduction;
                            $size[1]=$size[1]*$reduction;
                            $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);
                        }
                    }

                    // also restrain the height
                    if($size[1] > $disp_height_max && $disp_height_max){
                        $reduction=$disp_height_max / $size[1];
                        $size[0]=$size[0]*$reduction;
                        $size[1]=$size[1]*$reduction;
                        $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);
                    }

                    echo '      <tr>',"\n",
                         '       <td>',"\n",
                         '        <input type="radio" value="',$file,'" name="watermark" /> ',
                         '<img src="watermarks/',$file,'" ',$size[3],' alt="" />',"\n",
                         '       </td>',"\n",
                         '      </tr>',"\n";

                    $i++;
                }
                if($i==0){
                    // no images at this time
                    echo '     <tr>',"\n",
                         '      <td>',"\n",
                         '       There are currently no watermark images to use with this the system on this server.',"\n",
                         '      </td>',"\n",
                         '     </tr>',"\n";
                }
            }
        }
?>
        </table>
       </td>
      </tr>
      <tr>
       <th valign="top">
        Watermark Position:
        <p>(Horizontal)</p>
       </th>
       <td>
        <input type="radio" name="h_position" value="left" /> Left<br />
        <input type="radio" name="h_position" value="center" /> Center<br />
        <input type="radio" name="h_position" value="right" /> Right<br />
       </td>
      </tr>
      <tr>
       <th valign="top">
        Watermark Position:
        <p>(Vertical)</p>
       </th>
       <td>
        <input type="radio" name="v_position" value="top" /> Top<br />
        <input type="radio" name="v_position" value="center" /> Center<br />
        <input type="radio" name="v_position" value="bottom" /> Bottom<br />
       </td>
      </tr>
      <tr>
       <th valign="top">
        Watermark Coverage:
        <p>(Size of Originial)</p>
       </th>
       <td>
        <input type="radio" name="wm_size" value="larger" /> Cover Entire Image<br />
        <input type="radio" name="wm_size" value="1" /> 100% (Shows entire watermark)<br />
        <input type="radio" name="wm_size" value=".5" /> 50%<br />
       </td>
      </tr>
      <tr>
       <td align="center" colspan="2">
        <input type="submit" name="process" value="Watermark Image" />
       </td>
      </tr>
     </table>
    </form>
   </div>
<?php
    }
    $page_display=ob_get_clean();

function resize_png_image($img,$newWidth,$newHeight,$target){
    $srcImage=imagecreatefrompng($img);
    if($srcImage==''){
        return FALSE;
    }
    $srcWidth=imagesx($srcImage);
    $srcHeight=imagesy($srcImage);
    $percentage=(double)$newWidth/$srcWidth;
    $destHeight=round($srcHeight*$percentage)+1;
    $destWidth=round($srcWidth*$percentage)+1;
    if($destHeight > $newHeight){
        // if the width produces a height bigger than we want, calculate based on height
        $percentage=(double)$newHeight/$srcHeight;
        $destHeight=round($srcHeight*$percentage)+1;
        $destWidth=round($srcWidth*$percentage)+1;
    }
    $destImage=imagecreatetruecolor($destWidth-1,$destHeight-1);
    if(!imagealphablending($destImage,FALSE)){
        return FALSE;
    }
    if(!imagesavealpha($destImage,TRUE)){
        return FALSE;
    }
    if(!imagecopyresampled($destImage,$srcImage,0,0,0,0,$destWidth,$destHeight,$srcWidth,$srcHeight)){
        return FALSE;
    }
    if(!imagepng($destImage,$target)){
        return FALSE;
    }
    imagedestroy($destImage);
    imagedestroy($srcImage);
    return TRUE;
}

    echo '<?xml version="1.0" encoding="iso-8859-1"?>',"\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Watermarking JPEG and PNG Images with PHP and GD2</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta name="description" content="A PHP implementation to watermark JPEG or PNG images with a PNG-24 image with alpha transparency." />
  <style type="text/css">
   @import url(http://koivi.com/styles.css);
    th{
        text-align: right;
        font-weight: bold;
    }
    th p{
        font-weight: normal;
        font-size: 75%;
        color: #c00;
        background-color: transparent;
    }
  </style>
  <!--[if lt IE 7]><script src="/ie7/ie7-standard.js" type="text/javascript"></script><![endif]-->
 
 </head>


 <body>
  <div id="container">
   <div id="intro">
    <h1>Watermarking PNG and JPEG Images with PHP and GD2</h1>
    <p>
     Have you ever wanted to add a transparent watermark to an image that you post on your website?
     If so, then this may be exactly what you are looking for!
    </p>
    <p>
     Choose the image to watermark, the watermark you want to use (pulled from a directory of images),
     how you want it positioned on the resulting image and its size. Watermarked images are stored on
     the server so you can use them later. Management screen also lets you delete a watermarked image
     from the server to reduce clutter.
    </p>
    <p>
     A <big>BIG</big> &quot;Thank-you&quot; goes out to J Wynia for his excellent article entitled
     &quot;<a href="http://www.phpgeek.com/articles.php?content_id=6">Watermarking Photos with PHP/GD2</a>&quot;
     from June 3, 2003 that covers the basics that were used to get this system together!
    </p>
   </div>

   <div>
    <?php echo $page_display ?>
   </div>



  </div>


 </body>
</html>

Where should I change to get the image croped according to the watermark image size ratio?
Please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:45:09+00:00Added an answer on May 27, 2026 at 7:45 am

    I have not looked into the script you’ve posted, but a script that does what you want without editting is http://www.verot.net/php_class_upload.htm Perhaps it’s usefull for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am integrating this Twitter API in my project. http://mobile.tutsplus.com/tutorials/iphone/twitter-api-iphone/ This is running well
I'm currently integrating a CMS (developed in PHP) authentication with Active Directory. This specific
learning Jquery and integrating with PHP - getting there, but have one last challenge
after integrating Three20 open source and it works good but i want to know
I'm considering integrating some D3D code I have with WPF via the new D3DImage
I'm currently integrating external applications in my app ex SalesForce.com. My question pertains to
I'm looking into integrating elasticsearch into my spring-jpa driven application. For this purpose the
Im integrating local notification into my app. It is working fine. But client want
I'm integrating a content slider into magento theme, but having some issue with the
I'm working on integrating Ben Gottlieb's Twitter-OAuth-iPhone code into my cocos2d 0.99.5 project using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.