Det er nok som Jesper siger. Du kan evt. se hvordan jeg har lavet mit resize script. Med det bliver kvaliteten ikke dårligere.
class JaRS{
var $image;
// Constructor
function JaRS($imagefile){
$this->image["imagefile"] = $imagefile;
// Get the extension
$this->image["extension"] = strtolower(end(explode(".", $imagefile)));
// Get image dimensions
list($this->image["width"], $this->image["height"]) = getimagesize($this->image["imagefile"]);
// Sets the new height and width to the orginal height and width
$this->image["new_width"] = $this->image["width"];
$this->image["new_height"] = $this->image["height"];
// Sets default quality - Only for jpg/jpeg
$this->image["quality"] = 75;
// jpg/jpeg
if($this->image["extension"] == "jpg" || $this->image["extension"] == "jpeg" ){
// Try to load the image
$this->image["imagesrc"] = @imagecreatefromjpeg($imagefile);
if(!$this->image["imagesrc"]){
die("Couldn't load the image (" . $imagefile . ") Make sure the file exists, you have the right permissions and that the file is a valid " . $this->image["extension"] . " file.");
}
}
// gif
elseif($this->image["extension"] == "gif"){
$this->image["imagesrc"] = @imagecreatefromgif($imagefile);
if(!$this->image["imagesrc"]){
die("Couldn't load the image (" . $imagefile . ") Make sure the file exists, you have the right permissions and that the file is a valid " . $this->image["extension"] . " file.");
}
}
// png
elseif($this->image["extension"] == "png"){
$this->image["imagesrc"] = @imagecreatefrompng($imagefile);
if(!$this->image["imagesrc"]){
die("Couldn't load the image (" . $imagefile . ") Make sure the file exists, you have the right permissions and that the file is a valid " . $this->image["extension"] . " file.");
}
}
else{
die("The format (" . $this->image["extension"] . ") is not supportet");
}
}
function set_quality($quality=75){
$this->image["quality"] = $quality;
}
function set_width($width, $maxheight=""){
// Calculate the new dimensions
$this->image["new_width"] = $width;
$this->image["new_height"] = ($this->image["new_width"]/$this->image["width"])*$this->image["height"];
// If the new_height exceeds the maxheight
if ($maxheight != ""){
if ($this->image["new_height"] > $maxheight){
$this->image["new_height"] = $maxheight;
$this->image["new_width"] = ($this->image["new_height"]/$this->image["height"])*$this->image["width"];
}
}
}
function set_height($height, $maxwidth=""){
// Calculate the new dimensions
$this->image["new_height"] = $height;
$this->image["new_width"] = ($this->image["new_height"]/$this->image["height"])*$this->image["width"];
// If the new_width exceeds the maxheight
if ($maxwidth != ""){
if ($this->image["new_width"] > $maxwidth){
$this->image["new_width"] = $maxwidth;
$this->image["new_height"] = ($this->image["new_width"]/$this->image["width"])*$this->image["height"];
}
}
}
function generate_image($watermark = ""){
if(!isset($this->image["new_width"]) && !isset($this->image["new_height"])){
return false;
}
// Creates the new image
$this->image["newimage"] = imagecreatetruecolor($this->image["new_width"], $this->image["new_height"]);
imagecopyresampled($this->image["newimage"], $this->image["imagesrc"], 0, 0, 0, 0, $this->image["new_width"], $this->image["new_height"], $this->image["width"], $this->image["height"]);
// Copy a watermark onto the image if watermark is set
if($watermark != ""){
// Get the watermark extension
$watermark_extension = (strtolower(end(explode(".", $watermark))));
// jpg/jpeg
if($watermark_extension == "jpg" || $watermark == "jpeg" ){
$watermark_image = @imagecreatefromjpeg($watermark);
if(!$watermark_image){
die("Couldn't load the image (" . $watermark . ") Make sure the file exists, you have the right permissions and that the file is a valid " . $this->image["extension"] . " file.");
}
}
// gif
elseif($watermark_extension == "gif"){
$watermark_image = @imagecreatefromgif($watermark);
if(!$watermark_image){
die("Couldn't load the image (" . $watermark . ") Make sure the file exists, you have the right permissions and that the file is a valid " . $this->image["extension"] . " file.");
}
}
// png
elseif($watermark_extension == "png"){
$watermark_image = @imagecreatefrompng($watermark);
if(!$watermark_image){
die("Couldn't load the image (" . $watermark . ") Make sure the file exists, you have the right permissions and that the file is a valid " . $this->image["extension"] . " file.");
}
}
else{
die("The format (" . $watermark_extension . ") is not supportet");
}
// Get the dimensions of the watermark
list($watermark_width, $watermark_height) = getimagesize($watermark);
imagecopy($this->image["newimage"], $watermark_image, $this->image["new_width"]-$watermark_width, $this->image["new_height"]-$watermark_height, 0, 0, $watermark_width, $watermark_height);
}
// Return the generated image
return $this->image["newimage"];
}
function show_image($watermark = ""){
if($watermark != ""){
if(!$tmpimage = $this->generate_image($watermark)){
$error = 1;
}
}
elseif($watermark == ""){
if(!$tmpimage = $this->generate_image()){
$error = 1;
}
}
if($error != 1){
// Outputs the image
header("Content-type: image/" . $this->image["extension"]);
if($this->image["extension"] == "jpg" || $this->image["extension"] == "jpeg"){
imagejpeg($tmpimage, NULL, $this->image["quality"]);
}
if($this->image["extension"] == "gif"){
imagegif($tmpimage);
}
if($this->image["extension"] == "png"){
imagepng($tmpimage);
}
}
else{
die("Something weird is happening. You shouldn't really see this");
}
}
function save_image($filename, $watermark = ""){
if($watermark != ""){
if(!$tmpimage = $this->generate_image($watermark)){
$error = 1;
}
}
elseif($watermark == ""){
if(!$tmpimage = $this->generate_image()){
$error = 1;
}
}
if($error != 1){
if($this->image["extension"] == "jpg" || $this->image["extension"] == "jpeg"){
imagejpeg($tmpimage, $filename, $this->image["quality"]);
}
if($this->image["extension"] == "gif"){
imagegif($tmpimage, $filename);
}
if($this->image["extension"] == "png"){
imagepng($tmpimage, $filename);
}
}
else{
die("Something weird is happening. You shouldn't really see this");
}
}
}
?>
Eller det kan hentes her (
http://janc-online.dk/files/JaRS.tar.gz)
Indlæg senest redigeret d. 23.01.2007 11:30 af Bruger #6788