使用 GD 库的图像随机化


Image Randomization using GD library

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

为了实现这一点,我们可以在图像背景中随机添加随机像素。

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

使用 Imagesetpixel。

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

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

编辑:由于你想要一个透明的像素,你将需要图像alphablending和类似的东西:

$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');