This article is an example of the implementation of PHP image local mosaic method. Share to everyone for your reference. The specific analysis is as follows:
Principle:
Adds a number of widths and heights to each pixel in the selected area of the picture, generating a moment type. And the moment of each pixel overlapping together, forming a mosaic effect.
This example uses the Imagecolorat of the GD library to get the pixel color and use the Imagefilledrectangle to draw the moment type.
The effect diagram looks like this:
The code is as follows:
Copy Code code as follows:
<?php
/** picture Local dozen mosaic
* @param String $source Artwork
* @param stirng $dest generated pictures
* @param int $x 1-Point Horizontal axis
* @param int $y 1 start ordinate
* @param int $x 2-point Horizontal axis
* @param int $y 2 endpoint 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 artwork information
List ($owidth, $oheight, $otype) = getimagesize ($source);
Determine if the region is out of 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);
}
}
Generate pictures
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 ' ';
?>
Full instance code click here to download the site.
I hope this article will help you with your PHP program design.