Home >Backend Development >PHP Tutorial >PHP adds background example code to pictures

PHP adds background example code to pictures

WBOY
WBOYOriginal
2016-07-25 08:53:423911browse
  1. function overlayjpg($imgsrc,$imgdst,$width,$height="")

  2. {
  3. //$imgsrc jpg format image path $imgdst jpg format image save File name $imgwidth Width to change $imgheight Height to change
  4. //Get the width and height value of the image
  5. $arr = getimagesize($imgsrc);
  6. //Calculate the X-axis position 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 color 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 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. }

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn