Php adds background instance code for images

Source: Internet
Author: User
Php adds background instance code for images

  1. Function overlayjpg ($ imgsrc, $ imgdst, $ width, $ height = "")

  2. {
  3. // $ Imgsrc jpg format image path $ imgdst jpg format Image save file name $ imgwidth to change the width $ imgheight to change the height
  4. // Obtain the image width and height
  5. $ Arr = getimagesize ($ imgsrc );
  6. // Calculate the x axis of the image.
  7. $ Img_X = ($ width-$ arr [0])/2;
  8. If ($ height = ""){
  9. $ Heights = $ arr [1];
  10. $ Img_Y = 0;
  11. }
  12. Else {
  13. If ($ height <= $ arr [1]) {
  14. $ Heights = $ arr [1];
  15. $ Img_Y = 0;
  16. }
  17. Else {
  18. $ Heights = $ height;
  19. $ Img_Y = ($ height-$ arr [1])/2;
  20. }
  21. }

  22. // $ W = $ arr [0];

  23. // $ H = $ arr [1];
  24. // Create image and define colors
  25. $ Image = imagecreatetruecolor ($ width, $ heights); // create a colored Basemap
  26. $ Bg = imagecolorallocate ($ image, 255,255,255 );
  27. Imagefill ($ image, 0, 0, $ bg );
  28. $ Imgsrc = LoadIMG ($ imgsrc, $ arr ['Mime ']);
  29. Imagecopy ($ image, $ imgsrc, $ img_X, $ img_Y, 0, 0, $ arr [0], $ arr [1]);
  30. Imagejpeg ($ image, $ imgdst, 90 );
  31. // Imagedestroy ($ image );
  32. }
  33. // Load the background image
  34. Function LoadIMG ($ imgname, $ mime)
  35. {
  36. If ($ mime = "image/gif "){
  37. $ Im = @ imagecreatefromgif ($ imgname);/* Attempt to open */
  38. }
  39. Elseif ($ mime = "image/png "){
  40. $ Im = @ imagecreatefrompng ($ imgname);/* Attempt to open */
  41. }
  42. Else {
  43. $ Im = @ imagecreatefromjpeg ($ imgname);/* Attempt to open */
  44. }
  45. If (! $ Im) {/* See if it failed */
  46. $ Im = imagecreatetruecolor (150, 30);/* Create a blank image */
  47. $ Bgc = imagecolorallocate ($ im, 255,255,255 );
  48. $ Tc = imagecolorallocate ($ im, 0, 0, 0 );
  49. Imagefilledrectangle ($ im, 0, 0,150, 30, $ bgc );
  50. /* Output an errmsg */
  51. Imagestring ($ im, 1, 5, 5, "Error loading $ imgname", $ tc );
  52. }
  53. Return $ im;
  54. }

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.