php添加水印-标题问题
我阅读了许多类似的问题和答案,包括关于Stackoverflow的问题,但我无法找到当前问题的解决方案 我正在编写一个php脚本,动态添加水印。我从php.net()获得了初始脚本 当脚本用作独立脚本时,例如仅显示图像(浏览器仅显示图像)时,它的工作方式就像一个符咒。但是,当我使用嵌入php脚本从html调用php函数时,它不会显示图像,而是返回:“警告:无法修改标题信息-标题已由(输出…第61行)”61是行号,其中:header('Content-type:images/png') 有没有办法让它与嵌入html的php中的水印函数调用一起工作 谢谢,, 克里斯 正在工作但仅显示图像php添加水印-标题问题,php,html,watermark,Php,Html,Watermark,我阅读了许多类似的问题和答案,包括关于Stackoverflow的问题,但我无法找到当前问题的解决方案 我正在编写一个php脚本,动态添加水印。我从php.net()获得了初始脚本 当脚本用作独立脚本时,例如仅显示图像(浏览器仅显示图像)时,它的工作方式就像一个符咒。但是,当我使用嵌入php脚本从html调用php函数时,它不会显示图像,而是返回:“警告:无法修改标题信息-标题已由(输出…第61行)”61是行号,其中:header('Content-type:images/png') 有没有办法
<?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);
?>
你好,世界
您必须将img的src
设置为返回图像的PHP,如
<img src="/path/to/image.php" />
多亏了altrisi,我才让它正常工作
这是解决办法
我创建了一个文件“watermark.php”,从我想要给图像加水印的页面调用它
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;
?>
<?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;
?>
<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>