Jump to content

image watermarking problem


maxso

Recommended Posts

<?php
function watermark($sourcefile, $watermarkfile, $saveFile)
{
    #
$sourcefile = '/background.jpg'
$watermarkfile = '/8.png'
$saveFile = '/new.png'
    #
   
    //Get the resource ids of the pictures
    $watermarkfile_id = imagecreatefrompng($watermarkfile);
   
    imageAlphaBlending($watermarkfile_id, false);
    imageSaveAlpha($watermarkfile_id, true);
    $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));
   
    switch($fileType)
    {
        case('gif'):
            $sourcefile_id = imagecreatefromgif($sourcefile);
        break;
       
        case('png'):
            $sourcefile_id = imagecreatefrompng($sourcefile);
        break;
       
        default:
            $sourcefile_id = imagecreatefromjpeg($sourcefile);
    }
   
    //Get the sizes of both pix   
    $sourcefile_width=imageSX($sourcefile_id);
    $sourcefile_height=imageSY($sourcefile_id);
    $watermarkfile_width=imageSX($watermarkfile_id);
    $watermarkfile_height=imageSY($watermarkfile_id);
   
    if(isset($_REQUEST['imgPhoto_x']))
    {       
        $dest_x = $_REQUEST['imgPhoto_x'];
        $dest_y = $_REQUEST['imgPhoto_y'];
    }
    else
    {
        $dest_x = ( $sourcefile_width / 2 ) - ( $watermarkfile_width / 2 );
        $dest_y = ( $sourcefile_height / 2 ) - ( $watermarkfile_height / 2 );
    }   
   
    // if a gif, we have to upsample it to a truecolor image
    if($fileType == 'gif')
    {
        // create an empty truecolor container
        $tempimage = imagecreatetruecolor($sourcefile_width,$sourcefile_height);
       
        // copy the 8-bit gif into the truecolor image
        imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0,$sourcefile_width, $sourcefile_height);
       
        // copy the source_id int
        $sourcefile_id = $tempimage;
    }
    imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0,$watermarkfile_width, $watermarkfile_height);
    //Create a jpeg out of the modified picture
    switch($fileType)
    {
        // remember we don't need gif any more, so we use only png or jpeg.
        // See the upsaple code immediately above to see how we handle gifs
        case('png'):
            //header("Content-type: image/png");
            imagepng ($sourcefile_id);
        break;
       
        default:
            //header("Content-type: image/jpg");
            if(imagejpeg ($sourcefile_id,$saveFile))
            {
            }
        //echo "<img src='$saveFile'>";
    }       
     
    imagedestroy($sourcefile_id);
    imagedestroy($watermarkfile_id);
    return true;
}
?>

Thats my code so far but all i get is this.

"; } imagedestroy($sourcefile_id); imagedestroy($watermarkfile_id); return true; } ?>
Link to comment
Share on other sites

Your code works perfectly. Puts a watermark in the middle of the background picture. Just make sure you end all statements with a colon.

 

This is your problem.

 

$sourcefile = 'background.jpg';
$watermarkfile = 'watermark.png';
$saveFile = 'new.png';

 

Make sure to change the names.

 

Just one thing, why do you have variables being passed to the function if you are just gonna overwrite them later?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.