PHP Play Mosaic

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.