Partial mosaic of php images,
Partial mosaic of php Images
Principle:
Increase the width and height of each pixel in the selected area of the image to generate a rectangle. The mosaic effect is formed when the rectangle of each pixel overlaps.
In this example, the imagecolorat In the GD library is used to obtain the pixel color and imagefilledrectangle is used to draw the rectangle type.
:
The Code is as follows:
<? Php/** partially mosaic images * @ param String $ source image * @ param Stirng $ image generated by dest * @ param int $ x1 start point X coordinate * @ param int $ y1 start point y coordinate * @ param int $ x2 X-axis of the end * @ param int $ y2 X-axis of the end * @ param int $ deep depth, the bigger the number, the blurrier it. * @ return boolean */function imageMosaics ($ source, $ dest, $ x1, $ y1, $ x2, $ y2, $ deep) {// determine whether the source image exists if (! File_exists ($ source) {return false;} // obtain the source Image Information list ($ owidth, $ oheight, $ otype) = getimagesize ($ source ); // determine whether the region exceeds the image if ($ x1> $ owidth | $ x1 <0 | $ x2> $ owidth | $ x2 <0 | $ y1> $ oheight | $ y1 <0 | $ y2> $ oheight | $ y2 <0) {return false;} switch ($ otype) {case 1: $ source_img = imagecreatefromgif ($ source); break; case 2: $ source_img = imagecreatefromjpeg ($ source); break; case 3: $ source_img = imagecreat Efrompng ($ source); break; default: return false;} // mosaic for ($ x = $ x1; $ x <$ x2; $ x = $ x + $ deep) {for ($ y = $ y1; $ y <$ y2; $ 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 an image switch ($ otype) {case 1: imagegif ($ source_img, $ dest); break; case 2: imagejpeg ($ source_img, $ dest); break; case 3: I Magepng ($ 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