PHP pictures Local mosaic

Source: Internet
Author: User

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

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.