PHP implementation of the image of the local mosaic method _php skills

Source: Internet
Author: User

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.

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.