当前位置: 编程技术>php
本页文章导读:
▪php点击链接下载图片或其它类型文件的实例代码 点击链接即下载图片或其它类型的文件,代码如下:
例1,
<?php
//点击链接下载图片
//by www.
$filename = $_GET['filename'];
header("Content-type: application/octet-stream");
header("Content-Length: ".filesize(.........
▪php 缩略图类(附调用示例) 分享个php缩略图类,可以实现如下的功能:
1,支持加载图片文件和加载图片字符串
2,可以将缩略图输出到浏览器和保持缩略图文件
3,支持gif,png,jpeg类型的缩略
4,可以设定是否按.........
▪php下载远程文件的类(支持断点续传) 为大家提供一个不错的php下载远程文件的类,代码太长就不贴出来了。
这里给出下载地址:
php下载远程文件的类
此类的功能:
支持断点续传的下载,可以计算传输率,可以控制传输率。
.........
[1]php点击链接下载图片或其它类型文件的实例代码
来源: 互联网 发布时间: 2013-12-24
点击链接即下载图片或其它类型的文件,代码如下:
例1,
<?php
//点击链接下载图片
//by www.
$filename = $_GET['filename'];
header("Content-type: application/octet-stream");
header("Content-Length: ".filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
$fp = fopen($filename, 'rb');
fpassthru($fp);
fclose($fp);
?>
例2,下载某一类型的文件。
代码:
<?php
//直接输出一个 PDF 文件
header('Content-type: application/pdf');
//提示下载 PDF 文件 downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
//original.pdf 的源文件
readfile('original.pdf');
?>
说明:
如果仅是将文件输出到标准输出,可以使用 readfile()函数,比fopen()要好。
[2]php 缩略图类(附调用示例)
来源: 互联网 发布时间: 2013-12-24
分享个php缩略图类,可以实现如下的功能:
1,支持加载图片文件和加载图片字符串
2,可以将缩略图输出到浏览器和保持缩略图文件
3,支持gif,png,jpeg类型的缩略
4,可以设定是否按比例来拉伸
2,可以将缩略图输出到浏览器和保持缩略图文件
3,支持gif,png,jpeg类型的缩略
4,可以设定是否按比例来拉伸
代码如下:
<?php
/**
* 生成缩略图(支持加载图片文件和字符串2种方式)
* @param $maxWidth 缩略图最大宽度
* @param $maxHeight 缩略图最大高度
* @param bool $scale 是否按比例缩小,否则拉伸
* @param bool $inflate 是否放大以来填充缩略图
* @edit by www.
*/
class Thumbnail {
private $maxWidth;
private $maxHeight;
private $scale;
private $inflate;
private $types;
private $imgLoaders;
private $imgCreators;
private $source;
private $sourceWidth;
private $sourceHeight;
private $sourceMime;
private $thumb;
private $thumbWidth;
private $thumbHeight;
public function __construct($maxWidth, $maxHeight, $scale = true, $inflate = true) {
$this->maxWidth = $maxWidth;
$this->maxHeight = $maxHeight;
$this->scale = $scale;
$this->inflate = $inflate;
$this->types = array(
'image/jpeg',
'image/png',
'image/gif'
);
//加载MIME类型图像的函数名称
$this->imgLoaders = array(
'image/jpeg' => 'imagecreatefromjpeg',
'image/png' => 'imagecreatefrompng',
'image/gif' => 'imagecreatefromgif'
);
//储存创建MIME类型图片的函数名称
$this->imgCreators = array(
'image/jpeg' => 'imagejpeg',
'image/png' => 'imagepng',
'image/gif' => 'imagegif'
);
}
/**
* 文件方式加载图片
* @param string $image 源图片
* @return bool
*/
public function loadFile($image){
if(!$dims = @getimagesize($image)){
trigger_error("源图片不存在");
}
if(in_array($dims['mime'], $this->types)){
$loader = $this->imgLoaders[$dims['mime']];
$this->source = $loader($image);
$this->sourceWidth = $dims[0];
$this->sourceHeight = $dims[1];
$this->sourceMime = $dims['mime'];
$this->initThumb();
return TRUE;
}else{
trigger_error('不支持'.$dims['mime']."图片类型");
}
}
/**
* 字符串方式加载图片
* @param string $image 字符串
* @param string $mime 图片类型
* @return type
*/
public function loadData($image,$mime){
if(in_array($mime, $this->types)){
if($this->source = @imagecreatefromstring($image)){
$this->sourceWidth = imagesx($this->source);
$this->sourceHeight = imagesy($this->source);
$this->sourceMime = $mime;
$this->initThumb();
return TRUE;
}else{
trigger_error("不能从字符串加载图片");
}
}else{
trigger_error("不支持".$mime."图片格式");
}
}
/**
* 生成缩略图
* @param string $file 文件名。如果不为空则储存为文件,否则直接输出到浏览器
*/
public function buildThumb($file = null){
$creator = $this->imgCreators[$this->sourceMime];
if(isset($file)){
return $creator($this->thumb,$file);
}else{
return $creator($this->thumb);
}
}
/**
* 处理缩放
*/
public function initThumb(){
if($this->scale){
if($this->sourceWidth > $this->sourceHeight){
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = floor($this->sourceHeight*($this->maxWidth/$this->sourceWidth));
}elseif($this->sourceWidth < $this->sourceHeight){
$this->thumbHeight = $this->maxHeight;
$this->thumbWidth = floor($this->sourceWidth*($this->maxHeight/$this->sourceHeight));
}else{
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = $this->maxHeight;
}
}
$this->thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
if($this->sourceWidth <= $this->maxWidth && $this->sourceHeight <= $this->maxHeight && $this->inflate == FALSE){
$this->thumb = $this->source;
}else{
imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0, $this->thumbWidth, $this->thumbHeight,
$this->sourceWidth, $this->sourceHeight);
}
}
public function getMine(){
return $this->sourceMime;
}
public function getThumbWidth(){
return $this->thumbWidth;
}
public function getThumbHeight(){
return $this->thumbHeight;
}
}
/**
* 缩略图类调用示例(文件)
*/
$thumb = new Thumbnail(200, 200);
$thumb->loadFile('wap.gif');
header('Content-Type:'.$thumb->getMine());
$thumb->buildThumb();
/**
* 缩略图类调用示例(字符串)
*/
$thumb = new Thumbnail(200, 200);
$image = file_get_contents('wap.gif');
$thumb->loadData($image, 'image/jpeg');
$thumb->buildThumb('wap_thumb.gif');
?>
[3]php下载远程文件的类(支持断点续传)
来源: 互联网 发布时间: 2013-12-24
为大家提供一个不错的php下载远程文件的类,代码太长就不贴出来了。
这里给出下载地址:
php下载远程文件的类
此类的功能:
支持断点续传的下载,可以计算传输率,可以控制传输率。
调用示例:
<?php
/**
* 下载远程文件
* edit by www.
*/
//载入类
require_once("download.class.php");
//创建类的实例
$object = new httpdownload();
$object->set_byfile($file); //服务器文件名,包括路径
$object->filename = $filename; //下载另存为的文件名
$object->download();
?>最新技术文章: