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

  • Home
  • SEARCH
  • 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 317237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:27:26+00:00 2026-05-12T08:27:26+00:00

Below is a watermark function I made for my php photo upload script. I

  • 0

Below is a watermark function I made for my php photo upload script. I am curious if there is a better way of doing the parts that check for the file type, notice I had to use that part of code 2 times

<?PHP
function watermark($source_file,$source_width,$source_height,$image_type) {
    //first image below will be large then small in 1line if/else
    $watermarksize = ($source_width > 300) ? '../images/fpwatermark.gif' : '../images/fpwatermark.gif';
    //now add the watermark to the image.
    $watermark = imagecreatefromgif($watermarksize);
    switch ($image_type) {
        case 'gif':
            $image = imagecreatefromgif($source_file);
            break;
        case 'jpg':
            $image = imagecreatefromjpeg($source_file);
            break;
        case 'png':
            $image = imagecreatefrompng($source_file);
            break;
        default:
            $image = imagecreatefromjpeg($source_file);
            break;
    }
    //get the dimensions of the watermark
    list($water_width, $water_height) = getimagesize($watermarksize);
    // Water mark process
    $x = $source_width - $water_width - 8; //horizontal position
    $y = $source_height - $water_height - 8; //vertical positon
    // imagesy($image) can be the source images width
    imagecopymerge($image, $watermark, $x, $y, 0, 0, $water_width, $water_height, 65);
    switch ($image_type) {
        case 'gif':
            imagegif($image, $source_file, 90);
            break;
        case 'jpg':
            imagejpeg($image, $source_file, 90);
            break;
        case 'png':
            imagepng($image, $source_file, 90);
            break;
        default:
            imagejpeg($image, $source_file, 90);
            break;
    }
    imagedestroy($image);
    return $source_file;
}
?>
  • 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-12T08:27:26+00:00Added an answer on May 12, 2026 at 8:27 am

    You could use dynamic function calls, as illustrated below. This code is subtly different from yours since it returns if an invalid image type is provided rather than assume that it’s jpeg. If you insist on that behavior it should be easy enough to change, tough.

    It’s not always the case that all these image types are supported by PHP so you might want to use function_exists() to check for that before calling them. Calling a non-existant function is a fatal error in PHP.

    <?PHP
    function watermark($source_file,$source_width,$source_height,$image_type) {
        $validTypes = array("gif" => "gif", "jpg" => "jpeg", "jpeg" => "jpeg", "png" => "png");
        if (!array_key_exists($image_type, $validTypes)) {
            trigger_error("Not a valid image type", E_USER_WARNING);
            return NULL;
        }
    
        $inFunc = "imagecreatefrom" . $validTypes[$image_type];
        $outFunc = "image" . $validTypes[$image_type];
    
        //first image below will be large then small in 1line if/else
        $watermarksize = ($source_width > 300) ? '../images/fpwatermark.gif' : '../images/fpwatermark.gif';
        //now add the watermark to the image.
        $watermark = imagecreatefromgif($watermarksize);
    
        // open the image using the assigned function
        $image = $inFunc($source_file);
    
        //get the dimensions of the watermark
        list($water_width, $water_height) = getimagesize($watermarksize);
        // Water mark process
        $x = $source_width - $water_width - 8; //horizontal position
        $y = $source_height - $water_height - 8; //vertical positon
        // imagesy($image) can be the source images width
        imagecopymerge($image, $watermark, $x, $y, 0, 0, $water_width, $water_height, 65);
    
        // save the image
        $outFunc($image, $source_file, 90);
    
        imagedestroy($image);
        return $source_file;
    }
    

    If you have the exif extension installed you could use exif_imagetype() to automatically detect the type of the image.

    Another option that’s a bit more elegant, but also contains more code is to use polymorfism:

    <?php
    
    interface Codec {
        public function open($file);
        public function save($img);
    }
    
    class JPEGCodec implements Codec {
        public function open($file) { return imagecreatefromjpeg($file); }
        public function save($img, $out_file) { imagejpeg($img, $out_file, 90); }
    }
    
    class PNGCodec implements Codec {
        public function open($file) { return imagecreatefrompng($file); }
        public function save($img, $out_file) { imagepng($img, $out_file, 9); }
    }
    
    class GIFCodec implements Codec {
        public function open($file) { return imagecreatefromgif($file); }
        public function save($img, $out_file) { imagegif($img, $out_file); }
    }
    
    class WatermarkException extends Exception {}
    
    class Watermark
    {
        private $_codecs = array();
    
        public function __construct()
        {
            $this->_codecs["jpg"] = $this->_codecs["jpeg"] = new JPEGCodec();
            $this->_codecs["png"] = new PNGCodec();
            $this->_codecs["gif"] = new GIFCodec();
        }
    
        function watermark($source_file,$source_width,$source_height,$image_type) {
            if (!array_key_exists($image_type, $this->_codecs)) {
                throw new WatermarkException("Not a valid image type");
            }
    
            $codec = $this->_codecs[$image_type];
    
            //first image below will be large then small in 1line if/else
            $watermarksize = ($source_width > 300) ? '../images/fpwatermark.gif' : '../images/fpwatermark.gif';
            //now add the watermark to the image.
            $watermark = imagecreatefromgif($watermarksize);
            
            // load image
            $image = $codec->open($source_file);
            
            //get the dimensions of the watermark
            list($water_width, $water_height) = getimagesize($watermarksize);
            // Water mark process
            $x = $source_width - $water_width - 8; //horizontal position
            $y = $source_height - $water_height - 8; //vertical positon
            // imagesy($image) can be the source images width
            imagecopymerge($image, $watermark, $x, $y, 0, 0, $water_width, $water_height, 65);
            
            // save image
            $codec->save($image, $source_file);
    
            imagedestroy($image);
            return $source_file;
        }
    }
    

    I realize that you’ll probably prefer the first one. 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 217k
  • Answers 217k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have you looked at using a BindingContext? And here's a… May 12, 2026 at 11:20 pm
  • Editorial Team
    Editorial Team added an answer You have made the table cell in the outer table… May 12, 2026 at 11:20 pm
  • Editorial Team
    Editorial Team added an answer There isn't the concept of "session" on the Client/JS side… May 12, 2026 at 11:20 pm

Related Questions

I have content that is first htmlentities and then stripslashes followed by nl2br .
SOLUTION: Thanks to Patrick below, I have refactored the C# CodeProject version into a
I just fixed a memory leak caused by someone forgetting to call the superclass's
I have a textbox that I want to be watermarked. In my window.resources section

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.