php怎么加水印图片大小
时间 : 2023-03-27 10:49:02声明: : 文章内容来自网络,不保证准确性,请自行甄别信息有效性

在PHP中加水印可以使用GD库。GD库是PHP的一个图形库,可以用于创建和操作图片,包括加水印等操作。

下面我们来介绍一下如何使用GD库实现在图片上添加水印并控制其大小。

首先需要确保PHP安装了GD库扩展。可以通过命令`php -m | grep -i gd`来查看。

接下来,我们来编写示例代码:

```php

<?php

// 加载原图片

$srcImage = imagecreatefromjpeg('original.jpg');

// 加载水印图片

$watermarkImage = imagecreatefrompng('watermark.png');

// 获取原图和水印图片的宽高

$srcWidth = imagesx($srcImage);

$srcHeight = imagesy($srcImage);

$watermarkWidth = imagesx($watermarkImage);

$watermarkHeight = imagesy($watermarkImage);

// 计算水印图片缩放比例,保证整个水印图片可以在原图上完全显示

$scale = min($srcWidth / $watermarkWidth, $srcHeight / $watermarkHeight);

// 创建水印图片的缩略图,大小为原图大小的 $scale 倍

$thumbWidth = $watermarkWidth * $scale;

$thumbHeight = $watermarkHeight * $scale;

$thumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);

imagecopyresampled($thumbImage, $watermarkImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $watermarkWidth, $watermarkHeight);

// 将缩略图拷贝到原图的中心位置

$destX = ($srcWidth - $thumbWidth) / 2;

$destY = ($srcHeight - $thumbHeight) / 2;

imagecopy($srcImage, $thumbImage, $destX, $destY, 0, 0, $thumbWidth, $thumbHeight);

// 输出加水印后的图片

header('Content-Type: image/jpeg');

imagejpeg($srcImage);

// 销毁图片资源

imagedestroy($srcImage);

imagedestroy($watermarkImage);

imagedestroy($thumbImage);

?>

在上面的代码中,我们先加载原图片和水印图片,然后计算水印图片缩放比例,并创建水印图片的缩略图。最后将缩略图拷贝到原图的中心位置,形成水印效果。

更改水印大小可以通过修改以下代码实现:

```php

// 计算水印图片缩放比例

$scale = 0.5; // 缩小为原图的一半

// 创建水印图片的缩略图,大小为原图大小的 $scale 倍

$thumbWidth = $watermarkWidth * $scale;

$thumbHeight = $watermarkHeight * $scale;

$thumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);

在上面的代码中,我们将缩放比例设置为0.5,即缩小为原图的一半大小。如果需要放大水印,可以将缩放比例设置为大于1的值。

在实际应用中,可以将上面的代码封装成一个函数,方便以后调用,同时也可以根据实际需求进行修改。

在PHP中加水印通常需要以下步骤:

1.读取原始图片

2.创建水印图片

3.将水印图片合并到原始图片上

4.保存新图片

在加水印的过程中,我们还需要考虑水印图片的大小问题。以下是一些可以帮助你调整水印图片大小的方法:

1.根据原始图片大小动态调整水印图片大小

2.指定固定的水印图片大小

下面是具体步骤的示例代码:

动态调整

<?php

// Load the stamp and the photo to apply the watermark to

$watermark = imagecreatefrompng('watermark.png');

$photo = imagecreatefromjpeg('photo.jpg');

// Get the width and height of the watermark image

$watermark_width = imagesx($watermark);

$watermark_height = imagesy($watermark);

// Get the width and height of the photo to add the watermark to

$photo_width = imagesx($photo);

$photo_height = imagesy($photo);

// Calculate the position of the watermark

$watermark_pos_x = $photo_width - $watermark_width - 10;

$watermark_pos_y = $photo_height - $watermark_height - 10;

// Resize the watermark image based on the size of the photo

$watermark_resized = imagecreatetruecolor($photo_width * 0.2, $photo_height * 0.2);

imagecopyresampled($watermark_resized, $watermark, 0, 0, 0, 0, $photo_width * 0.2, $photo_height * 0.2, $watermark_width, $watermark_height);

// Merge the photo and the watermark

imagecopy($photo, $watermark_resized, $watermark_pos_x, $watermark_pos_y, 0, 0, imagesx($watermark_resized), imagesy($watermark_resized));

// Save the merged image

imagejpeg($photo, 'merged.jpg');

// Destroy the images

imagedestroy($watermark);

imagedestroy($photo);

imagedestroy($watermark_resized);

?>

固定大小

<?php

// Load the stamp and the photo to apply the watermark to

$watermark = imagecreatefrompng('watermark.png');

$photo = imagecreatefromjpeg('photo.jpg');

// Get the width and height of the watermark image

$watermark_width = imagesx($watermark);

$watermark_height = imagesy($watermark);

// Resize the watermark image to a fixed width and height

$new_watermark_width = 50;

$new_watermark_height = 50;

$watermark_resized = imagecreatetruecolor($new_watermark_width, $new_watermark_height);

imagecopyresampled($watermark_resized, $watermark, 0, 0, 0, 0, $new_watermark_width, $new_watermark_height, $watermark_width, $watermark_height);

// Get the width and height of the photo to add the watermark to

$photo_width = imagesx($photo);

$photo_height = imagesy($photo);

// Calculate the position of the watermark

$watermark_pos_x = $photo_width - $new_watermark_width - 10;

$watermark_pos_y = $photo_height - $new_watermark_height - 10;

// Merge the photo and the watermark

imagecopy($photo, $watermark_resized, $watermark_pos_x, $watermark_pos_y, 0, 0, imagesx($watermark_resized), imagesy($watermark_resized));

// Save the merged image

imagejpeg($photo, 'merged.jpg');

// Destroy the images

imagedestroy($watermark);

imagedestroy($photo);

imagedestroy($watermark_resized);

?>

上述代码中,动态调整水印图片大小的方法是根据原始图片的大小来计算水印图片的大小,比例为原始图片的20%。而固定大小的方法则是将水印图片的大小设为50x50像素。你可以根据自己的需求来调整这些代码,让它们适应你的项目。