Php image processing function example Summary, php image function instance code

Source: Internet
Author: User
Php image processing function example Summary, php image function instance code

  1. Imagecolorsforindex ()

  2. Imagecolorat ()
  3. Image $ img = imagecreatefromgif ("./images/map.gif ");

  4. /**

  5. * Image sharpening
  6. * Edit by bbs.it-home.org
  7. */
  8. $ Red = imagecolorallocate ($ img, 255, 0, 0 );

  9. Imageline ($ img, 0, 0,100,100, $ red );

  10. Imageellipse ($ img, 200,100,100,100, $ red );
  11. Imagegif ($ img, "./images/map2.gif ");
  12. Imagedestroy ($ img );

Normal image scaling

  1. Function thumn ($ background, $ width, $ height, $ newfile ){
  2. List ($ s_w, $ s_h) = getimagesize ($ background); // Obtain the height and width of the original image.
  3. If ($ width & ($ s_w <$ s_h )){
  4. $ Width = ($ height/$ s_h) * $ s_w;
  5. } Else {
  6. $ Height = ($ width/$ s_w) * $ s_h;
  7. }
  8. $ New = imagecreatetruecolor ($ width, $ height );
  9. $ Img = imagecreatefromjpeg ($ background );
  10. Imagecopyresampled ($ new, $ img, 0, 0, 0, 0, $ width, $ height, $ s_w, $ s_h );
  11. Imagejpeg ($ new, $ newfile );
  12. Imagedestroy ($ new );
  13. Imagedestroy ($ img );
  14. }
  15. Thumn ("images/hee.jpg", 200,200, "./images/hee3.jpg ");

Gif transparent color processing

  1. /**

  2. * Image cropping
  3. * Edit by bbs.it-home.org
  4. */

  5. Function cut ($ background, $ cut_x, $ cut_y, $ cut_width, $ cut_height, $ location ){

  6. $ Back = imagecreatefromjpeg ($ background );
  7. $ New = imagecreatetruecolor ($ cut_width, $ cut_height );
  8. Imagecopyresampled ($ new, $ back, 0, 0, $ cut_x, $ cut_y, $ cut_width, $ cut_height, $ cut_width, $ cut_height );
  9. Imagejpeg ($ new, $ location );
  10. Imagedestroy ($ new );
  11. Imagedestroy ($ back );
  12. }
  13. Cut ("./images/hee.jpg", 440,140,117,112, "./images/hee5.jpg ");
  14. ?>

Add text watermarks to images

  1. /**

  2. *
  3. * Adding a text watermark to an image
  4. */

  5. Function mark_text ($ background, $ text, $ x, $ y ){

  6. $ Back = imagecreatefromjpeg ($ background );
  7. $ Color = imagecolorallocate ($ back, 0,255, 0 );
  8. Imagettftext ($ back, 20, 0, $ x, $ y, $ color, "simkai. ttf", $ text );
  9. Imagejpeg ($ back, "./images/hee7.jpg ");
  10. Imagedestroy ($ back );
  11. }
  12. Mark_text ("./images/hee.jpg", "PHP", 150,250 );
  13. // Image watermark
  14. Function mark_pic ($ background, $ waterpic, $ x, $ y ){
  15. $ Back = imagecreatefromjpeg ($ background );
  16. $ Water = imagecreatefromgif ($ waterpic );
  17. $ W_w = imagesx ($ water );
  18. $ W_h = imagesy ($ water );
  19. Imagecopy ($ back, $ water, $ x, $ y, 0, 0, $ w_w, $ w_h );
  20. Imagejpeg ($ back, "./images/hee8.jpg ");
  21. Imagedestroy ($ back );
  22. Imagedestroy ($ water );
  23. }
  24. Mark_pic ("./images/hee.jpg", "./images/gaolf.gif", 50,200 );

Image Rotation

  1. /**

  2. * Image rotation
  3. * Edit by bbs.it-home.org
  4. */
  5. $ Back = imagecreatefromjpeg ("./images/hee.jpg ");

  6. $ New = imagerotate ($ back, 45, 0 );

  7. Imagejpeg ($ new, "./images/hee9.jpg ");
  8. ?>

Horizontal flip vertical flip

  1. /**

  2. * Horizontal flip vertical flip
  3. * Edit by bbs.it-home.org
  4. */
  5. Function turn_y ($ background, $ newfile ){

  6. $ Back = imagecreatefromjpeg ($ background );

  7. $ Width = imagesx ($ back );
  8. $ Height = imagesy ($ back );
  9. $ New = imagecreatetruecolor ($ width, $ height );
  10. For ($ x = 0; $ x <$ width; $ x ++ ){
  11. Imagecopy ($ new, $ back, $ width-$ X-1, 0, $ x, 0, 1, $ height );
  12. }
  13. Imagejpeg ($ new, $ newfile );
  14. Imagedestroy ($ back );
  15. Imagedestroy ($ new );
  16. }
  17. Function turn_x ($ background, $ newfile ){
  18. $ Back = imagecreatefromjpeg ($ background );
  19. $ Width = imagesx ($ back );
  20. $ Height = imagesy ($ back );
  21. $ New = imagecreatetruecolor ($ width, $ height );
  22. For ($ y = 0; $ y <$ height; $ y ++ ){
  23. Imagecopy ($ new, $ back, 0, $ height-$ Y-1, 0, $ y, $ width, 1 );
  24. }
  25. Imagejpeg ($ new, $ newfile );
  26. Imagedestroy ($ back );
  27. Imagedestroy ($ new );
  28. }
  29. Turn_y ("./images/hee.jpg", "./images/hee11.jpg ");
  30. Turn_x ("./images/hee.jpg", "./images/hee12.jpg ");
  31. ?>

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.