Partial mosaic of php images,

Source: Internet
Author: User

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







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.