In this paper, the author introduces the method of local mosaic of image in PHP implementation. Share to everyone for your reference. The specific analysis is as follows:
Principle:
For each pixel in the selected area of the picture, a number of widths and heights are added to create the moment shape. The moments of each pixel overlap together, creating a mosaic effect.
This example uses the Imagecolorat of the GD library to get the pixel color, using imagefilledrectangle to draw the moment shape.
As shown below:
The code is as follows:
Copy CodeThe code is as follows: <?php
/** Pictures Local mosaic
* @param String $source Original
* @param stirng $dest generated pictures
* @param int $x 1 start axis
* @param int $y 1 start ordinate
* @param int $x 2 End axis
* @param int $y 2 End ordinate
* @param int $deep Depth, the larger the number the more blurred
* @return Boolean
*/
function Imagemosaics ($source, $dest, $x 1, $y 1, $x 2, $y 2, $deep) {
Determine if the original image exists
if (!file_exists ($source)) {
return false;
}
Get the original information
List ($owidth, $oheight, $otype) = getimagesize ($source);
Determine if the area is outside the picture
if ($x 1> $owidth | | $x 1<0 | | $x 2> $owidth | | $x 2<0 | | $y 1> $oheight | | $y 1<0 | | $y 2> $oheight | | $y 2<0) {
return false;
}
Switch ($otype) {
Case 1: $source _img = Imagecreatefromgif ($source); Break
Case 2: $source _img = Imagecreatefromjpeg ($source); Break
Case 3: $source _img = Imagecreatefrompng ($source); Break
Default
return false;
}
Play Mosaic
for ($x = $x 1; $x < $x 2; $x = $x + $deep) {
for ($y = $y 1; $y < $y 2; $y = $y + $deep) {
$color = Imagecolorat ($source _img, $x +round ($deep/2), $y +round ($deep/2));
Imagefilledrectangle ($source _img, $x, $y, $x + $deep, $y + $deep, $color);
}
}
Create a picture
Switch ($otype) {
Case 1:imagegif ($source _img, $dest); Break
Case 2:imagejpeg ($source _img, $dest); Break
Case 3:imagepng ($source _img, $dest); Break
}
Return Is_file ($dest)? True:false;
}
$source = ' source.jpg ';
$dest = ' dest.jpg ';
$flag = Imagemosaics ($source, $dest, 176, 98, 273, 197, 4);
Echo ' ';
Echo ' ';
?>
PHP Play Mosaic