Home > Backend Development > PHP Tutorial > Example of how to add watermark to images in php

Example of how to add watermark to images in php

墨辰丷
Release: 2023-03-29 10:12:01
Original
2239 people have browsed it

This article mainly shares with you an example of adding watermarks to php images, which has certain reference value. Interested friends can refer to

Adding watermarks to images. I believe all friends know it. Today we will look at an example of adding watermarks to pictures in PHP. I hope this article can help you all.

<?php 
  /** 
   * 图片添加水印 
   * $target 源文件路径 
   * $wtrmrk_file 水印图片路径 
   * $newcopy 添加水印后的图片路径 
   * 
   */ 
  public function watermark_image($target, $wtrmrk_file, $newcopy) { 
    $watermark = imagecreatefrompng($wtrmrk_file); 
    imagealphablending($watermark, false); 
    imagesavealpha($watermark, true); 
    $img = imagecreatefromjpeg($target); 
    $img_w = imagesx($img); 
    $img_h = imagesy($img); 
    $wtrmrk_w = imagesx($watermark); 
    $wtrmrk_h = imagesy($watermark); 
    $dst_x = ($img_w ) – ($wtrmrk_w); // For centering the watermark on any image //phpfensi.com 
    $dst_y = ($img_h) – ($wtrmrk_h ); // For centering the watermark on any image 
    imagecopy($img, $watermark, $dst_x, $dst_y, 0, 0, $wtrmrk_w, $wtrmrk_h); 
     
    imagejpeg($img, $newcopy, 100); 
    imagedestroy($img); 
    imagedestroy($watermark); 
    //return $img; 
  } 
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Detailed explanation of the steps to generate data dictionary in PHP

##phpFramework CodeIgniter database Detailed explanation of the configuration steps

php7 Detailed explanation of the use of new features

The above is the detailed content of Example of how to add watermark to images in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template