使用GD库的图像随机化

时间:2012-05-29 13:02:04

标签: php image-processing gd

我的任务是遇到一个问题,我需要从1000个图像中拾取一个图像,并根据get查询中传递的参数将其提供给用户。 这很简单。 另外,我被要求以这样的方式提供图像:即使每次都是同一个文件服务器,图像文件的sha1哈希也应该不同。

为实现这一目标,我们可以在随机位置添加图像背景中的随机像素。

有人可以告诉我如何使用GD库实现这一目标

1 个答案:

答案 0 :(得分:0)

使用Imagesetpixel

$img = imagecreatefrompng('your_image.png');
$red = imagecolorallocate($img, 255, 0, 0); 
imagesetpixel($img, $x, $y, $red);
........
........

另一方面,为什么要在每次请求时更改图像的sha1哈希?

编辑:由于您需要透明像素,因此您需要imagealphablending,如下所示:

$img = imagecreatefrompng('your_image.png');
imagealphablending($img, false);
$transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagesetpixel($img, $x, $y, $transparent);
imagesavealpha($img, true);         
imagepng($img, 'my_saved_file.png');
相关问题