Yükleme php görüntü dosyasını yeniden adlandırma

0 Cevap php

Ben görüntü yüklemek için bir form var. Index.html resizer.php veri gönderir. Aşağıdaki gibi kodlama olduğu;

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

    }
?>

Her şey iyi çalışıyor .. dosyası orijinal dosya yükledi olsun. Ama resizer.php bir değişkendir "myimagename", dosyayı yeniden adlandırmak istiyorum. Nasıl bu mümkün yapabilirsiniz?

Şimdiden teşekkürler ... :)

blasteralfred

0 Cevap