2009-10-20 78 views
1

我想为图像加水印并保存。这里是我使用它的代码。这里是输出图像并将输出存储到文件中。我想保存它而不输出它。在php中的图像水印

// Load the stamp and the photo to apply the watermark to 
$stamp = imagecreatefrompng('stamp.png'); 
$im = imagecreatefromjpeg('proverbs.jpeg'); 

// Set the margins for the stamp and get the height/width of the stamp image 
$marge_right = 10; 
$marge_bottom = 10; 
$sx = imagesx($stamp); 
$sy = imagesy($stamp); 

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
    imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp)); 

// Output and free memory 
header('Content-type: image/png'); 
ob_start(); 
// output jpeg (or any other chosen) format & quality 
imagejpeg($im, NULL, 85); 
// capture output to string 
$contents = ob_get_contents(); 
// end capture 
ob_end_clean(); 
//imagepng($im); 
imagedestroy($im); 
$fh = fopen("proverbs.jpeg", "w"); 
fwrite($fh, $contents); 
fclose($fh); 

回答

2

将其保存到文件?

简单地改变

imagejpeg($im, NULL, 85); 

imagejpeg($im, 'image.jpg', 85); 
+0

万阿英,蒋达清是,其输出的“http://本地主机/ IM age/index.php“放到屏幕上。我不希望输出任何东西 – Andromeda 2009-10-20 11:14:40

+2

已修复它...只是省略输出缓冲区 – Andromeda 2009-10-20 11:16:29

+0

真棒!伟大的工作=) – mauris 2009-10-20 11:56:36

0

这是代码,

<?php 

header('content-type: image/jpeg'); 
$src = $_GET['src']; 
$path = pathinfo($src); 
$watermark = imagecreatefrompng('watermark.png'); 
$watermark_width = imagesx($watermark); 
$watermark_height = imagesy($watermark); 
$image = imagecreatetruecolor($watermark_width, $watermark_height); 
if ($path['extension']=='png') 
$image = imagecreatefrompng($src); 
else if ($path['extension']=='jpg'||$path['extension']=='jpeg') 
$image = imagecreatefromjpeg($src); 
$size = getimagesize($_GET['src']); 
$dest_x = $size[0] - $watermark_width-10; 
$dest_y = $size[1] - $watermark_height-10; 
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50); 
imagejpeg($image); 
imagedestroy($image); 
imagedestroy($watermark); 

?> 

细节在这里,Image watermark in PHP