为了试试图片处理函数的效果,就写了这个小Demo,里面的函数都是单独的,可以根据自己的情况提取,和修改参数。
因为偷懒吧,没有给封装成个类。需要的将就着看下吧。
功能包括
1.图片添加文字 2.图片缩放 3.图片裁剪 4.添加图片水印 5.图片旋转 6.水平翻转 7.垂直翻转
这里不再贴样例图片了
样例地址:http://xhua521.cn/DemoFolder/ImageHandle/index.php
1.ImgHandle.php文件
<?php
$flag = $_GET['flag'];
switch ($flag) {
case 1:
image("headImg.jpg","JPG11111"); //给图片加添字符串
break;
case 2:
thumb("headImg.jpg",50,50); //图片缩放
break;
case 3:
cut("headImg.jpg",50,50,230,130); //图片剪切
break;
case 4:
wetermark("headImg.jpg","shuiyin.png"); //添加水印文字
break;
case 5:
rotate("headImg.jpg",90); //旋转一定角度
break;
case 6:
trun_x("headImg.jpg"); //沿X轴水平翻转
break;
case 7:
trun_y("headImg.jpg"); //沿Y轴水平翻转
break;
default:
thumb("headImg.jpg",50,50); //图片缩放
break;
}
/**
* 为图片添加文字
* string $filename 图片的路径
* string $string 像图片中添加的字符串
*/
function image($filename,$string){
list($width,$height,$type) = getimagesize($filename);
$types = array(1=>"gif",2=>"jpeg",3=>"png");
$createfrom = "imagecreatefrom".$types[$type];
$image = $createfrom($filename);
$x = ($width-imagefontwidth(5)*strlen($string))/2;
$y = ($height-imagefontheight(5))/2;
$textColor = imagecolorallocate($image,255,0,0);
imageString($image,5,$x,$y,$string,$textColor);
header("Content-type:image/$types[$type]");
$output = "image".$types[$type];
$output($image);
imagedestroy($image);
}
// image("headImg.jpg","JPG11111"); //给图片加添字符串
/**
* 修改图片缩放
* string $filename 图片的路径
* int $width 缩放后的宽度
* int $height 缩放后的高度
*/
function thumb($filename,$width=100,$height=100){
list($width_orig,$height_orig,$type) = getimagesize($filename);
$types = array(1=>"gif",2=>"jpeg",3=>"png");
if($width && ($width_orig < $height_orig)){
$width = ($height/$height_orig)*$width_orig;
}else {
$height = ($width/$width_orig)*$height_orig;
}
$image_p = imagecreatetruecolor($width,$height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);
header("Content-type:image/$types[$type]");
imagejpeg($image_p);
imagedestroy($image_p);
imagedestroy($image);
}
// thumb("headImg.jpg",50,50); //图片缩放
/**
* 裁剪图片
* string $filename 图片的路径
* int $x 起始点的x位置
* int $y 起始点的y位置
* int $width 缩放后的宽度
* int $height 缩放后的高度
*/
function cut($filename,$x,$y,$width,$height){
list(,,$type) = getimagesize($filename);
$types = array(1=>"gif",2=>"jpeg",3=>"png");
$createfrom = "imagecreatefrom".$types[$type];
$image = $createfrom($filename);
$cutimg = imagecreatetruecolor($width,$height);
imagecopyresampled($cutimg,$image,0,0,$x,$y,$width,$height,$width,$height);
header("Content-type:image/$types[$type]");
$output = "image".$types[$type];
$output($cutimg);
imagedestroy($cutimg);
imagedestroy($image);
}
// cut("headImg.jpg",50,50,230,130); //图片剪切
/**
* 为图片添加文字
* string $filename 背景图片的路径
* string $water 水印图片路径
*/
function wetermark($filename,$water){
list($b_w,$b_h,$type) = getimagesize($filename);
list($w_w,$w_h) = getimagesize($water);
$posX = mt_rand(0,($b_w - $w_w));
$posY = mt_rand(0,($b_h - $w_h));
$types = array(1=>"gif",2=>"jpeg",3=>"png");
$createfrom = "imagecreatefrom".$types[$type];
$back = $createfrom($filename);
$water = imagecreatefrompng($water);
imagecopy($back,$water,$posX,$posY,0,0,$w_w,$w_h);
header("Content-type:image/jpeg");
$output = "image".$types[$type];
$output($back);
imagedestroy($image);
imagedestroy($back);
}
// wetermark("headImg.jpg","shuiyin.png"); //添加水印文字
/**
* 为图片添加文字
* string $filename 背景图片的路径
* int $degrees 旋转角度
*/
function rotate($filename,$degrees){
list(,,$type) = getimagesize($filename);
$types = array(1=>"gif",2=>"jpeg",3=>"png");
$createfrom = "imagecreatefrom".$types[$type];
$source = $createfrom($filename);
$rotate = imagerotate($source,$degrees,0);
header("Content-type:image/jpeg");
$output = "image".$types[$type];
$output($rotate);
imagedestroy($rotate);
}
// rotate("headImg.jpg",90); //旋转一定角度
/**
* 将图片沿Y轴翻转
* string $filename 背景图片的路径
*/
function trun_y($filename){
list($width,$height,$type) = getimagesize($filename);
//获取宽高也可以使用imagesx()、imagesy()
$types = array(1=>"gif",2=>"jpeg",3=>"png");
$createfrom = "imagecreatefrom".$types[$type];
$back = $createfrom($filename);
$new = imagecreatetruecolor($width,$height);
for ($i=0; $i < $width; $i++) {
imagecopy($new,$back,$width-$i-1,0,$i,0,1,$height);
}
header("Content-type:image/jpeg");
$output = "image".$types[$type];
$output($new);
imagedestroy($new);
}
// trun_y("headImg.jpg"); //沿Y轴水平翻转
/**
* 将图片沿X轴翻转
* string $filename 背景图片的路径
*/
function trun_x($filename){
list($width,$height,$type) = getimagesize($filename);
//获取宽高也可以使用imagesx()、imagesy()
$types = array(1=>"gif",2=>"jpeg",3=>"png");
$createfrom = "imagecreatefrom".$types[$type];
$back = $createfrom($filename);
$new = imagecreatetruecolor($width,$height);
for ($i=0; $i < $height; $i++) {
imagecopy($new,$back,0,$height-$i-1,0,$i,$width,1);
}
header("Content-type:image/jpeg");
$output = "image".$types[$type];
$output($new);
imagedestroy($new);
}
// trun_x("headImg.jpg"); //沿X轴垂直翻转
?>
2.index.php文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>图片处理结果</title>
</head>
<style media="screen">
.div1{
display: inline-block;
border: 1px solid #000;
}
</style>
<body>
<a href="../index.html">返回Demo列表</a>
<br/>
<dl class="dlist">
<dt><h2>原图:</h2></dt>
<dd><img src="./headImg.jpg" alt=""></dd>
<hr/>
<div class="div1">
<dt><h2>加添字符串:</h2></dt>
<dd><img src="./ImgHandle.php?flag=1" alt=""></dd>
</div>
<div class="div1">
<dt><h2>图片缩放:</h2></dt>
<dd><img src="./ImgHandle.php?flag=2" alt=""></dd>
</div>
<div class="div1">
<dt><h2>图片剪切:</h2></dt>
<dd><img src="./ImgHandle.php?flag=3" alt=""></dd>
</div>
<div class="div1">
<dt><h2>添加水印图文:</h2></dt>
<dd><img src="./ImgHandle.php?flag=4" alt=""></dd>
</div>
<div class="div1">
<dt><h2>旋转一定角度:</h2></dt>
<dd><img src="./ImgHandle.php?flag=5" alt=""></dd>
</div>
<div class="div1">
<dt><h2>沿X轴水平翻转:</h2></dt>
<dd><img src="./ImgHandle.php?flag=6" alt=""></dd>
</div>
<div class="div1">
<dt><h2>沿Y轴水平翻转:</h2></dt>
<dd><img src="./ImgHandle.php?flag=7" alt=""></dd>
</div>
</dl>
</body>
</html>
如果代码中有什么不对的地方可以向我提出来~!