问题描述
我有一个上传图片的表单。 index.html将数据提交给resizer.php。编码如下:
index.html
< form action =resizer.phpmethod =postenctype =multipart / form-data>
Image:< input type =filename =file/>
< input type =submitname =submitvalue =upload/>
< / form>
resizer.php
<?php
require_once('imageresizer.class.php');
$ imagename =myimagename;
//上传目录的路径
$ dirpath =uploaded /;
//最大宽度和图像高度
$ max_height = 100;
$ max_width = 100;
//创建图像控制对象 - 参数(文件名,文件tmp名称,文件类型,目录路径)
$ resizer = new ImageResizer($ _ FILES ['file'] ['name '],$ _ FILES [' 文件 '] [' tmp_name的值'],$ dirpath);
//调整大小图像 - 参数(最大高度,最大宽度)
$ resizer-> resizeImage($ max_height,$ max_width);
//显示图片
$ resizer-> showResizedImage();
?>
imageresizer.class.php
<?php
class ImageResizer {
public $ file_name;
public $ tmp_name;
public $ dir_path;
$ b //设置变量
公共函数__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();
}
//将上传的图片移动到新目录中,并将
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 $这 - > FILE_NAME);
$ b //定义新文件名
public function setFileName($ file_name){
$ this-> file_name = $ file_name;
返回$ this-> file_name;
}
//用新的最大高度和宽度调整图像函数的大小
public function resizeImage($ max_height,$ max_width){
$ this-> max_height = $ max_height;
$ this-> max_width = $ max_width;
$ b $ 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;
案例2:
$ image = imagecreatefromjpeg($ this-> file_name);
break;
案例3:
$ image = imagecreatefrompng($ this-> file_name);
break;
案例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;
情况2:
imagejpeg($ thumb,$ this-> file_name,100);
break;
案例3:
imagepng($ thumb,$ this-> file_name,0);
break;
案例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;
$ b public function showResizedImage(){
echo< img src ='。$ this-> file_name。/>;
$ b public function onSuccess(){
header(location:index.php);
}
}
?>
一切正常。文件上传到原来的文件名中。但是我想把文件重命名为myimagename,这是resizer.php中的一个变量。我怎么能这样做?
预先感谢...:)
blasteralfred
试试这个:
public函数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]);
$ b 编辑: strong>为您添加了扩展名; - )
I have a form for uploading image. The index.html submits data to resizer.php. The coding is as follows;
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" />
</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();
?>
imageresizer.class.php
<?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");
}
}
?>
Everything is working well.. The file get uploaded in it's original filename. But I want to rename the file to "myimagename", which is a variable in resizer.php. How can i make this possible??
Thanks in advance... :)
blasteralfred
解决方案 Try this:
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]);
}
}
Edit: Added the extension for you ;-)
这篇关于重命名上传php的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!