PHP pictures Local mosaic
Principle:
For each pixel in the selected area of the picture, add a number of widths and heights to create a moment shape. And the moments of each pixel overlap. It creates a mosaic effect.
This example uses the Imagecolorat of the GD library to get the pixel color, using imagefilledrectangle to draw the moment shape.
:
The code is as follows:
<?php/** Image Local Mosaic * @param String $source original * @param stirng $dest generated picture * @param int $x 1 Beginning Horizontal axis * @param in T $y 1 start ordinate * @param int $x 2 end point Horizontal axis * @param int $y 2 end point ordinate * @param int $deep Depth, the larger the number the more fuzzy * @re Turn boolean*/function imagemosaics ($source, $dest, $x 1, $y 1, $x 2, $y 2, $deep) {//infer if the original artwork exists if (!file_exists ($source) ) {return false; }//Get the original information list ($owidth, $oheight, $otype) = getimagesize ($source); Infer if the area exceeds the picture if ($x 1> $owidth | | $x 1<0 | | $x 2> $owidth | | $x 2<0 | | $y 1> $oheight | | $y 1<0 $y ight | | $y 2<0) {return false; } switch ($otype) {Case 1: $source _img = Imagecreatefromgif ($source); 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); }}//Generate picture switch ($otype) {case 1:imagegif ($source _img, $dest); 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 ' ';?>
Source code: Click to view
PHP pictures Local mosaic