PHP code for cropping images and adding watermarks. _ PHP Tutorial

Source: Internet
Author: User
Tags crop image
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...

Related Article

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.