Hi all,
i am working with Uploadify ( a neat little file uploader ) and had a tough time finding an image re-sizer to work with it. I finally found one that works quite well but it encodes the file name using md5 encoding.
I am not sure where and how to grab the md5 encoded filename and query an INSERT into sql with it.
Here is the code:
if (!empty($_FILES)) {
class Image {
var $uploaddir;
var $quality = 80;
var $ext;
var $dst_r;
var $img_r;
var $img_w;
var $img_h;
var $output;
var $data;
var $datathumb;
function setFile($src = null) {
$this->ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION));
if(is_file($src) && ($this->ext == "JPG" OR $this->ext == "JPEG")) {
$this->img_r = ImageCreateFromJPEG($src);
} elseif(is_file($src) && $this->ext == "PNG") {
$this->img_r = ImageCreateFromPNG($src);
} elseif(is_file($src) && $this->ext == "GIF") {
$this->img_r = ImageCreateFromGIF($src);
}
$this->img_w = imagesx($this->img_r);
$this->img_h = imagesy($this->img_r);
}
function resize($w = 100) {
$h = $this->img_h / ($this->img_w / $w);
$this->dst_r = ImageCreateTrueColor($w, $h);
imagecopyresampled($this->dst_r, $this->img_r, 0, 0, 0, 0, $w, $h, $this->img_w, $this->img_h);
$this->img_r = $this->dst_r;
$this->img_h = $h;
$this->img_w = $w;
}
function createFile($output_filename = null) {
if($this->ext == "JPG" OR $this->ext == "JPEG") {
imageJPEG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext, $this->quality);
} elseif($this->ext == "PNG") {
imagePNG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
} elseif($this->ext == "GIF") {
imageGIF($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
}
$this->output = $this->uploaddir.$output_filename.'.'.$this->ext;
}
function setUploadDir($dirname) {
$this->uploaddir = $dirname;
}
function flush() {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
imagedestroy($this->dst_r);
unlink($targetFile);
//imagedestroy($this->img_r);
}
}
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
mkdir(str_replace('//','/',$targetPath), 0755, true);
move_uploaded_file ($tempFile, $targetFile);
$image = new Image();
$image->setFile($targetFile);
$image->setUploadDir($targetPath);
$image->resize(640);
$image->createFile(md5($tempFile));
$image->resize(100);
$image->createFile("small".md5($tempFile));
$image->flush();
}
Also, why encode an image filename? Is this to avoid any malicious uploads to the server?
Any help on this would be greatly appreciated.