如何在强制下载前在图像上添加水印

时间:2012-12-13 11:48:13

标签: php codeigniter

我想在

的帮助下添加强制下载功能
$this->load->helper('download');
$photo_path = "uploads/default/photos/".$photo;
$name = $photo_name.'.jpg';
$data = file_get_contents($photo_path); // Read the file's contents
$name = $photo_name.'.jpg';
force_download($name, $data); 

现在我想在下载图像之前在图像上添加水印图像。这可以通过图像处理库实现,也可以在上传文件时尝试添加水印。

1 个答案:

答案 0 :(得分:2)

以下是在将水印发送到force_download之前添加水印的方法:

// Get the watermark from a file
$watermark = imagecreatefrompng('watermark.png');
$wmsize = getimagesize('watermark.png');
// Get your source image
$image = imagecreatefromjpeg($photo_path);
$size = getimagesize($photo_path);
// Set the watermark to be centered within the size of the destination image
$dest_x = ($size[0] - $wmsize[0]) / 2;
$dest_y = ($size[1] - $wmsize[1]) / 2;
// Copy the watermark over the original image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $wmsize[0], $wmsize[1]);
// Use output buffering to capture the output to send to force_download
ob_start(); //Stdout --> buffer
imagejpeg($image); 
$img2 = ob_get_contents(); //store stdout in $img2
ob_end_clean(); //clear buffer
imagedestroy($image);
force_download($name, $img2);