PHP 文字,图片水印,缩略图,裁切成小图(大小变小)

文字水印基本思路:
1。用getimagesize()获取图片的信息(as:大小,属性等);
2。根据图片信息用imagecreatefromjpeg

()/imagecreatefromgif/imagecreatepng创建图片(即标识符);
3。设置要用到的字体(as:"arial.ttf",一定要给出存放的正确路径,如在

windows系统中,可到windows/fonts中把文件拷贝到程序的目录,或者从网

上下载到本地);
4。设置作为水印的文字;
5。用imagecolorallocate()设置水印文字的颜色;
6。用imagettftext()把文字写到创建的图片中;
7。根据图片的属性设置头部属性(as:header("content-

type:image/jpeg")));
8。用imagejpeg(),imagepng()等相应的函数输出图像;其中:如:

imagejpeg($im)是给出相应的效果,没有真正打上水印,要真正给图打水印

,则给出相应的参数;如:imagejpeg($im, $newimagename[, $flag]);
9。用imagedestroy()销毁生成的内容

代码: 

文字水印
<?php
$image = "1.jpg";
$ims = getimagesize($image);
list($width, $height) = $ims;
switch($ims[2]){
 case 1:
  $im = imagecreatefromjpeg($image);
  break;
 case 2:
  $im = imagecreatefromjpeg($image);
  break;
 case 3:
  $im = imagecreatefrompng($image);
  break;
}
$font = "arial.ttf";
$text = "hi,world!";
$col = imagecolorallocate($im, 255, 0, 0);
imagettftext($im, 12, 0, rand(0, $width), rand(0, $height), $col,

$font,$text);
header("Content-type:image/jpeg");
//imagejpeg($im, 'ok.jpg');
imagejpeg($im);
imagedestroy($im);
?>

图片水印基本思路:
1。给出两幅图片,一幅要打水印的,一幅是作为水印的;
2。用getimagesize()获取图片的信息(如是jpeg,gif,png等属性);
3。根据图片的信息选择函数imagecreatefromgif(), imagecreatefrompng()

等分别创建两个图片(图片标识符);
4。用imagecopy()函数把水印图片复制到要打水印的图片;
5。根据图片的属性选择头部函数(as:header("content-

type:image/jpeg"), header("content-type:image/gif"));
6。根据图片的属性用imagejpeg() imagepng() 等函数输出图片。如果是一

个参数的话,相当于是预览,多个参数即给出新图名称,新图就是打上水印

的效果图。
7。用imagedestroy()销毁生成的内容

示例代码:

图片水印
<?php
$image = "1.jpg";
$ims = getimagesize($image);
$logo = "2.jpg";
$ins = getimagesize($logo);

switch($ims[2]){
 case 1:
  $im = imagecreatefromgif($image);
  break;
 case 2:
  $im = imagecreatefromjpeg($image);
  break;
 case 3:
  $im = imagecreatefrompng($image);
  break;
}

switch($ins[2]){
 case 1:
  $in = imagecreatefromgif($logo);
  break;
 case 2:
  $in = imagecreatefromjpeg($logo);
  break;
 case 3:
  $in = imagecreatefrompng($logo);
  break;
 
}
imagecopy($im, $in, rand(10,300), rand(10, 50), 20, 20, 100, 100);

header("Content-type:image/jpeg");
//imagejpeg($im, 'final.jpg');
imagejpeg($im);
imagedestroy($in);
imagedestroy($im);
?>

裁切成小图:
基本思路:
1。分别创建两个图片区域,一个是要输出的图片(原来没有的),用

imagecreatetruecolor()创建,一个是原来存在的图片(即原图),用

imagecreatefromjpeg(),imagecreatefromgif等函数创建;
2。用imagecopyresized()函数把原图填充新区域;
3。用imagejpeg(), imagegif();等函数输出小图,如给出多个参数,就可以
生成小图存放了。
4。用imagedestroy()销毁生成的内容

小图
示例代码:
<?php

$filename = '1.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth,

$newheight, $width, $height);

// Output
imagejpeg($thumb, 'final.jpg');
imagejpeg($thumb);
imagedestroy($thumb);
imagedestroy($source);
?>

原文地址:https://www.cnblogs.com/lin3615/p/3543536.html