温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP中怎么实现生成图片水印

发布时间:2021-06-30 15:36:47 阅读:167 作者:Leah 栏目:编程语言
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

PHP中怎么实现生成图片水印,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

文本水印

我们使用函数watermark_text()来生成文本水印,你必须先指定字体源文件、字体大小和字体文本,具体代码如下:

$font_path = "GILSANUB.TTF"// Font file  $font_size = 30// in pixcels  $water_mark_text_2 = "phpfuns"// Watermark Text  function watermark_text($oldimage_name, $new_image_name)  {  global $font_path, $font_size, $water_mark_text_2;  list($owidth,$oheight) = getimagesize($oldimage_name);  $width = $height = 300;  $image = imagecreatetruecolor($width, $height);  $image_src = imagecreatefromjpeg($oldimage_name);  imagecopyresampled($image, $image_src, 0000, $width, $height, $owidth, $oheight);  $blue = imagecolorallocate($image, 79166185);  imagettftext($image, $font_size, 068190, $blue, $font_path, $water_mark_text_2);  imagejpeg($image, $new_image_name, 100);  imagedestroy($image);  unlink($oldimage_name);  return true;  }

可以在这里查看demo。

图片水印

我们使用函数watermark_image()来生成图片水印,你必须先水银图片的源文件。具体代码如下:

$image_path "phpfuns.png";  function watermark_image($oldimage_name$new_image_name)  /{  global $image_path;  list($owidth,$oheight) = getimagesize($oldimage_name);  $width $height 300;  $im imagecreatetruecolor($width$height);  $img_src imagecreatefromjpeg($oldimage_name);  imagecopyresampled($im$img_src0000$width$height$owidth$oheight);  $watermark imagecreatefrompng($image_path);  list($w_width$w_height) = getimagesize($image_path);  $pos_x $width $w_width;  $pos_y $height $w_height;  imagecopy($im$watermark$pos_x$pos_y00$w_width$w_height);  imagejpeg($im$new_image_name100);  imagedestroy($im);  unlink($oldimage_name);  return true;  }

你可以在这里查看demo。

上传图片表单

我们使用下面的表单来上传图片:

<?php  $demo_image"";  if(isset($_POST['createmark']) and $_POST['createmark'] == "Submit")  {  $path "uploads/";  $valid_formats array("jpg""bmp","jpeg");  $name $_FILES['imgfile']['name'];  if(strlen($name))  {  list($txt$ext) = explode("."$name);  if(in_array($ext,$valid_formats) && $_FILES['imgfile']['size'] <= 256*1024)  {  $upload_status move_uploaded_file($_FILES['imgfile']['tmp_name'], $path.$_FILES['imgfile']['name']);  if($upload_status){  $new_name $path.time().".jpg";  // Here you have to user functins watermark_text or watermark_image  if(watermark_text($path.$_FILES['imgfile']['name'], $new_name))  $demo_image = $new_name;  }  }  else $msg="File size Max 256 KB or Invalid file format.";  }  }  ?>  // HTML Code  <form name="imageUpload" method="post" enctype="multipart/form-data" >  Upload Image  Image :<input type="file" name="imgfile" /><br />  <input type="submit" name="createmark" value="Submit" />  <?php  if(!emptyempty($demo_image))  echo '<img src="'.$demo_image.'" />';  ?>  </form>

关于PHP中怎么实现生成图片水印问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI

开发者交流群×