php開發文字浮水印教學之操作圖片

操作圖片

/*操作图片*/
  //设置字体的路径
  $font="/tpl/Index/Static/css/img/fonts/Christmas.ttf";
  //添加内容
  $content="欢迎来到php中文网";
  //设置字体的颜色和透明度
  $col= imagecolorallocatealpha ($image,255,255,255,30);
  //写入文字
  imagettftext($image,20,0,20,30,$col,$font,$content);

int imagecolorallocatealpha ( resource $image , int $red , int $green , int $blue , int $alpha )

imagecolorallocatealpha() 的行為和imagecolorallocate() 相同,但多了一個額外的透明度參數alpha,其值從0 到127。0表示完全不透明,127 表示完全透明。

imagettftext函數:

array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

使用 TrueType 字型將 指定的 text 寫入映像。

參數

image

由圖象建立函數(例如imagecreatetruecolor())傳回的圖象資源。

size

字體的尺寸。根據 GD 的版本,為像素尺寸(GD1)或點(磅)尺寸(GD2)。

angle

角度製表示的角度,0 度為從左向右讀的文字。更高數值表示逆時針旋轉。例如 90 度表示從下方向上讀取的文字。

x

由 x,y 所表示的座標定義了第一個字元的基本點(大概是字元的左下角)。這和 imagestring() 不同,其 x,y 定義了第一個字元的左上角。例如 "top left" 為 0, 0。

y

Y 座標。它設定了字體基線的位置,不是字元的最底端。

color

顏色索引。使用負的顏色索引值具有關閉防鋸齒的效果。參見 imagecolorallocate()。

fontfile

是想要使用的 TrueType 字型的路徑。

<?php
     /*打开图片*/
     //1.配置图片路径(填入你的图片路径)
     $src="https://img.php.cn/upload/course/000/000/004/581454f755fb1195.jpg";
     //获取图片信息
     $info = getimagesize($src);
     //通过图像的编号来获取图像的类型
     $type=image_type_to_extension($info[2],false);
     //在内存中创建一个和我们图像类型一样的图像
     $fun = "imagecreatefrom{$type}";
     //把图片复制到我们的内存中
     $image=$fun($src);
    /*操作图片*/
     //设置字体的路径
     $font="/tpl/Index/Static/css/img/fonts/Christmas.ttf";
     //添加内容
     $content="欢迎来到php中文网";
     //设置字体的颜色和透明度
     $col= imagecolorallocatealpha($image,255,255,255,30);
     //写入文字
     imagettftext($image,20,0,20,30,$col,$font,$content);
 ?>

 


#
繼續學習
||
<?php /*打开图片*/ //1.配置图片路径(填入你的图片路径) $src="https://img.php.cn/upload/course/000/000/004/581454f755fb1195.jpg"; //获取图片信息 $info = getimagesize($src); //通过图像的编号来获取图像的类型 $type=image_type_to_extension($info[2],false); //在内存中创建一个和我们图像类型一样的图像 $fun = "imagecreatefrom{$type}"; //把图片复制到我们的内存中 $image=$fun($src); /*操作图片*/ //设置字体的路径 $font="/tpl/Index/Static/css/img/fonts/Christmas.ttf"; //添加内容 $content="欢迎来到php中文网"; //设置字体的颜色和透明度 $col= imagecolorallocatealpha($image,255,255,255,30); //写入文字 imagettftext($image,20,0,20,30,$col,$font,$content); ?>
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!