php 创建图片教程
<?php
$height = 300;
$width = 300;
$im = ImageCreateTrueColor($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$blue = ImageColorAllocate ($im, 0, 0, 64);
ImageFill($im, 0, 0, $blue);
//ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 10, 100, 120, 'Hello,PHP', $white);
Header ('Content-type: image/png');
ImagePng($im);
ImageDestroy($im);
?>
php 图片下载程序function displayimg($image)
{
$image = './upload/'.imgdecode($image);
//echo $image;exit;
if (file_exists($image))
{
$info = getimagesize($image);
header("Content-type:$info[mime]");
switch ($info[2])
{
case 1: $cimage = imagecreatefromgif($image);
imagegif($cimage);
imagedestroy($cimage);
break;
case 2: $cimage = imagecreatefromjpeg($image);
imagejpeg($cimage);
imagedestroy($cimage);
break;
case 3: $cimage = imagecreatefrompng($image);
imagepng($cimage);
imagedestroy($cimage);
break;
case 6: $cimage = imagecreatefromwbmp($image);
imagewbmp($cimage);
imagedestroy($cimage);
break;
}
echo $simage;
}
else
{
header("HTTP/1.0 404 Not Found");
}
}
<?php
//公用函数
//把角度转换为弧度
function deg2Arc($degrees) {
return($degrees * (pi()/180.0));
}
//RGB
function getRGB($color){
$R=($color>>16) & 0xff;
$G=($color>>8) & 0xff;
$B=($color) & 0xff;
return (array($R,$G,$B));
}
// 取得在椭圆心为(0,0)的椭圆上 x,y点的值
function pie_point($deg,$va,$vb){
$x= cos(deg2Arc($deg)) * $va;
$y= sin(deg2Arc($deg)) * $vb;
return (array($x, $y));
}
//3D饼图类
class chart{
var $a; //椭圆长半轴
var $b; //椭圆短半轴
var $DataArray; //每个扇形的数据
var $ColorArray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x
//为边缘及阴影为黑色
function chart($pa=100,$pb=60,$sData="100,200,300,400,500,300", $sColor="ee00ff,dd0000,cccccc,ccff00,00ccff,ccff00")
{
$this->a=$pa;
$this->b=$pb;
$this->DataArray=split(",",$sData);
$this->ColorArray=split(",",$sColor);
}
function setA($v){
$this->a=$v;
}
function getA(){
return $this->a;
}
function setB($v){
$this->b=$v;
}
function getB(){
return $this->b;
}
function setDataArray($v){
$this->DataArray=split(",",$v);
}
function getDataArray($v){
return $this->DataArray;
}
function setColorArray($v){
$this->ColorArray=split(",",$v);
}
function getColorArray(){
return $this->ColorArray;
}
function DrawPie(){
$image=imagecreate($this->a*2+40,$this->b*2+40);
$PieCenterX=$this->a+10;
$PieCenterY=$this->b+10;
$DoubleA=$this->a*2;
$DoubleB=$this->b*2;
list($R,$G,$B)=getRGB(0);
$colorBorder=imagecolorallocate($image,$R,$G,$B);
$DataNumber=count($this->DataArray);
//$DataTotal
for($i=0;$i<$DataNumber;$i++) $DataTotal+=$this->DataArray[$i]; //算出数据和
//填充背境
imagefill($image, 0, 0, imagecolorallocate($image, 0xFF, 0xFF, 0xFF));
/*
** 画每一个扇形
*/
$Degrees = 0;
for($i = 0; $i < $DataNumber; $i++){
$StartDegrees = round($Degrees);
$Degrees += (($this->DataArray[$i]/$DataTotal)*360);
$EndDegrees = round($Degrees);
$percent = number_format($this->DataArray[$i]/$DataTotal*100, 1);
list($R,$G,$B)=getRGB(hexdec($this->ColorArray[$i]));
$CurrentColor=imagecolorallocate($image,$R,$G,$B);
if ($R>60 and $R<256) $R=$R-60;
if ($G>60 and $G<256) $G=$G-60;
if ($B>60 and $B<256) $B=$B-60;
$CurrentDarkColor=imagecolorallocate($image,$R,$G,$B);
//画扇形弧
imagearc($image,$PieCenterX,$PieCenterY,$DoubleA,$DoubleB,$StartDegrees,$EndDegrees,$CurrentColor);
//画直线
list($ArcX, $ArcY) = pie_point($StartDegrees , $this->a , $this->b);
imageline($image,$PieCenterX,$PieCenterY,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY),$CurrentColor);
//画直线
list($ArcX, $ArcY) = pie_point($EndDegrees,$this->a , $this->b);
imageline($image,$PieCenterX,$PieCenterY,ceil($PieCenterX + $ArcX),ceil($PieCenterY + $ArcY),$CurrentColor);
//填充扇形
$MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees);
list($ArcX, $ArcY) = Pie_point($MidPoint, $this->a*3/4 , $this->b*3/4);
imagefilltoborder($image,floor($PieCenterX + $ArcX),floor($PieCenterY + $ArcY), $CurrentColor,$CurrentColor);
imagestring($image,2,floor($PieCenterX + $ArcX-5),floor($PieCenterY + $ArcY-5),$percent."%",$colorBorder);
//画阴影
if ($StartDegrees>=0 and $StartDegrees<=180){
if($EndDegrees<=180){
for($k = 1; $k < 15; $k++)
imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, $EndDegrees, $CurrentDarkColor);
}else{
for($k = 1; $k < 15; $k++)
imagearc($image,$PieCenterX, $PieCenterY+$k,$DoubleA, $DoubleB, $StartDegrees, 180, $CurrentDarkColor);
}
}
}
/*到此脚本已经生了一幅图像了
**现在需要的是把它发到浏览器上,重要的一点是要将标头发给浏览器,让它知道是一个GIF文件。不然的话你只能看到一堆奇怪的乱码
*/
//输出生成的图片
header("Content-type: image/gif");
imagegif($image);
imagedestroy($image);
}//End drawPie()
}//End class
//实现
$objp = new chart();
$objp->DrawPie();
?>
php上传图片并生成缩位图代码,我们时常要上传图片,但也要保留自己的版权所以就会用到图片加水印哦,下面的程序就是上传图片成功后再给图片加上你自己做的水印效果哦.
<?php
class Image {
var $imageResource = NULL;
var $target = NULL;
var $enableTypes = array();
var $imageInfo = array();
var $createFunc = '';
var $imageType = NULL;
/**
* Construct for this class
*
* @param string $image
* @return Image
*/
function Image($image = NULL) {
//get enables
if(imagetypes() & IMG_GIF) {
$this->enableTypes[] = 'image/gif';
}
if(imagetypes() & IMG_JPEG) {
$this->enableTypes[] = 'image/jpeg';
}
if (imagetypes() & IMG_JPG) {
$this->enableTypes[] = 'image/jpg';
}
if(imagetypes() & IMG_PNG) {
$this->enableTypes[] = 'image/png';
}
//end get
if($image != NULL) {
$this->setImage($image);
}
}
/**
* set a image resource
*
* @param string $image
* @return boolean
*/
function setImage($image) {
if(file_exists($image) && is_file($image)) {
$this->imageInfo = getimagesize($image);
$img_mime = strtolower($this->imageInfo['mime']);
if(!in_array($img_mime, $this->enableTypes)) {
exit('系统不能操作这种图片类型.');
}
switch ($img_mime) {
case 'image/gif':
$link = imagecreatefromgif($image);
$this->createFunc = 'imagegif';
$this->imageType = 'gif';
break;
case 'image/jpeg':
case 'image/jpg':
$link = imagecreatefromjpeg($image);
$this->createFunc = 'imagejpeg';
$this->imageType = 'jpeg';
break;
case 'image/png':
$link = imagecreatefrompng($image);
$this->createFunc = 'imagepng';
$this->imageType = 'png';
break;
default:
$link = 'unknow';
$this->imageType = 'unknow';
break;
}
if($link !== 'unknow') {
$this->imageResource = $link;
} else {
exit('这种图片类型不能改变尺寸.');
}
unset($link);
return true;
} else {
return false;
}
}
/**
* set header
*
*/
function setHeader() {
switch ($this->imageType) {
case 'gif':
header('content-type:image/gif');
break;
case 'jpeg':
header('content-type:image/jpeg');
break;
case 'png':
header('content-type:image/png');
break;
default:
exit('Can not set header.');
break;
}
return true;
}
/**
* change the image size
*
* @param int $width
* @param int $height
* @return boolean
*/
function changeSize($width, $height = -1) {
if(!is_resource($this->imageResource)) {
exit('不能改变图片的尺寸,可能是你没有设置图片来源.');
}
$s_width = $this->imageInfo[0];
$s_height = $this->imageInfo[1];
$width = intval($width);
$height = intval($height);
if($width <= 0) exit('图片宽度必须大于零.');
if($height <= 0) {
$height = ($s_height / $s_width) * $width;
}
$this->target = imagecreatetruecolor($width, $height);
if(@imagecopyresized($this->target, $this->imageResource, 0, 0, 0, 0, $width, $height, $s_width, $s_height))
return true;
else
return false;
}
/**
* Add watermark
*
* @param string $image
* @param int $app
*/
function addWatermark($image, $app = 50) {
if(file_exists($image) && is_file($image)) {
$s_info = getimagesize($image);
} else {
exit($image . '文件不存在.');
}
$r_width = $s_info[0];
$r_height = $s_info[1];
if($r_width > $this->imageInfo[0]) exit('水印图片必须小于目标图片');
if($r_height > $this->imageInfo[1]) exit('水印图片必须小于目标图片');
switch ($s_info['mime']) {
case 'image/gif':
$resource = imagecreatefromgif($image);
break;
case 'image/jpeg':
case 'image/jpg':
$resource = imagecreatefromjpeg($image);
break;
case 'image/png':
$resource = imagecreatefrompng($image);
break;
default:
exit($s_info['mime'] .'类型不能作为水印来源.');
break;
}
$this->target = &$this->imageResource;
imagecopymerge($this->target, $resource, $this->imageInfo[0] - $r_width - 5, $this->imageInfo[1] - $r_height - 5, 0,0 ,$r_width, $r_height, $app);
imagedestroy($resource);
unset($resource);
}
/**
* create image
*
* @param string $name
* @return boolean
*/
function create($name = NULL) {
$function = $this->createFunc;
if($this->target != NULL && is_resource($this->target)) {
if($name != NULL) {
$function($this->target, $name);
} else {
$function($this->target);
}
return true;
} else if($this->imageResource != NULL && is_resource($this->imageResource)) {
if($name != NULL) {
$function($this->imageResource, $name);
} else {
$function($this->imageResource);
}
return true;
} else {
exit('不能创建图片,原因可能是没有设置图片来源.');
}
}
/**
* free resource
*
*/
function free() {
if(is_resource($this->imageResource)) {
@imagedestroy($this->imageResource);
}
if(is_resource($this->target)) {
@imagedestroy($this->target);
}
}
}
?>
<?php
require_once("../dbconnect.php");
$proimagepath="photo/";
$product_id=$_POST["product_id"];
$product_pid=$_POST["product_pid"];
$product_title=$_POST["product_title"];
$product_name=$_POST["product_name"];
$product_xinghao=$_POST["product_xinghao"];
$product_yongtu=$_POST["product_yongtu"];
$product_danjia=$_POST["product_danjia"];
$product_content=mysql_escape_string($_POST["content1"]);
$product_date=date("YmdHis");
$product_smallid=$_POST["product_catename"];
$product_img=$_FILES["smallimage"];
$smalltype=$product_img["type"];
////数据验证////////////////
if($product_name=="")
{
alert_back("产品名称不能为空!");
exit;
}
if(!$smalltype=getImgType($smalltype))
{
alert_back("上传的图片类型不对!");
exit;
}
///////////上传图片/////////////////
$smallimgname=$product_smallid.$product_date.".".$smalltype;
$smallimgpath=$proimagepath.$smallimgname;
if(!move_uploaded_file($product_img["tmp_name"],"../".$smallimgpath))
{
alert_back("上传图片失败!");
exit;
}
//////////添加数据到数据库///////////////
$insertSQL="insert into product (product_title,product_name,product_xinghao,product_yongtu,product_danjia,product_img,product_content,product_date,product_smallid,product_pid) values
('$product_title','$product_name','$product_xinghao','$product_yongtu','$product_danjia','$smallimgpath','$product_content','$product_date','$product_smallid','$product_pid')";
mysql_query("SET NAMES 'gb2312'");
//echo($insertSQL);
if(mysql_query($insertSQL))
{
alert_back("添加产品成功!");
}
else
{
alert_back("添加产品失败!");
}
?>
在网上找的水印代码:
/*
* 功能:PHP图片水印 (水印支持图片或文字)
* 参数:
* $product_img 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
* $waterPos 水印位置,有10种状态,0为随机位置;
* 1为顶端居左,2为顶端居中,3为顶端居右;
* 4为中部居左,5为中部居中,6为中部居右;
* 7为底端居左,8为底端居中,9为底端居右;
* $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
* $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
* $textFont 文字大小,值为1、2、3、4或5,默认为5;
* $textColor 文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
*
* 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
* $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。
* 当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。
* 加水印后的图片的文件名和 $product_img 一样。
* 作者:longware @ 2004-11-3 14:15:13
*/
function imageWaterMark($product_img,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
{
$isWaterImage = FALSE;
$formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
//读取水印文件
if(!empty($waterImage) && file_exists($waterImage))
{
$isWaterImage = TRUE;
$water_info = getimagesize($waterImage);
$water_w = $water_info[0];//取得水印图片的宽
$water_h = $water_info[1];//取得水印图片的高
switch($water_info[2])//取得水印图片的格式
{
case 1water_im = imagecreatefromgif($waterImage);break;
case 2water_im = imagecreatefromjpeg($waterImage);break;
case 3water_im = imagecreatefrompng($waterImage);break;
default:die($formatMsg);
}
}
//读取背景图片
if(!empty($product_img) && file_exists($product_img))
{
$ground_info = getimagesize($product_img);
$ground_w = $ground_info[0];//取得背景图片的宽
$ground_h = $ground_info[1];//取得背景图片的高
switch($ground_info[2])//取得背景图片的格式
{
case 1:$ground_im = imagecreatefromgif($product_img);break;
case 2:$ground_im = imagecreatefromjpeg($product_img);break;
case 3:$ground_im = imagecreatefrompng($product_img);break;
default:die($formatMsg);
}
}
else
{
die("需要加水印的图片不存在!");
}
//水印位置
if($isWaterImage)//图片水印
{
$w = $water_w;
$h = $water_h;
$label = "图片的";
}
else//文字水印
{
$temp = imagettfbbox(ceil($textFont*2.5),0,"arial.ttf",$waterText);//取得使用 TrueType 字体的文本的范围
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
unset($temp);
$label = "文字区域";
}
if( ($ground_w<$w) || ($ground_h<$h) )
{
echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
return;
}
switch($waterPos)
{
case 0://随机
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
case 1://1为顶端居左
$posX = 0;
$posY = 0;
break;
case 2://2为顶端居中
$posX = ($ground_w - $w) / 2;
$posY = 0;
break;
case 3://3为顶端居右
$posX = $ground_w - $w;
$posY = 0;
break;
case 4://4为中部居左
$posX = 0;
$posY = ($ground_h - $h) / 2;
break;
case 5://5为中部居中
$posX = ($ground_w - $w) / 2;
$posY = ($ground_h - $h) / 2;
break;
case 6://6为中部居右
$posX = $ground_w - $w;
$posY = ($ground_h - $h) / 2;
break;
case 7://7为底端居左
$posX = 0;
$posY = $ground_h - $h;
break;
case 8://8为底端居中
$posX = ($ground_w - $w) / 2;
$posY = $ground_h - $h;
break;
case 9://9为底端居右
$posX = $ground_w - $w;
$posY = $ground_h - $h;
break;
default://随机
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
}
//设定图像的混色模式
imagealphablending($ground_im, true);
if($isWaterImage)//图片水印
{
imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
}
else//文字水印
{
if( !empty($textColor) && (strlen($textColor)==7) )
{
$R = hexdec(substr($textColor,1,2));
$G = hexdec(substr($textColor,3,2));
$B = hexdec(substr($textColor,5));
}
else
{
die("水印文字颜色格式不正确!");
}
imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
}
//生成水印后的图片
@unlink($product_img);
switch($ground_info[2])//取得背景图片的格式
{
case 1:imagegif($ground_im,$product_img);break;
case 2:imagejpeg($ground_im,$product_img);break;
case 3:imagepng($ground_im,$product_img);break;
default:die($errorMsg);
}
//释放内存
if(isset($water_info)) unset($water_info);
if(isset($water_im)) imagedestroy($water_im);
unset($ground_info);
imagedestroy($ground_im);
}
//---------------------------------------------------------------------------------------
if(isset($_FILES) && !empty($_FILES['userfile']) && $_FILES['userfile']['size']>0)
{
$uploadfile = "./".time()."_".$_FILES['userfile']['name'];
if (copy($_FILES['userfile']['tmp_name'], $uploadfile))
{
echo "OK<br>";
//文字水印
imageWaterMark($uploadfile,0,"","http://www.hi-pwr.com",5,"#FF0000");
//图片水印
//$waterImage="images/bz.gif";//水印图片路径
//imageWaterMark($uploadfile,0,$waterImage);
echo "<img src="".$uploadfile."" border="0">";
}
else
{
echo "Fail<br>";
}
}
相关文章
- 今天小编在这里就来给Painter的这一款软件的使用者们来说一说绘制红衣喝酒男水粉画效果的教程,各位想知道具体绘制步骤的使用者,那么下面就快来跟着小编一起看一看教程...2016-09-14
iPhone6怎么激活?两种苹果iPhone6激活教程图文详解
iPhone6新机需要激活后才可以正常使用,那么对于小白用户来说,iPhone6如何激活使用呢?针对此问题,本文就为大家分别介绍Wifi无线网络激活以及iPhone6连接电脑激活这两种有效的方法,希望本文能够帮助到大家...2022-09-14- 今天小编在这里就来给各位photoshop的这一款软件的使用者们来说下制作雨中野外孤独行走的一头牛海报的教程,各位想知道具体制作方法的使用者们,大家就快来看一看小编给...2016-09-14
- 今天小编在这里就来给Painter的这一款软件的使用者们来说一下绘制帅气卡通魔法王子漫画的具体教程,各位想知道绘制步骤的使用者,那么下面就快来跟着小编一起看一看教程...2016-09-14
- 今天小编在这里就来给各位Illustrator的这一款软件的使用者们来说说鼠绘堆雪人的孩童矢量插画的教程,各位想知道具体绘制方法的使用者们,那么各位就快来跟着小编来看看...2016-09-14
- 今天小编在这里就来给美图秀秀的这一款软件的使用者们来说下究竟该怎么给照片天空加蓝天白云的教程,各位想知道具体制作步骤的,那么下面就来跟着小编一起看看吧。 ...2016-09-14
- 支付宝支付在国内算是大家了,我们到处都可以使用支付宝了,下文整理介绍的是在安卓app应用中使用支付宝进行支付的开发例子。 之前讲了一篇博客关与支付宝集成获取...2016-09-20
- 今天小编在这里就来给llustrator的这一款软件的使用者们来说一说绘制扁平化风格卡通警察护士空姐肖像的教程,各位想知道具体绘制步骤的使用者们,那么下面就快来跟着小编...2016-09-14
- 今天小编在这里就来给Illustrator的这一款软件的使用者们来说一下绘制一个方形的录音机图标的教程,各位想知道具体绘制方法的使用者们,那么下面就来看一下小编给大家分...2016-09-14
使用PHP+JavaScript将HTML页面转换为图片的实例分享
这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19- 今天小编在这里就来给photoshop的这一款软件的使用者们来说一说简单制作一个搞笑的换脸表情包的教程,各位想知道具体制作方法的使用者们,那么大家就快来看一看教程吧。...2016-09-14
- 今天小编在这里就来给各位photoshop的这一款软件的使用者们来说说给手绘画调色变换场景的后期教程,各位想知道具体后期处理步骤的使用者们,那么大家就快来跟着小编来看...2016-10-02
- php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 今天小编在这里就来给美图秀秀的这一款软件的使用者们来说一下让你胸丰满起来的处理教程,各位想知道具体处理步骤的,那么下面就快来跟着小编一起看一下教程吧。 给...2016-09-14
- 今天小编在这里就来给Painter的这一款软件的使用者们来说一下绘制雷神传插画的教程,各位想知道具体绘制步骤的使用者,那么下面就快来跟着小编一起看看绘制方法吧。 ...2016-09-14
- 下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13