Reputation: 54
I read many similar questions and answers, including on Stackoverflow but I was not able to find the solution of my current problem.
I am working on a php script adding watermark on the fly. I got the initial script from php.net (https://www.php.net/manual/en/function.imagecopymerge.php).
Script works like a charm when it is used as standalone, e.g. when only displaying the image (browser only show image). But when I call the php function from my html with embedded php scripting, it does not display the image but returns: "WARNING: Cannot modify header information - headers already sent by (output ... line 61)" 61 is the line number where: header ('Content-type: images/png').
Any way to have it work with watermark function call in php embedded in html?
Thank you, Chris.
Working but only displays image
<?php
/******************************************************************/
function watermark($sourcefile, $watermarkfile) {
#
# $sourcefile = Filename of the picture to be watermarked.
# $watermarkfile = Filename of the 24-bit PNG watermark file.
#
//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);
$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');
imagejpeg ($sourcefile_id);
}
imagedestroy($sourcefile_id);
imagedestroy($watermarkfile_id);
}
$watermarkfile_nathalie="./images/cartes/watermark.png";
$image_to_watermark="./images/cartes/YV1_1ereDeCouverture2.png";
$image_watermarked=watermark($image_to_watermark, $watermarkfile_nathalie);
?>
Not working when integrated with html
<?php
/******************************************************************/
function watermark($sourcefile, $watermarkfile) {
#
# $sourcefile = Filename of the picture to be watermarked.
# $watermarkfile = Filename of the 24-bit PNG watermark file.
#
//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);
$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');
imagejpeg ($sourcefile_id);
}
imagedestroy($sourcefile_id);
imagedestroy($watermarkfile_id);
}
?>
<html>
<head>
</head>
<body>
<p>
Hello world
<br />
<?php
$watermarkfile_nathalie="./images/cartes/watermark.png";
$image_to_watermark="./images/cartes/YV1_1ereDeCouverture2.png";
$image_watermarked=watermark($image_to_watermark, $watermarkfile_nathalie);
echo "<img src = \"";
echo $image_watermarked;
echo "\" />";
?>
</p>
</body>
</html>
Upvotes: 0
Views: 357
Reputation: 54
Thanks to altrisi, I manage to have it work.
Here is the solution.
I created a file "watermark.php" which I call from the page I want to watermark images.
watermark.php
<?php
/******************************************************************/
function watermark($sourcefile, $watermarkfile) {
#
# $sourcefile = Filename of the picture to be watermarked.
# $watermarkfile = Filename of the 24-bit PNG watermark file.
#
//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);
$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');
imagejpeg ($sourcefile_id);
}
imagedestroy($sourcefile_id);
imagedestroy($watermarkfile_id);
}
// Constants
$path_to_images="./image/";
$default_image="YV1_1ereDeCouverture2.png";
$default_watermark="watermark.png";
// Read passing arguments: image to watermark and watermark image
// 1. image to watermark
$img_to_watermark=$_GET["img"];
if($img_to_watermark) {
/* Validate that img exists */
/* Set path */
$img_to_watermark=$path_to_images . $img_to_watermark;
}
else {
/* Display default image */
$img_to_watermark=$path_to_images . $default_image;
}
// 2. watermark image
$watermarkfile=$_GET["watermark"];
if($watermarkfile) {
/* Validate that watermark img exists */
/* Set path */
$watermarkfile=$path_to_images . $watermarkfile;
}
else {
/* Use default watermak image */
$watermarkfile=$path_to_images . $default_watermark;
}
$image_watermarked=watermark($img_to_watermark, $watermarkfile);
return $image_watermarked;
?>
Then the php file which call watermark.php:
<html>
<head>
</head>
<body>
<p>
Hello world
<br />
<?php
$watermarkfile_nathalie="watermark.png";
$image_to_watermark="postureDeLaGraineALaFleur_recto.png";
echo "<img src = \"./watermark.php?img=" . $image_to_watermark . "&watermark=" . $watermarkfile_nathalie . "\" />\n";
?>
<br />
What's up now?
</p>
</body>
</html>
Hope this will help! Do not hesitate to post your enhancements :-).
Chris.
Upvotes: 0
Reputation: 131
You have to set the src
of your img
to the PHP that is returning the image, like
<img src="/path/to/image.php" />
Upvotes: 1