2011-01-06 154 views
2

我有一个上传图像的表单。 index.html将数据提交给resizer.php。编码如下:重命名上传图像文件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" /> 
</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"); 
     } 

    } 
?> 

永远东西运行良好..文件上传到它的原始文件名。但我想将文件重命名为“myimagename”,它是resizer.php中的一个变量。我如何使这可能?

在此先感谢... :)

blasteralfred

+1

因此,你甚至不能用这个代码中的一个变量替换另一个变量?可能你需要花一些时间来首先理解这段代码吗? – 2011-01-06 13:04:05

回答

2

试试这个:

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]); 
     } 

    } 

编辑:为你添加;-)扩展

+0

是啊..这上传文件,但没有扩展 – 2011-01-06 13:10:13

0
//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); 

// Rename 
$resizer->setFileName('new_file_name.jpg'); 

//RESIZE IMAGE - Parameteres(max height, max width) 
$resizer->resizeImage($max_height,$max_width); 
+0

不应该提及扩展名,因为它支持其他格式以及.. – 2011-01-06 13:12:18

0
<form action="<?php echo $_server['php-self']; ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm"> 
     <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" /> 
     <button name="submit" type="submit" class="submitButton">Upload Image</button> 
</form> 
<?php 
     if(isset($_POST['submit'])){ 
      if (isset ($_FILES['new_image'])){ 
       $imagename = $_FILES['new_image']['name']; 
       $source = $_FILES['new_image']['tmp_name']; 
       $target = "./temp/".$imagename; 
       move_uploaded_file($source, $target); 

       $imagepath = $imagename; 
       $save = "./temp/" . $imagepath; //This is the new file you saving 
       $file = "./temp/" . $imagepath; //This is the original file 

       list($width, $height) = getimagesize($file) ; 

       $modwidth = 500; 

       $diff = $width/$modwidth; 

       $modheight = $height/$diff; 
       $tn = imagecreatetruecolor($modwidth, $modheight) ; 
       $image = imagecreatefromjpeg($file) ; 
       imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

       imagejpeg($tn, $save, 100) ; 

       $i=123; 
       $j='.jpg'; 

       $save = "./images/".$i .$imagepath; //This is the new file you saving 
       $file = "./temp/" . $imagepath; //This is the original file 

       list($width, $height) = getimagesize($file) ; 

       $modwidth = 160; 

       $diff = $width/$modwidth; 

       $modheight = $height/$diff; 
       $tn = imagecreatetruecolor($modwidth, $modheight) ; 
       $image = imagecreatefromjpeg($file) ; 
       imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

       imagejpeg($tn, $save, 100) ; 
       unlink($target); //Delete our uploaded file 
      echo "Large image: <img src='./temp/".$imagepath."'><br>"; 
      echo "Small image: <img src='./images/".$i .$imagepath; echo"'>"; 
      echo " <br> Large image path: './temp/".$imagepath; 
      echo " <br> Small image path: './images/".$i .$imagepath; 

      } 
     } 

?> 
+0

这一个工作! – Azim 2013-12-29 03:25:15

+0

出于好奇,你为什么评论你自己的答案有效? – 2013-12-29 03:42:08

0

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"); 
    } 

}