求教个关于PHP图像输出的问题
求达人帮忙修改一下,要把输出的问题修改名字成 原名+日期.jpg,而且要生成在aa目录下现在的代码是直接覆盖掉原文件了
代码如下<?php
$_path = '/pay1.jpg';
$_img = new Image($_path);//$_path为图片文件的路径
$_img->thumb(150, 200);
$_img->out();//图像处理类
class Image {
private $file; //图片地址
private $width; //图片长度
private $height; //图片长度
private $type; //图片类型
private $img; //原图的资源句柄
private $new; //新图的资源句柄//构造方法,初始化
public function __construct($_file) {
$this->file = $_SERVER["DOCUMENT_ROOT"].$_file;
list($this->width, $this->height, $this->type) = getimagesize($this->file);
$this->img = $this->getFromImg($this->file, $this->type);
}
//缩略图(固定长高容器,图像等比例,扩容填充,裁剪)[固定了大小,不失真,不变形]
public function thumb($new_width = 0,$new_height = 0) { if (empty($new_width) && empty($new_height)) {
$new_width = $this->width;
$new_height = $this->height;
} if (!is_numeric($new_width) || !is_numeric($new_height)) {
$new_width = $this->width;
$new_height = $this->height;
} //创建一个容器
$_n_w = $new_width;
$_n_h = $new_height; //创建裁剪点
$_cut_width = 0;
$_cut_height = 0; if ($this->width < $this->height) {
$new_width = ($new_height / $this->height) * $this->width;
} else {
$new_height = ($new_width / $this->width) * $this->height;
}
if ($new_width < $_n_w) { //如果新高度小于新容器高度
$r = $_n_w / $new_width; //按长度求出等比例因子
$new_width *= $r; //扩展填充后的长度
$new_height *= $r; //扩展填充后的高度
$_cut_height = ($new_height - $_n_h) / 2; //求出裁剪点的高度
} if ($new_height < $_n_h) { //如果新高度小于容器高度
$r = $_n_h / $new_height; //按高度求出等比例因子
$new_width *= $r; //扩展填充后的长度
$new_height *= $r; //扩展填充后的高度
$_cut_width = ($new_width - $_n_w) / 2; //求出裁剪点的长度
}
$this->new = imagecreatetruecolor($_n_w,$_n_h);
imagecopyresampled($this->new,$this->img,0,0,$_cut_width,$_cut_height,$new_width,$new_height,$this->width,$this->height);
}//加载图片,各种类型,返回图片的资源句柄
private function getFromImg($_file, $_type) {
switch ($_type) {
case 1 :
$img = imagecreatefromgif($_file);
break;
case 2 :
$img = imagecreatefromjpeg($_file);
break;
case 3 :
$img = imagecreatefrompng($_file);
break;
default:
Tool::alertBack('警告:此图片类型本系统不支持!');
}
return $img;
}//图像输出
public function out() {
imagepng($this->new,$this->file);
imagedestroy($this->img);
imagedestroy($this->new);
}}
?>
求达人帮忙修改一下,要把输出的问题修改名字成 原名+日期.jpg,而且要生成在aa目录下现在的代码是直接覆盖掉原文件了
代码如下<?php
$_path = '/pay1.jpg';
$_img = new Image($_path);//$_path为图片文件的路径
$_img->thumb(150, 200);
$_img->out();//图像处理类
class Image {
private $file; //图片地址
private $width; //图片长度
private $height; //图片长度
private $type; //图片类型
private $img; //原图的资源句柄
private $new; //新图的资源句柄//构造方法,初始化
public function __construct($_file) {
$this->file = $_SERVER["DOCUMENT_ROOT"].$_file;
list($this->width, $this->height, $this->type) = getimagesize($this->file);
$this->img = $this->getFromImg($this->file, $this->type);
}
//缩略图(固定长高容器,图像等比例,扩容填充,裁剪)[固定了大小,不失真,不变形]
public function thumb($new_width = 0,$new_height = 0) { if (empty($new_width) && empty($new_height)) {
$new_width = $this->width;
$new_height = $this->height;
} if (!is_numeric($new_width) || !is_numeric($new_height)) {
$new_width = $this->width;
$new_height = $this->height;
} //创建一个容器
$_n_w = $new_width;
$_n_h = $new_height; //创建裁剪点
$_cut_width = 0;
$_cut_height = 0; if ($this->width < $this->height) {
$new_width = ($new_height / $this->height) * $this->width;
} else {
$new_height = ($new_width / $this->width) * $this->height;
}
if ($new_width < $_n_w) { //如果新高度小于新容器高度
$r = $_n_w / $new_width; //按长度求出等比例因子
$new_width *= $r; //扩展填充后的长度
$new_height *= $r; //扩展填充后的高度
$_cut_height = ($new_height - $_n_h) / 2; //求出裁剪点的高度
} if ($new_height < $_n_h) { //如果新高度小于容器高度
$r = $_n_h / $new_height; //按高度求出等比例因子
$new_width *= $r; //扩展填充后的长度
$new_height *= $r; //扩展填充后的高度
$_cut_width = ($new_width - $_n_w) / 2; //求出裁剪点的长度
}
$this->new = imagecreatetruecolor($_n_w,$_n_h);
imagecopyresampled($this->new,$this->img,0,0,$_cut_width,$_cut_height,$new_width,$new_height,$this->width,$this->height);
}//加载图片,各种类型,返回图片的资源句柄
private function getFromImg($_file, $_type) {
switch ($_type) {
case 1 :
$img = imagecreatefromgif($_file);
break;
case 2 :
$img = imagecreatefromjpeg($_file);
break;
case 3 :
$img = imagecreatefrompng($_file);
break;
default:
Tool::alertBack('警告:此图片类型本系统不支持!');
}
return $img;
}//图像输出
public function out() {
imagepng($this->new,$this->file);
imagedestroy($this->img);
imagedestroy($this->new);
}}
?>
解决方案 »
- 如何通过ip获取域名??
- PHP碰到一个怪问题,做一个排班表的时候,循环显示时间的时候到第五个就多加了一秒!!!!怪
- 谁有flash上传头像的组件?
- 二维数组如何值
- 你提供软件,我负责销售。寻优秀教育相关软件
- 如何在自己的网站实现登陆到另外一个网站和得到那个网站的某些内容。在线等答案。
- 我在Web 开发开发中看到"Coldfusion"
- 怎样在WINDOWS下创建一个名字为 guestbook 的mysql数据库
- 想要下载与php-mysql-4.2.2-8.0.5.i386.rpm相匹配的phpmysqmin的LINUX版本
- 怎么才能给在WIN98下给PHP加入扩展模块如:GIF、PDF?
- php
- iis 创建ftp时 报530错误 iis板块人气不旺
public function out() {
imagepng($this->new,'新文件的路径位置,linux注意权限问题,以及文件是否存在/www/img/b.png');
imagedestroy($this->img);
imagedestroy($this->new);
}
imagepng — 以 PNG 格式将图像输出到浏览器或文件说明bool imagepng ( resource $image [, string $filename ] )
imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器),或者如果用 filename 给出了文件名则将其输出到该文件。<?php
$im = imagecreatefrompng("test.png");
imagepng($im);
?>
参见 imagegif(),imagewbmp(),imagejpeg() 和 imagetypes()。
$name = basename($path,"jpg").time().".jpg";
echo "<img src='{$path}' title='{$name}' />";