您的位置:首页 > 编程语言 > PHP开发

PHP给图片添加水印

2016-07-20 11:22 323 查看
PHP 拷贝图像 imagecopy 与 imagecopyresized 函数

imagecopy() 函数用于拷贝图像或图像的一部分。

  imagecopyresized() 函数用于拷贝部分图像并调整大小。

[b]imagecopy()[/b]

imagecopy() 函数用于拷贝图像或图像的一部分,成功返回 TRUE ,否则返回 FALSE 。

  语法:

  bool imagecopy( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,

  int src_w, int src_h )

  参数说明:参数说明

  dst_im目标图像

  src_im被拷贝的源图像

  dst_x目标图像开始 x 坐标

  dst_y目标图像开始 y 坐标,x,y同为 0 则从左上角开始

  src_x拷贝图像开始 x 坐标

  src_y拷贝图像开始 y 坐标,x,y同为 0 则从左上角开始拷贝

  src_w(从 src_x 开始)拷贝的宽度

  src_h(从 src_y 开始)拷贝的高度

  例子:

  <?php

  header("Content-type: image/jpeg");

  //创建目标图像

  $dst_im = imagecreatetruecolor(150, 150);

  //源图像

  $src_im = @imagecreatefromjpeg("images/flower_1.jpg");

  //拷贝源图像左上角起始 150px 150px

  imagecopy( $dst_im, $src_im, 0, 0, 0, 0, 150, 150 );

  //输出拷贝后图像

  imagejpeg($dst_im);

  imagedestroy($dst_im);

  imagedestroy($src_im);

  ?>

<?php
//这里仅仅是为了案例需要准备一些素材图片
$url = 'http://www.iyi8.com/uploadfile/2014/0521/20140521105216901.jpg';
$content = file_get_contents($url);
$filename = 'tmp.jpg';
file_put_contents($filename, $content);
$url = 'http://wiki.ubuntu.org.cn/images/3/3b/Qref_Edubuntu_Logo.png';
file_put_contents('logo.png', file_get_contents($url));
//开始添加水印操作
$im = imagecreatefromjpeg($filename);
$logo = imagecreatefrompng('logo.png');
$size = getimagesize('logo.png');
imagecopy($im, $logo, 600, 150, 0, 00, $size[0], $size[1]);

header("content-type: image/jpeg");
imagejpeg($im);
?>


imagecopyresized()

imagecopyresized() 函数用于拷贝图像或图像的一部分并调整大小,成功返回 TRUE ,否则返回 FALSE 。

  语法:

  bool imagecopyresized( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,

  int dst_w, int dst_h, int src_w, int src_h )

  本函数参数可参看 imagecopy() 函数,只是本函数增加了两个参数(注意顺序):

  dst_w:目标图像的宽度。

  dst_h:目标图像的高度。

  imagecopyresized() 的典型应用就是生成图片的缩略图:

  <?php

  header("Content-type: image/jpeg");

  //原图文件

  $file = "images/flower_1.jpg";

  // 缩略图比例

  $percent = 0.5;

  // 缩略图尺寸

  list($width, $height) = getimagesize($file);

  $newwidth = $width * $percent;

  $newheight = $height * $percent;

  // 加载图像

  $src_im = @imagecreatefromjpeg($file);

  $dst_im = imagecreatetruecolor($newwidth, $newheight);

  // 调整大小

  imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

  //输出缩小后的图像

  imagejpeg($dst_im);

  imagedestroy($dst_im);

  imagedestroy($src_im);

  ?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: