php入门教程七(图片操作与实例)
本文章利用用大量的例子来讲解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);
//绘制字符串:hello,php
imagestring($im, 10, 100, 120, 'hello,php', $white);
//输出图像,定义头
header ('content-type: image/png');
//将图像发送至浏览器
imagepng($im);
//清除资源
imagedestroy($im);
?>
实例二生成缩略图片
<?php
header("content-type: image/jpeg");
// 载入图像
$imagen1 = imagecreatefromjpeg("imagen1.jpg");
$imagen2 = imagecreatefromjpeg("imagen2.jpg");// 复制图像
imagecopy($imagen1,$imagen2,0,0,0,0,200,150);// 输出jpeg图像
imagejpeg($imagen1);//释放内存
imagedestroy($imagen2);
imagedestroy($imagen1);?>
获取图片大小信息
<?php
$info = getimagesize("imagen2.jpg");
print_r($info);?>
绘制png图片
<?php
//png格式图像处理函数
function loadpng ($imgname) {
$im = @imagecreatefrompng ($imgname);
if (!$im) { //载入图像失败
$im = imagecreate (400, 30);
$bgc = imagecolorallocate ($im, 255, 255, 255);
$tc = imagecolorallocate ($im, 0, 0, 0);
imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
imagestring($im, 4, 5, 5, "error loading: $imgname", $tc);
}
return $im;
}
$imgpng=loadpng("./karte.png");
/* 输出图像到浏览器 */
header("content-type: image/png");
imagepng($imgpng);
?>
给图片加文字
<?php
//创建 100*30 图像
$im = imagecreate(100, 30);
// white background and blue text
$bg = imagecolorallocate($im, 200, 200, 200);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// write the string at the top left
imagestring($im, 5, 0, 0, "hello world!", $textcolor);
// output the image
header ("content-type: image/jpeg");
imagejpeg ($im);
imagedestroy($im);?>
主要是购物车部分代码的讲解,和对session操作的php教程代码演示。
<?php
//
// add_item.php:
// add an item to the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
session_register('cart');
}require 'lib.inc.php'; // loadproducts()
loadproducts(); // load products in $master_products_list
// make $curr_product global
$curr_product = array();// loop through all the products and pull up the product
// that we are interested in
foreach ($master_products_list as $prod_id => $product) {
if (trim($prod_id) == trim($_get[id])) {
$curr_product = $product;
}
}
// register our session
//session_register('cart');
//if(session_is_registered('cart')) echo "已经注册";
if ($_post[ordered]) { // if they have chosen the product
array_push($_session[cart][products], array(trim($_post[id]), $_post[quantity]));
$_session[cart][num_items] += $_post[quantity];
}
?><html>
<head>
<title>
<?php if ($_post[ordered]) { ?>
已经添加 <?php echo $curr_product[name]; ?> 到您的购物篮
<?php } else { ?>
添加 <?php echo $curr_product[name]; ?> 到您的购物篮
<?php } ?>
</title>
</head>
<body>
<?php if ($_post[ordered]) { ?>
<h1><?php echo $curr_product[name]; ?>
添加至购物篮成功</h1><a href="cart.php">返回</a> 商品列表页面.
<?php } else { ?>
<h1>添加 <?php echo $curr_product[name]; ?> 到您的购物篮</h1><form action="<?php echo $php_self; ?>" method="post">
商品名称: <?php echo $curr_product[name]; ?>
<br>
商品说明: <?php echo $curr_product[desc]; ?>
<br>
商品单价: rmb<?php echo $curr_product[price]; ?>
<br>
商品数量: <input type="text" size="7" name="quantity">
<input type="hidden" name="id" value="<?php echo $_get[id]; ?>">
<input type="hidden" name="ordered" value="1"><input type="submit" value="添加至购物栏">
</form>
<?php } ?>
</body>
</html>
php代码
<?php
//
// cart.php: the main file
//
session_start();require 'lib.inc.php';
//判断购物篮会话变量cart是否注册,不注册则注册cart变量
if (session_is_registered('cart')) {
session_register('cart');
}
// 如果购物篮没有初始化,则初始化购物篮
if (!isset($_session[cart][num_items])) {
$_session[cart] = array("num_items" => 0,
"products" => array());
}
// from site_lib.inc, loads the $master_products_list array
loadproducts(); //载入物品列表
?><html>
<head>
<title>演示会话跟踪的购物篮程序</title>
</head><body>
<h1>欢迎进入网上商店</h1>
<?php
if ($_session[cart][num_items]) { // if there is something to show
?>
<h2>当前在购物篮里的物品</h2>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
<th>
商品名称
</th>
<th>
商品说明
</th>
<th>
单价
</th>
<th>
数量
</th>
<th>
</th>
</tr>
<?php
// loop through the products
foreach ($_session[cart][products] as $i => $product) {
$product_id = $product[0];
$quantity = $product[1];$total += $quantity *
(double)$master_products_list[$product_id][price];
?>
<tr>
<td>
<?php echo $master_products_list[$product_id][name]; ?>
</td>
<td>
<?php echo $master_products_list[$product_id][desc]; ?>
</td>
<td>
<?php echo $master_products_list[$product_id][price]; ?>
</td>
<td>
<form action="change_quant.php" method="post">
<input type="hidden" name="id" value="<?php echo $i; ?>">
<input type="text" size="3" name="quantity"
value="<?php echo $quantity; ?>">
</td>
<td>
<input type="submit" value="数量更改">
</form>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="2" align="right">
<b>合计: </b>
</td>
<td colspan="2">
rmb:<?php echo $total; ?>
</td>
<td> </td>
</tr>
</table>
<br>
<br>
<?php
}
?><h2>商店待出售的商品</h2>
<br>
<i>
我们提供以下商品待售:
</i>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
<th>
商品名称
</th>
<th>
商品说明
</th>
<th>
单价
</th>
<th>
</th>
</tr>
<?php
// show all of the products
foreach ($master_products_list as $product_id => $item) {
?>
<tr>
<td>
<?php echo $item[name]; ?>
</td>
<td>
<?php echo $item[desc]; ?>
</td>
<td>
$<?php echo $item[price]; ?>
</td>
<td>
<a href="add_item.php?id=<?php echo $product_id; ?>">
添加至购物篮
</a>
</td>
</tr>
<?php
}?>
</table>
购物车
<?php
//
// change_quant.php:
// change the quantity of an item in the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
session_register('cart');
}// typecast to int, making sure we access the
// right element below
$i = (int)$_post[id];// save the old number of products for display
// and arithmetic
$old_num = $_session[cart][products][$i][1];if ($_post[quantity]) {
$_session[cart][products][$i][1] = $_post[quantity]; //change the quantity
} else {
unset($_session[cart][products][$i]); // send the product into oblivion
}// update the number of items
$_session[cart][num_items] = ($old_num >$_post[quantity]) ?
$_session[cart][num_items] - ($old_num-$_post[quantity]) :
$_session[cart][num_items] + ($_post[quantity]-$old_num);
?><html>
<head>
<title>
数量修改
</title>
</head>
<body>
<h1> 将数量: <?php echo $old_num; ?> 更改为
<?php echo $_post[quantity]; ?></h1>
<a href="cart.php">返回</a> 商品列表页面.
</body>
</html>
打开文件
本款教程主要是讲对数据库操作的php代码范例,有php操作mysql数据连接,以及删除数据,查询数据 修改数据,修改更新记录等实例。<?php
//物品数组
$master_products_list = array();
//载入物品数据函数
function loadproducts() {
global $master_products_list;
$filename = 'products.txt';$fp = @fopen($filename, "r")
or die("打开 $filename 文件失败");
@flock($fp, 1)
or die("锁定 $filename 文件失败");//读取文件内容
while ($line = fgets($fp, 1024)) {
list($id, $name, $desc, $price) = explode('|', $line); //读取每行数据,数据以| 格开
$id = trim($id); //去掉首尾特殊符号
$master_products_list[$id] = array("name" => $name, //名称
"desc" => $desc, //说明
"price" => $price); //单价
}@fclose($fp) //关闭文件
or die("关闭 $filename 文件失败");
}
?>
简单查询数据
代码如下 | 复制代码 |
<?php //显示用户列表
while($row=mysql_fetch_array($result)){ echo "<br><br><br><a href=query.php>继续查询</a>"; |
删除数据
代码如下 | 复制代码 |
<?php $result=mysql_query("select * from userinfo");
while($row=mysql_fetch_array($result)){ }//显示列表的内容 $sql="delete from userinfo where id=".$_get[id]; }//else($id部分) |
修改,更新记录
代码如下 | 复制代码 |
<?php $result=mysql_query("select * from userinfo");
while($row=mysql_fetch_array($result)){ }//显示列表的内容 |
保存记录到数据库
代码如下 | 复制代码 |
<?php |
用php教程实现连接服务器,还有实现连接imap服务器,最后通过php编写的代码来实现上传和下载文件
代码如下 | 复制代码 |
<?php |
邮件发送函数mail
代码如下 | 复制代码 |
<?php mail( "163@111cn.net", "欢迎你", "hello,你好! " ); ?> |
代码如下 | 复制代码 |
<?php //连接imap服务器 $mbox = imap_open("{imap.example.org}", "username", "password", op_halfopen) or die("连接失败: " . imap_last_error()); $list = imap_getmailboxes($mbox, "{imap.example.org}", "*"); if (is_array($list)) { foreach ($list as $key => $val) { echo "($key) "; echo imap_utf7_decode($val->name) . ","; echo "'" . $val->delimiter . "',"; echo $val->attributes . "<br />n"; } } else { echo "imap_getmailboxes 失败: " . imap_last_error() . "n"; } //关闭imap连接 imap_close($mbox); ?> |
连接ftp服务器
代码如下 | 复制代码 |
<?php // 连接ftp服务器 // 上传文件 // 关闭ftp连接 |
ftp文件上传下载功能
代码如下 | 复制代码 |
<?php $file = 'somefile.txt'; $remote_file = 'readme.txt'; // 连接ftp服务器 $conn_id = ftp_connect($ftp_server); //使用用户名、密码登陆 $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); //上传文件 if (ftp_put($conn_id, $remote_file, $file, ftp_ascii)) { echo "成功上传 $file 文件n"; } else { echo "上传 $file 文件失败n"; } // 关闭ftp连接 ftp_close($conn_id); ?> |
ftp删除文件
代码如下 | 复制代码 |
<?php $file = 'public_html/old.txt'; // 连接ftp服务器 $conn_id = ftp_connect($ftp_server); // 验证用户名和密码 $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); // 删除指定文件 if (ftp_delete($conn_id, $file)) { echo "$file 文件删除成功 n"; } else { echo "删除 $file 文件失败n"; } // 关闭ftp连接 ftp_close($conn_id); ?> |
ftp获取远程文件大小
代码如下 | 复制代码 |
<?php $file = 'somefile.txt'; // 连接ftp服务器 $conn_id = ftp_connect($ftp_server); //验证用户名和密码 $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); //获取指定文件的大小 $res = ftp_size($conn_id, $file); if ($res != -1) { echo " $file 文件大小为 $res字节"; } else { echo "获取远程文件大小失败"; } //关闭ftp连接 ftp_close($conn_id); ?> |
对文件访问实现的源代码。主要讲到php教程操作文件以及xml文档实例方法。
<?php
// 创建一个新的pdf文档句柄
$pdf = cpdf_open(0);
// 打开一个文件
cpdf_open_file($pdf);
// 开始一个新页面(a4)
cpdf_begin_page($pdf, 595, 842);
// 得到并使用字体对象
$arial = cpdf_findfont($pdf, "arial", "host", 1);
cpdf_setfont($pdf, $arial, 10);
// 输出文字
cpdf_show_xy($pdf, "this is an exam of pdf documents, it is a good lib,",50, 750);
cpdf_show_xy($pdf, "if you like,please try yourself!", 50, 730);
// 结束一页
cpdf_end_page($pdf);
// 关闭并保存文件
cpdf_close($pdf);// 如果要直接输出到客户端的话,把下面的代码加上
$buf = cpdf_get_buffer($pdf);
$len = strlen($buf);
header("content-type: application/pdf");
header("content-length: $len");
header("content-disposition: inline; filename=pie_php.pdf");
print $buf;
cpdf_delete($pdf);
?>
xml文件
<?php
$dom = new domdocument();
$dom->load("order.xsl");
$proc = new xsltprocessor;
$xsl = $proc->importstylesheet($dom);$xml = new domdocument();
$xml->load('order.xml');
$string = $proc->transformtoxml($xml);
echo $string;
?>
order.sls
<?xml version="1.0" encoding='gb2312' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">
<xsl:output encoding='gb2312'/><xsl:param name="column" select="'sku'"/>
<xsl:param name="order" select="'ascending'"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="order">
<xsl:with-param name="sortcolumn" select="$column" />
<xsl:with-param name="sortorder" select="$order" />
</xsl:apply-templates>
</body>
</html>
</xsl:template><xsl:template match="order">
<xsl:param name="sortcolumn" />
<xsl:param name="sortorder" />
<table border="1">
<tr>
<th>订单号</th>
<th>id</th>
<th>说明</th>
<th>价格</th>
<th>数量</th>
<th>合计</th>
</tr>
<xsl:apply-templates select="item">
</xsl:apply-templates>
</table>
</xsl:template>
<xsl:template match="item">
<tr>
<td><xsl:value-of select="../account" /></td>
<td><xsl:value-of select="sku" /></td>
<td><xsl:value-of select="description" /></td>
<td><xsl:value-of select="priceper" /></td>
<td><xsl:value-of select="quantity" /></td>
<td><xsl:value-of select="subtotal" /></td>
</tr>
</xsl:template>
</xsl:stylesheet>
order.xml文件
<?xml version="1.0" encoding='gb2312' ?>
<order>
<account>9900234</account>
<item id="1">
<sku>1234</sku>
<priceper>5.95</priceper>
<quantity>100</quantity>
<subtotal>595.00</subtotal>
<description>www.111cn.net</description>
</item>
<item id="2">
<sku>6234</sku>
<priceper>22.00</priceper>
<quantity>10</quantity>
<subtotal>220.00</subtotal>
<description>足球</description>
</item>
<item id="3">
<sku>9982</sku>
<priceper>2.50</priceper>
<quantity>1000</quantity>
<subtotal>2500.00</subtotal>
<description>手机包</description>
</item>
<item id="4">
<sku>3256</sku>
<priceper>389.00</priceper>
<quantity>1</quantity>
<subtotal>389.00</subtotal>
<description>手机</description>
</item>
<numberitems>www.111cn.net</numberitems>
<total>3704.00</total>
<orderdate>07/07/2002</orderdate>
<ordernumber>8876</ordernumber>
</order>
相关文章
使用PHP+JavaScript将HTML页面转换为图片的实例分享
这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19- php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 这篇文章算是超级入门级别的了,我们下面来给各位介绍在photoshop画斜线/直线/虚线的教程了,希望下面这篇文章给你入门来帮助。 PS怎么画斜线 选择铅笔工具,或者画笔...2016-09-14
- 下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13- 这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
- Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
- 这篇文章主要给大家介绍的是关于Lua语言新手入门的简单教程,文中通过示例代码一步步介绍的非常详细,对各位新手们的入门提供了一个很方便的教程,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。...2020-06-30
- 图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
- 共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
- 这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
- PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
- ps软件是现在很多人比较喜欢的,有着非常不错的使用效果,这次文章就给大家介绍下ps怎么制作图片阴影效果,还不知道制作方法的赶紧来看看。 ps图片阴影效果怎么做方法/...2017-07-06
C#中图片旋转和翻转(RotateFlipType)用法分析
这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了OpenCV如何去除图片中的阴影的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
- 这篇文章主要介绍了JavaScript 如何禁止用户保存图片,帮助大家完成需求,更好的理解和使用JavaScript,感兴趣的朋友可以了解下...2020-11-19