I have a script that resizes photos submitted by users of my site that works very poorly. The script contains a class that resizes an uploaded image and saves it to a directory on the web server. It does what it is supposed to do but the photo quality is terrible and the ratio on the photos is not consistant. For design reasons I want the photos to all end up the same size (consistant height and width) so the page looks good. Any ideas on how I could adjust the script below to accomplish consistant sizes and better quality?
============== Current Code ==========================
===
NOTE: A form posts to this script with a file field called "imgfile" as a $_POST variable:
class thumbnail
{
var $img;
function thumbnail($imgfile)
{
//detect image format
$this->img["format"]=ereg_
replace(".
*\.(.*)$",
"\\1",$img
file);
$this->img["format"]=strto
upper($thi
s->img["fo
rmat"]);
if ($this->img["format"]=="JP
G" || $this->img["format"]=="JPE
G") {
//JPEG
$this->img["format"]="JPEG
";
$this->img["src"] = ImageCreateFromJPEG ($imgfile);
} elseif ($this->img["format"]=="PN
G") {
//PNG
$this->img["format"]="PNG"
;
$this->img["src"] = ImageCreateFromPNG ($imgfile);
} elseif ($this->img["format"]=="GI
F") {
//GIF
$this->img["format"]="GIF"
;
$this->img["src"] = ImageCreateFromGIF ($imgfile);
} elseif ($this->img["format"]=="WB
MP") {
//WBMP
$this->img["format"]="WBMP
";
$this->img["src"] = ImageCreateFromWBMP ($imgfile);
} else {
//DEFAULT
echo "Not Supported File";
exit();
}
@$this->img["lebar"] = imagesx($this->img["src"])
;
@$this->img["tinggi"] = imagesy($this->img["src"])
;
//default quality jpeg
$this->img["quality"]=100;
}
function size_height($size=100)
{
//height
$this->img["tinggi_thumb"]
=$size;
@$this->img["lebar_thumb"]
= ($this->img["tinggi_thumb"
]/$this->i
mg["tinggi
"])*$this-
>img["leba
r"];
}
function size_width($size=100)
{
//width
$this->img["lebar_thumb"]=
$size;
@$this->img["tinggi_thumb"
] = ($this->img["lebar_thumb"]
/$this->im
g["lebar"]
)*$this->i
mg["tinggi
"];
}
function size_auto($size=100)
{
//size
if ($this->img["lebar"]>=$thi
s->img["ti
nggi"]) {
$this->img["lebar_thumb"]=
$size;
@$this->img["tinggi_thumb"
] = ($this->img["lebar_thumb"]
/$this->im
g["lebar"]
)*$this->i
mg["tinggi
"];
} else {
$this->img["tinggi_thumb"]
=$size;
@$this->img["lebar_thumb"]
= ($this->img["tinggi_thumb"
]/$this->i
mg["tinggi
"])*$this-
>img["leba
r"];
}
}
function jpeg_quality($quality=75)
{
//jpeg quality
$this->img["quality"]=$qua
lity;
}
function show()
{
//show thumb
@Header("Content-Type: image/".$this->img["format
"]);
/* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
$this->img["des"] = ImageCreateTrueColor($this
->img["leb
ar_thumb"]
,$this->im
g["tinggi_
thumb"]);
@imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"],
$this->img["tinggi_thumb"]
, $this->img["lebar"], $this->img["tinggi"]);
if ($this->img["format"]=="JP
G" || $this->img["format"]=="JPE
G") {
//JPEG
imageJPEG($this->img["des"
],"",$this
->img["qua
lity"]);
} elseif ($this->img["format"]=="PN
G") {
//PNG
imagePNG($this->img["des"]
);
} elseif ($this->img["format"]=="GI
F") {
//GIF
imageGIF($this->img["des"]
);
} elseif ($this->img["format"]=="WB
MP") {
//WBMP
imageWBMP($this->img["des"
]);
}
}
function save($save="")
{
//save thumb
if (empty($save)) $save=strtolower("./thumb.
".$this->i
mg["format
"]);
/* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
$this->img["des"] = ImageCreateTrueColor($this
->img["leb
ar_thumb"]
,$this->im
g["tinggi_
thumb"]);
@imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"],
$this->img["tinggi_thumb"]
, $this->img["lebar"], $this->img["tinggi"]);
if ($this->img["format"]=="JP
G" || $this->img["format"]=="JPE
G") {
//JPEG
imageJPEG($this->img["des"
],"$save",
$this->img
["quality"
]);
} elseif ($this->img["format"]=="PN
G") {
//PNG
imagePNG($this->img["des"]
,"$save");
} elseif ($this->img["format"]=="GI
F") {
//GIF
imageGIF($this->img["des"]
,"$save");
} elseif ($this->img["format"]=="WB
MP") {
//WBMP
imageWBMP($this->img["des"
],"$save")
;
}
}
}
$tm1 = $_FILES['file1']['tmp_name
'];
$tm2 = $_FILES['file2']['tmp_name
'];
$tm3 = $_FILES['file3']['tmp_name
'];
if($tm1 != ""){
move_uploaded_file("$tm1" , '/www/website.com/userpics
/'.$record
id.'_'.'1'
.'.'.'jpg'
);
$thumb=new thumbnail('/www/website.co
m/userpics
/'.$record
id.'_'.'1'
.'.'.'jpg'
);
$thumb->size_width(90);
$thumb->size_height(96);
$thumb->jpeg_quality(100);
$thumb->save('/www/website
.com/outpu
tpics/'.$r
ecordid.'_
'.'1'.'.'.
'jpg');
$thumb=new thumbnail('/www/website.co
m/userpics
/'.$record
id.'_'.'1'
.'.'.'jpg'
);
$thumb->size_width(178);
$thumb->size_height(192);
$thumb->jpeg_quality(100);
$thumb->save('/www/website
.com/listv
iewpics/'.
$adid.'_'.
'1'.'.'.'j
pg');
}
Any ideas are appreciated....
Start Free Trial