How to use PHP to achieve image coding? The use of GD Library can achieve image coding. This article mainly introduces the PHP implementation of the image of the local coding method, I hope to help you.
Principle:
For each pixel in the selected area of the picture, a number of widths and heights are added to create the moment shape. The moments of each pixel overlap together, creating a mosaic effect.
This example uses the Imagecolorat of the GD library to get the pixel color, using imagefilledrectangle to draw the moment shape.
As shown below:
The code is as follows:
<?php/** Pictures Local mosaic * @param String $source original * @param stirng $dest generated picture * @param int $x 1 Beginning Horizontal axis * @par AM int $y 1 start ordinate * @param int $x 2 end point Horizontal axis * @param int $y 2 end point ordinate * @param int $deep depth, number more The larger the more ambiguous * @return Boolean */function Imagemosaics ($source, $dest, $x 1, $y 1, $x 2, $y 2, $deep) {//To determine if the original image exists if (!f Ile_exists ($source)) {return false; }//Get the original information list ($owidth, $oheight, $otype) = getimagesize ($source); Determine 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 2> $o Height | | $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 ' '; ?>
Related recommendations:
How does PHP open the GD library?
What is the GD library? Specific introduction to loading the GD library in PHP
The solution of generating watermark garbled in GD library