在上载php时重命名图像文件
我有一个上传图片的表格。index.html将数据提交到resizer.php。编码如下 index.html在上载php时重命名图像文件,php,image-processing,file-upload,upload,file-rename,Php,Image Processing,File Upload,Upload,File Rename,我有一个上传图片的表格。index.html将数据提交到resizer.php。编码如下 index.html <form action="resizer.php" method="post" enctype="multipart/form-data"> Image: <input type="file" name="file" /> <input type="submit" name="submit" value="upload" /> &l
<form action="resizer.php" method="post" enctype="multipart/form-data">
Image: <input type="file" name="file" />
<input type="submit" name="submit" value="upload" />
</form>
图片:
resizer.php
<?php
require_once('imageresizer.class.php');
$imagename = "myimagename";
//Path To Upload Directory
$dirpath = "uploaded/";
//MAX WIDTH AND HEIGHT OF IMAGE
$max_height = 100;
$max_width = 100;
//Create Image Control Object - Parameters(file name, file tmp name, file type, directory path)
$resizer = new ImageResizer($_FILES['file']['name'],$_FILES['file']['tmp_name'],$dirpath);
//RESIZE IMAGE - Parameteres(max height, max width)
$resizer->resizeImage($max_height,$max_width);
//Display Image
$resizer->showResizedImage();
?>
<?php
class ImageResizer{
public $file_name;
public $tmp_name;
public $dir_path;
//Set variables
public function __construct($file_name,$tmp_name,$dir_path){
$this->file_name = $file_name;
$this->tmp_name = $tmp_name;
$this->dir_path = $dir_path;
$this->getImageInfo();
$this->moveImage();
}
//Move the uploaded image to the new directory and rename
public function moveImage(){
if(!is_dir($this->dir_path)){
mkdir($this->dir_path,0777,true);
}
if(move_uploaded_file($this->tmp_name,$this->dir_path.$this->file_name)){
$this->setFileName($this->dir_path.$this->file_name);
}
}
//Define the new filename
public function setFileName($file_name){
$this->file_name = $file_name;
return $this->file_name;
}
//Resize the image function with new max height and width
public function resizeImage($max_height,$max_width){
$this->max_height = $max_height;
$this->max_width = $max_width;
if($this->height > $this->width){
$ratio = $this->height / $this->max_height;
$new_height = $this->max_height;
$new_width = ($this->width / $ratio);
}
elseif($this->height < $this->width){
$ratio = ($this->width / $this->max_width);
$new_width = $this->max_width;
$new_height = ($this->height / $ratio);
}
else{
$new_width = $this->max_width;
$new_height = $this->max_height;
}
$thumb = imagecreatetruecolor($new_width, $new_height);
switch($this->file_type){
case 1:
$image = imagecreatefromgif($this->file_name);
break;
case 2:
$image = imagecreatefromjpeg($this->file_name);
break;
case 3:
$image = imagecreatefrompng($this->file_name);
break;
case 4:
$image = imagecreatefromwbmp($this->file_name);
}
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $this->width, $this->height);
switch($this->file_type){
case 1:
imagegif($thumb,$this->file_name);
break;
case 2:
imagejpeg($thumb,$this->file_name,100);
break;
case 3:
imagepng($thumb,$this->file_name,0);
break;
case 4:
imagewbmp($thumb,$this->file_name);
}
imagedestroy($image);
imagedestroy($thumb);
}
public function getImageInfo(){
list($width, $height, $type) = getimagesize($this->tmp_name);
$this->width = $width;
$this->height = $height;
$this->file_type = $type;
}
public function showResizedImage(){
echo "<img src='".$this->file_name." />";
}
public function onSuccess(){
header("location: index.php");
}
}
?>
imageresizer.class.php
<?php
require_once('imageresizer.class.php');
$imagename = "myimagename";
//Path To Upload Directory
$dirpath = "uploaded/";
//MAX WIDTH AND HEIGHT OF IMAGE
$max_height = 100;
$max_width = 100;
//Create Image Control Object - Parameters(file name, file tmp name, file type, directory path)
$resizer = new ImageResizer($_FILES['file']['name'],$_FILES['file']['tmp_name'],$dirpath);
//RESIZE IMAGE - Parameteres(max height, max width)
$resizer->resizeImage($max_height,$max_width);
//Display Image
$resizer->showResizedImage();
?>
<?php
class ImageResizer{
public $file_name;
public $tmp_name;
public $dir_path;
//Set variables
public function __construct($file_name,$tmp_name,$dir_path){
$this->file_name = $file_name;
$this->tmp_name = $tmp_name;
$this->dir_path = $dir_path;
$this->getImageInfo();
$this->moveImage();
}
//Move the uploaded image to the new directory and rename
public function moveImage(){
if(!is_dir($this->dir_path)){
mkdir($this->dir_path,0777,true);
}
if(move_uploaded_file($this->tmp_name,$this->dir_path.$this->file_name)){
$this->setFileName($this->dir_path.$this->file_name);
}
}
//Define the new filename
public function setFileName($file_name){
$this->file_name = $file_name;
return $this->file_name;
}
//Resize the image function with new max height and width
public function resizeImage($max_height,$max_width){
$this->max_height = $max_height;
$this->max_width = $max_width;
if($this->height > $this->width){
$ratio = $this->height / $this->max_height;
$new_height = $this->max_height;
$new_width = ($this->width / $ratio);
}
elseif($this->height < $this->width){
$ratio = ($this->width / $this->max_width);
$new_width = $this->max_width;
$new_height = ($this->height / $ratio);
}
else{
$new_width = $this->max_width;
$new_height = $this->max_height;
}
$thumb = imagecreatetruecolor($new_width, $new_height);
switch($this->file_type){
case 1:
$image = imagecreatefromgif($this->file_name);
break;
case 2:
$image = imagecreatefromjpeg($this->file_name);
break;
case 3:
$image = imagecreatefrompng($this->file_name);
break;
case 4:
$image = imagecreatefromwbmp($this->file_name);
}
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $this->width, $this->height);
switch($this->file_type){
case 1:
imagegif($thumb,$this->file_name);
break;
case 2:
imagejpeg($thumb,$this->file_name,100);
break;
case 3:
imagepng($thumb,$this->file_name,0);
break;
case 4:
imagewbmp($thumb,$this->file_name);
}
imagedestroy($image);
imagedestroy($thumb);
}
public function getImageInfo(){
list($width, $height, $type) = getimagesize($this->tmp_name);
$this->width = $width;
$this->height = $height;
$this->file_type = $type;
}
public function showResizedImage(){
echo "<img src='".$this->file_name." />";
}
public function onSuccess(){
header("location: index.php");
}
}
?>
试试这个:
public function moveImage(){
global $imagename;
if(!is_dir($this->dir_path)){
mkdir($this->dir_path,0777,true);
}
if(move_uploaded_file($this->tmp_name,$this->dir_path.$imagename)){
$fileParts = explode('.', $this->tmp_name);
$this->setFileName($this->dir_path.$imagename.'.'.$fileParts[count($fileParts)-1]);
}
}
编辑:为您添加了扩展;-)
class ImageResizer{
public $file_name;
public $tmp_name;
public $dir_path;
//Set variables
public function __construct($file_name,$tmp_name,$dir_path){
$this->file_name =$foto = $dir.rand(00,999).date("-Y-m-d_H-i-s").$file_name;
$this->tmp_name = $tmp_name;
$this->dir_path = $dir_path;
$this->getImageInfo();
$this->moveImage();
$sql="insert into foto (foto) values ('".$this->file_name."')";
mysql_query($sql);
}
//Move the uploaded image to the new directory and rename
public function moveImage(){
if(!is_dir($this->dir_path)){
mkdir($this->dir_path,0777,true);
}
if(move_uploaded_file($this->tmp_name,$this->dir_path.$this->file_name)){
$this->setFileName($this->dir_path.$this->file_name);
rename($this->file_name, $imagename );
}
}
//Define the new filename
public function setFileName($file_name){
$this->file_name = $file_name;
return $this->file_name;
}
//Resize the image function with new max height and width
public function resizeImage($max_height,$max_width){
$this->max_height = $max_height;
$this->max_width = $max_width;
if($this->height > $this->width){
$ratio = $this->height / $this->max_height;
$new_height = $this->max_height;
$new_width = ($this->width / $ratio);
}
elseif($this->height < $this->width){
$ratio = ($this->width / $this->max_width);
$new_width = $this->max_width;
$new_height = ($this->height / $ratio);
}
else{
$new_width = $this->max_width;
$new_height = $this->max_height;
}
$thumb = imagecreatetruecolor($new_width, $new_height);
switch($this->file_type){
case 1:
$image = imagecreatefromgif($this->file_name);
break;
case 2:
$image = imagecreatefromjpeg($this->file_name);
break;
case 3:
$image = imagecreatefrompng($this->file_name);
break;
case 4:
$image = imagecreatefromwbmp($this->file_name);
}
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $this->width, $this->height);
switch($this->file_type){
case 1:
imagegif($thumb,$this->file_name);
break;
case 2:
imagejpeg($thumb,$this->file_name,100);
break;
case 3:
imagepng($thumb,$this->file_name,0);
break;
case 4:
imagewbmp($thumb,$this->file_name);
}
imagedestroy($image);
imagedestroy($thumb);
}
public function getImageInfo(){
list($width, $height, $type) = getimagesize($this->tmp_name);
$this->width = $width;
$this->height = $height;
$this->file_type = $type;
}
public function showResizedImage(){
echo "<img src='".$this->file_name." />";
}
public function onSuccess(){
header("location: index.php");
}
}
类图像大小调整器{
公共$文件名;
公共$tmp_名称;
公共$dir_path;
//设置变量
公共函数构造($file\u name、$tmp\u name、$dir\u path){
$this->file\u name=$foto=$dir.rand(00999).date(“-Y-m-d\u H-i-s”)。$file\u name;
$this->tmp\u name=$tmp\u name;
$this->dir\u path=$dir\u path;
$this->getImageInfo();
$this->moveImage();
$sql=“插入foto(foto)值(“$this->file_name.”);
mysql_查询($sql);
}
//将上载的图像移动到新目录并重命名
公共功能图像(){
如果(!is_dir($this->dir_path)){
mkdir($this->dir\u path,0777,true);
}
如果(移动上传的文件($this->tmp\u name,$this->dir\u path.$this->file\u name)){
$this->setFileName($this->dir\u path.$this->file\u name);
重命名($this->file\u name,$imagename);
}
}
//定义新文件名
公共函数setFileName($file\u name){
$this->file\u name=$file\u name;
返回$this->file\u name;
}
//使用新的最大高度和宽度调整图像功能的大小
公共函数大小图像($max\u height,$max\u width){
$this->max\u height=$max\u height;
$this->max\u width=$max\u width;
如果($this->height>$this->width){
$ratio=$this->height/$this->max\u height;
$new\u height=$this->max\u height;
$new_width=($this->width/$ratio);
}
elseif($this->height<$this->width){
$ratio=($this->width/$this->max_width);
$new\u width=$this->max\u width;
$new_height=($this->height/$ratio);
}
否则{
$new\u width=$this->max\u width;
$new\u height=$this->max\u height;
}
$thumb=ImageCreateTureColor($new\u宽度,$new\u高度);
开关($this->file\u type){
案例1:
$image=imagecreatefromformgif($this->file\u name);
打破
案例2:
$image=imagecreatefromjpeg($this->file\u name);
打破
案例3:
$image=imagecreatefrompng($this->file\u name);
打破
案例4:
$image=imagecreatefromwbmp($this->file\u name);
}
imagecopyresampled($thumb,$image,0,0,0,0,$new\u width,$new\u height,$this->width,$this->height);
开关($this->file\u type){
案例1:
imagegif($thumb,$this->file\u name);
打破
案例2:
imagejpeg($thumb,$this->file\u name,100);
打破
案例3:
imagepng($thumb,$this->file\u name,0);
打破
案例4:
imagewbmp($thumb,$this->file\u name);
}
图像销毁($图像);
(拇指);
}
公共函数getImageInfo(){
列表($width,$height,$type)=getimagesize($this->tmp_name);
$this->width=$width;
$this->height=$height;
$this->file_type=$type;
}
公共函数showResizedImage(){
回显“文件名”。/>;
}
公共函数onSuccess(){
标题(“location:index.php”);
}
}
因此,您甚至不能用另一个变量替换此代码中的一个变量?可能您需要先花一些时间来理解此代码?是的..此上载文件,但没有扩展名不应提及扩展名,因为它还支持其他格式..出于好奇,您为什么评论自己的答案有效?它是如果你能描述一下最初的问题是什么,以及你是如何解决的,那就更好了,这样其他人就可以利用这篇文章来帮助他们解决类似的问题。