PHP implements Image cropping and watermark effect code ,. PHP real-time image cropping: watermark effect code; 3. PHP cut divh4cut before h4imgsrc1.png divpheader (content-type, te PHP to achieve Image cropping and adding watermark effect code,
3. PHP cropping images
Before cropping
<? Php headers ("content-type", "text/html; charset = utf-8 "); /** Image cropping * @ param string $ filename image url * @ param int $ width image cropping width * @ param int $ height image cropping height * @ param int $ x crop Image start position on the left * @ param int $ y crop the start position of the top edge of the image */function cut ($ filename, $ x, $ y, $ width, $ height) {/* obtain image resources, images to be cropped */$ image = imagecreatefrompng ($ filename ); /* Create a canvas with a new size and save the cropped image */$ image_p = imagecreatetruecolor ($ width, $ height);/* use imagecopyresa Mpled scaling */imagecopyresampled ($ image_p, $ image, 0, 0, $ x, $ y, $ width, $ height, $ width, $ height ); /* save the cropped image and name it */imagepng(+image_p,'test1.png ');/* release the resource */imagedestroy ($ image_p); imagedestroy ($ image );} /* call the function */cut('1.png ', 20, 20, 80, 80);?>After cropping
Effect
4. add a watermark to the image using PHP
No watermark
<? Php header ("content-type", "text/html; charset = utf-8");/** add a watermark to the background image in png format, watermark format: gif * @ param string $ filename image url * @ param string $ water watermark image */function watermark ($ filename, $ water) {/* obtain the source image size */list ($ B _w, $ B _h) = getimagesize ($ filename);/* obtain the watermark image size */list ($ w_w, $ w_h) = getimagesize ($ water);/* random start position of the watermark image in the background image */$ posX = rand (0, ($ B _w-$ w_w )); $ posY = rand (0, ($ B _h-$ w_h);/* get image resources, images to be cropped */$ ba Ck = imagecreatefrompng ($ filename); $ water = imagecreatefromgif ($ water);/* use the Inagecopy function to copy the watermark image to the specified position */imagecopy ($ back, $ water, $ posX, $ posY, 0, 0, $ w_w, $ w_h);/* save the watermark image and name */imagepng(background back,'test2.png '); /* release resources */imagedestroy ($ back); imagedestroy ($ water);}/* call the function */watermark('1.png', 'test.gif ');?>Add watermark
Effect
Crop, 3. PHP crop the image before p h4 crop/h4 img src = "1.png" style =" border: 1px solid red; "/p php header (" content-type "," te...