2009-10-20 91 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