Php implements local mosaic of images

Source: Internet
Author: User
This article mainly introduces how to implement partial mosaic of images in php. The example analyzes php's image processing skills, which is of great practical value. For more information, see

This article mainly introduces how to implement partial mosaic of images in php. The example analyzes php's image processing skills, which is of great practical value. For more information, see

This example describes how to implement partial mosaic of images in php. Share it with you for your reference. The specific analysis is as follows:

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 is overlapped.
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.

As follows:

The Code is as follows:

The Code is as follows:

<? Php
/** Partially mosaic the image
* @ Param String $ source Image
* @ Param Stirng $ dest generated image
* @ Param int $ x 1 start abscissa
* @ Param int $ y1 start column Coordinate
* @ Param int $ x2 X-axis of the end point
* @ Param int $ y2 end point ordinate
* @ Param int $ deep depth: the greater 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 area 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 = imagecreatefrompng ($ 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: 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 '';
?>

Click here to download the complete instance code.

I hope this article will help you with php programming.

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.