用PHP像素化图像
前几天,我需要隐藏图像,但保持原始图像不变,因此我不想使用明暗滤镜来做复杂的事情,我决定最好的方法是对图像进行像素化。像素化图像所需要做的只是缩小图像,然后将其放大到原始大小,从而降低图像质量。
以下代码将对jpeg图像执行此过程。pixelate量变量用于通过除法减小第一个图像的宽度和高度,然后将它们再次乘回到输出图像的原始值。切记将更ImageCreateFromJPEG()改为所需的格式。
$imgfile = 'mushrooms.jpg'; $image = ImageCreateFromJPEG($imgfile); $imagex = imagesx($image); $imagey = imagesy($image); //像素化量 $pixelate_amount = 10; # Create version of the original image: $tmpImage = ImageCreateTrueColor($imagex, $imagey); imagecopyresized($tmpImage, $image, 0, 0, 0, 0, round($imagex / $pixelate_amount), round($imagey / $pixelate_amount), $imagex, $imagey); # Create 100% version ... blow it back up to it's initial size: $pixelated = ImageCreateTrueColor($imagex, $imagey); imagecopyresized($pixelated, $tmpImage, 0, 0, 0, 0, $imagex, $imagey, round($imagex / $pixelate_amount), round($imagey / $pixelate_amount)); header("Content-Type: image/JPEG"); imageJPEG($pixelated, "", 75);
这将采用以下图像:
并将其更改为: