Note that there are some explanatory texts on larger screens.

plurals
  1. PORename image file on upload php
    primarykey
    data
    text
    <p>I have a form for uploading image. The index.html submits data to resizer.php. The coding is as follows;</p> <p><strong>index.html</strong></p> <pre><code>&lt;form action="resizer.php" method="post" enctype="multipart/form-data"&gt; Image: &lt;input type="file" name="file" /&gt; &lt;input type="submit" name="submit" value="upload" /&gt; &lt;/form&gt; </code></pre> <p><strong>resizer.php</strong></p> <pre><code>&lt;?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-&gt;resizeImage($max_height,$max_width); //Display Image $resizer-&gt;showResizedImage(); ?&gt; </code></pre> <p><strong>imageresizer.class.php</strong></p> <pre><code>&lt;?php class ImageResizer{ public $file_name; public $tmp_name; public $dir_path; //Set variables public function __construct($file_name,$tmp_name,$dir_path){ $this-&gt;file_name = $file_name; $this-&gt;tmp_name = $tmp_name; $this-&gt;dir_path = $dir_path; $this-&gt;getImageInfo(); $this-&gt;moveImage(); } //Move the uploaded image to the new directory and rename public function moveImage(){ if(!is_dir($this-&gt;dir_path)){ mkdir($this-&gt;dir_path,0777,true); } if(move_uploaded_file($this-&gt;tmp_name,$this-&gt;dir_path.$this-&gt;file_name)){ $this-&gt;setFileName($this-&gt;dir_path.$this-&gt;file_name); } } //Define the new filename public function setFileName($file_name){ $this-&gt;file_name = $file_name; return $this-&gt;file_name; } //Resize the image function with new max height and width public function resizeImage($max_height,$max_width){ $this-&gt;max_height = $max_height; $this-&gt;max_width = $max_width; if($this-&gt;height &gt; $this-&gt;width){ $ratio = $this-&gt;height / $this-&gt;max_height; $new_height = $this-&gt;max_height; $new_width = ($this-&gt;width / $ratio); } elseif($this-&gt;height &lt; $this-&gt;width){ $ratio = ($this-&gt;width / $this-&gt;max_width); $new_width = $this-&gt;max_width; $new_height = ($this-&gt;height / $ratio); } else{ $new_width = $this-&gt;max_width; $new_height = $this-&gt;max_height; } $thumb = imagecreatetruecolor($new_width, $new_height); switch($this-&gt;file_type){ case 1: $image = imagecreatefromgif($this-&gt;file_name); break; case 2: $image = imagecreatefromjpeg($this-&gt;file_name); break; case 3: $image = imagecreatefrompng($this-&gt;file_name); break; case 4: $image = imagecreatefromwbmp($this-&gt;file_name); } imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $this-&gt;width, $this-&gt;height); switch($this-&gt;file_type){ case 1: imagegif($thumb,$this-&gt;file_name); break; case 2: imagejpeg($thumb,$this-&gt;file_name,100); break; case 3: imagepng($thumb,$this-&gt;file_name,0); break; case 4: imagewbmp($thumb,$this-&gt;file_name); } imagedestroy($image); imagedestroy($thumb); } public function getImageInfo(){ list($width, $height, $type) = getimagesize($this-&gt;tmp_name); $this-&gt;width = $width; $this-&gt;height = $height; $this-&gt;file_type = $type; } public function showResizedImage(){ echo "&lt;img src='".$this-&gt;file_name." /&gt;"; } public function onSuccess(){ header("location: index.php"); } } ?&gt; </code></pre> <p>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??</p> <p>Thanks in advance... :)</p> <p>blasteralfred</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload