Rename image file on upload php
Posted
by
blasteralfred
on Stack Overflow
See other posts from Stack Overflow
or by blasteralfred
Published on 2011-01-06T10:49:44Z
Indexed on
2011/01/06
10:53 UTC
Read the original article
Hit count: 362
Hi, I have a form which uploads and re sizes image. The html file file submits data to a php file. The script 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 image will be uploaded in it's original filename and extension with a "_" prefix. But i want to rename the image to "myimagename" on upload, which is a variable in "Resizer.php". How can i make this possible??
Thanks in advance :)
blasteralfred
© Stack Overflow or respective owner