PHP watermarking

后端 未结 3 1585
抹茶落季
抹茶落季 2020-12-09 07:14

i am using this code to create watermark.

    $image = \'1.jpg\';
    $overlay = \'stamp.png\';
    $opacity = \"20\";
    if (!file_exists($image)) {
               


        
3条回答
  •  渐次进展
    2020-12-09 07:50

    replacing imagecopymerge with imagecopy solved the issue. here is the new code

    function watermark($image){
        $overlay = '../../../photos/photosets/stamp.png';
        $opacity = "20";
        if (!file_exists($image)) {
            die("Image does not exist.");
        }
        // Set offset from bottom-right corner
        $w_offset = 0;
        $h_offset = 100;
        $extension = strtolower(substr($image, strrpos($image, ".") + 1));
        // Load image from file
        switch ($extension)
        {
            case 'jpg':
            $background = imagecreatefromjpeg($image);
            break;
            case 'jpeg':
            $background = imagecreatefromjpeg($image);
            break;
            case 'png':
            $background = imagecreatefrompng($image);
            break;
            case 'gif':
            $background = imagecreatefromgif($image);
            break;
            default:
            die("Image is of unsupported type.");
        }
        // Find base image size
        $swidth = imagesx($background);
        $sheight = imagesy($background);
        // Turn on alpha blending
        imagealphablending($background, true);
        // Create overlay image
        //$overlay = imagecreatefrompng($overlay);
        // Get the size of overlay
        $owidth = imagesx($overlay);
        $oheight = imagesy($overlay);
    
        $photo = imagecreatefromjpeg($image);
        $watermark = imagecreatefrompng($overlay);
                 // This is the key. Without ImageAlphaBlending on, the PNG won't render correctly.
        imagealphablending($photo, true);
                // Copy the watermark onto the master, $offset px from the bottom right corner.
        $offset = 10;
        imagecopy($photo, $watermark, imagesx($photo) - imagesx($watermark) - $offset, imagesy($photo) - imagesy($watermark) - $offset, 0, 0, imagesx($watermark), imagesy($watermark));
                // Output to the browser
        header("Content-Type: image/jpeg");
        imagejpeg($photo,$image);
        // Overlay watermark
        // Destroy the images
        imagedestroy($background);
        imagedestroy($overlay);
    }
    

提交回复
热议问题