-
<?php
-
/**一、使用代码块
-
* code block name.
-
* some description
-
* @version 1.0.2
-
* @tutorial 用于显示相关渠道的二维码,并供用户下载
-
* @author leebaotong
-
*
-
*/
-
/**block name
-
* some description
-
* @tutorial ps:如果页面太乱,可用于规范代码层次;
-
* 这种用法我本见于 javascript,后来灵机一动用在了 PHP 里,竟然也可使用,可能因为同是脚本的原因吧.
-
* 但是在 python 里尝试时失败了,可能因为 python 的缩进要求很严格的原因吧.需要注意的是,这里有作用域的问题,切,切切!!!
-
*/
-
{
-
if (Request::get ( 1 )) {
-
} else {
-
die ( “缺少信息” );
-
}
-
}
-
/**二、用于生成图片下载页:
-
* ps:如果图片较大,最好不要用直接下载的方式,太耗资源,可显示到页面供用户自行保存
-
*/
-
{
-
function createPicToDown($picLocalPath) {
-
header ( “Content-Type:text/html;charset=utf-8” );
-
header ( “Content-type: image/jpeg” );
-
$image = imagecreatefromjpeg ( $picLocalPath );
-
header ( “Content-Disposition: attachment;filename=qr.jpg ” );
-
imagejpeg ( $image );
-
imagedestroy ( $image );
-
}
-
}
-
/**三、对于常用到的变量,或字符串常量,可用定义;这样修改时只修改一即可.
-
*/
-
{
-
define ( ‘DS’, DIRECTORY_SEPARATOR );
-
}
-
/**四、建立文件夹
-
*/
-
{
-
/**
-
* 生成路径.
-
*
-
* @param string $dir
-
* @return boolean
-
*/
-
function directory($dir) {
-
return is_dir ( $dir ) or (directory ( dirname ( $dir ) ) and mkdir ( $dir, 0777 ));
-
}
-
}
-
/**五、处理代分支有不确定因素时,要增加 default 做为默认处理方法
-
*/
-
{
-
$filetype = $_FILES [‘img’] [‘type’];
-
switch ($filetype) {
-
case ‘image/pjpeg’ :
-
case ‘image/jpg’ :
-
case ‘image/jpeg’ :
-
$fileExtAsString = ‘.jpg’;
-
break;
-
case ‘image/gif’ :
-
$fileExtAsString = ‘.gif’;
-
break;
-
default :
-
$fileExtAsString = ‘.gif’;
-
break;
-
}
-
}
-
/**六、上传文件处理
-
*/
-
{
-
/**
-
* 前台表单:
-
*
-
* @tutorial :直接写在视图页面
-
*/
-
{
-
‘<form action=”” method=”post” enctype=”multipart/form-data”>’;
-
‘<input type=”file” name=”img” >’;
-
‘</form>’;
-
}
-
/**
-
* 后台:
-
* @tutorial :过程比较简单,根据项目需要可增删.
-
*/
-
{
-
if (! empty ( $_FILES [“img”] [“name”] )) {
-
/**
-
* 检查上传文件是否在允许上传的类型
-
*/
-
{
-
// 允许上传的文件格式
-
$tp = array (
-
“image/gif” => ‘.gif’,
-
“image/pjpeg” => ‘.jpeg’,
-
“image/jpg” => ‘.jpg’,
-
“image/jpeg” => ‘.jpeg’
-
);
-
// 检查上传文件是否在允许上传的类型
-
if (! array_key_exists ( $_FILES [“img”] [“type”], $tp )) {
-
tusi ( ‘格式不对’ );
-
Redirect::delay_to ( ‘addchannel.html’, 1 );
-
}
-
$filetype = $_FILES [‘img’] [‘type’];
-
$fileExtAsString = in_array ( $filetype, $tp ) ? $tp [$filetype] : ‘.jpg’;
-
}
-
/**
-
* 验证目录
-
*/
-
{
-
$picSubPath = date ( ‘Y’ ) . ‘/’ . date ( ‘m’ ) . ‘/’ . date ( ‘d’ );
-
$picName = date ( ‘Ymdhis’, time () );
-
if (! is_dir ( $picSubPath )) {
-
directory ( $picSubPath );
-
}
-
$tempFileFullPathAsString = $picSubPath . ‘/’ . $picName . $fileExtAsString; // 图片的完整路径
-
}
-
$flag = 1;
-
}
-
// 上传文件检测完成,移至存储路径
-
if ($flag) {
-
$result = move_uploaded_file ( $_FILES [“img”] [“tmp_name”], $tempFileFullPathAsString );
-
} else {
-
// ….错误处理
-
}
-
}
-
}
-
/**七、合并两张图片
-
*
-
*/
-
{
-
/**设置路径相关,预备图片资源及临时文件存储位置
-
*/
-
{
-
$logoUrl = ‘http://img.hb.aicdn.com/317b4fa2476e23235f73a01a07d2727b2a4b3abae2f5-b3Ove2_fw658’;
-
$logoTemp = ‘./tempLogo.jpg’;
-
$bgUrl = ‘http://h.hiphotos.baidu.com/image/pic/item/c83d70cf3bc79f3d3f442939b8a1cd11728b2916.jpg’;
-
$bgTemp = ‘./tempbg.jpg’;
-
$newPic = ‘./new.jpeg’;
-
}
-
/**获取图片资源;如是本地文件,可省略此处.
-
*/
-
{
-
$bgTempContent = file_get_contents ($bgUrl );
-
file_put_contents ( $bgTemp, $bgTempContent );
-
$logoTempContent = file_get_contents ( $logoUrl );
-
file_put_contents ( $logoTemp, $logoTempContent );
-
}
-
/**合成图片
-
*/
-
{
-
if (file_exists ( $bgTemp )) {
-
$QR = imagecreatefromstring ( file_get_contents ( $bgTemp ) );
-
if ($logoTemp !== FALSE) {
-
$logo = imagecreatefromstring ( file_get_contents ( $logoTemp ) );
-
$QR_width = imagesx ( $QR );
-
$QR_height = imagesy ( $QR );
-
$logo_width = imagesx ( $logo );
-
$logo_height = imagesy ( $logo );
-
$logoW = $QR_width / 5;
-
$scale = $logo_width / $logo_qr_width;
-
$logoH = $logo_height / $scale;
-
$fw = ($QR_width – $logo_qr_width) / 2;
-
imagecopyresampled ( $QR, $logo, $from_width, $fw, 0, 0, $logoW, $logoH, $logo_width, $logo_height );
-
}
-
imagepng ( $QR, $newPic ); // 最终将图片输出到文件
-
imagedestroy ( $QR );
-
}
-
}
转载请注明:XAMPP中文组官网 » PHP 代码积累(一)