cachedir=$path."/cache/"; if (!file_exists($file)) { $this->errmsg = "file doesn't exists $file"; $this->error = true; } else if (!is_readable($file)) { $this->errmsg = "file is not readable"; $this->error = true; } if (strstr(strtolower($file), ".gif")) $this->format = "GIF"; else if (strstr(strtolower($file), ".jpg") || strstr(strtolower($file), ".jpeg")) $this->format = "JPEG"; else if (strstr(strtolower($file), ".png")) $this->format = "PNG"; else { $this->errmsg = "unknown file format"; $this->error = true; } if ($max_width == 0 && $max_height == 0 && $percent == 0) $percent = 100; $this->max_width = $max_width; $this->max_height = $max_height; $this->percent = $percent; $this->file = $file; $this->path = $path; $this->name = $name; $this->profile = $profile; } function _width($width, $height) { $new_width = $this->max_width; $new_wp = (100 * $new_width) / $width; $new_height = ($height * $new_wp) / 100; return array($new_width, $new_height); } function _height($width, $height) { $new_height = $this->max_height; $new_hp = (100 * $new_height) / $height; $new_width = ($width * $new_hp) / 100; return array($new_width, $new_height); } function _percent($width, $height) { $new_width = ($width * $this->percent) / 100; $new_height = ($height * $this->percent) / 100; return array($new_width, $new_height); } function return_value($array) { $array[0] = intval($array[0]); $array[1] = intval($array[1]); return $array; } function _image_size($width, $height) { $new_size = array($width, $height); if ($this->max_width > 0) { $new_size = $this->_width($width, $height); if ($this->max_height > 0) if ($new_size[1] > $this->max_height) $new_size = $this->_height($new_size[0], $new_size[1]); return $this->return_value($new_size); } if ($this->max_height > 0) { $new_size = $this->_height($width, $height); return $this->return_value($new_size); } if ($this->percent > 0) { $new_size = $this->_percent($width, $height); return $this->return_value($new_size); } } function _error() { header("Content-type: image/jpeg"); $img=ImageCreate(600, 23); $bg_color=ImageColorAllocate($img, 0, 0, 0); $color=ImageColorAllocate($img, 255, 255, 255); ImageString($img, 3, 5, 5, $this->errmsg, $color); ImageJpeg($img); ImageDestroy($img); } function getImage($name="") { if ($this->error) { $this->_error(); return; } # Setzten des Standard-Cache-Verzeichnis, wenn kein Verzeichnis # definiert wurde. $dir=explode("iwii.thumbnail.php",__FILE__); if (!$this->cachedir) $this->cachedir=$dir[0]."cache/"; if ($this->profile && file_exists($dir[0]."profiles/".$this->profile.".inc")) include_once($dir[0]."profiles/".$this->profile.".inc"); # Validierung vom $sharpening if ($this->sharpening=="auto") $this->sharpening="auto"; elseif ($this->sharpening>=1) $this->sharpening=1; elseif ($this->sharpening>0 && $this->sharpening<1) {} else $this->sharpening=0; # wenn Cache verwendet werden soll: if ($this->cache=="auto") { # Dateinamen für den Cache erstellen # Cachename setzt sich aus dem Path, Dateinanen und den Übergabe- # parametern zusammen. $this->cachename=$this->max_width."_".$this->max_height."_".$this->percent."_".$this->sharpening."_".$this->bgcolor."_".$this->color."_".$this->padding."_".$this->border."_".str_replace("/","_",$this->path)."_".$this->name; } # Prüfen, ob der Cache verwendet werden soll und wenn ja, # ob ein Bild für den Cache generiert werden soll oder das # Bild nur ausgelesen werden muss. if ($this->cache=="auto") { if (file_exists($this->cachedir.$this->cachename)) { # wenn Thumbnail jünger als die Originaldatei ist # das Thumbnail ausliefern und die Ausführung beenden. if (filemtime($this->cachedir.$this->cachename) >= filemtime($this->file)) { header("Content-type: image/".strtolower($this->format)); $output = fopen ($this->cachedir.$this->cachename, "rb"); fpassthru ($output); fclose ($output); return; } } } $size= GetImageSize($this->file); $new_size= $this->_image_size($size[0], $size[1]); if (function_exists("ImageCreateTrueColor")) $new_image = ImageCreateTrueColor($new_size[0], $new_size[1]); else $new_image = ImageCreate($new_size[0], $new_size[1]); switch ($this->format) { case "GIF": $old_image = ImageCreateFromGif($this->file); break; case "JPEG": $old_image = ImageCreateFromJpeg($this->file); break; case "PNG": $old_image = ImageCreateFromPng($this->file); break; } ImageCopyResampled($new_image, $old_image, 0, 0, 0, 0, $new_size[0], $new_size[1], $size[0], $size[1]); # Berechnen des automatischen Schärfefaktors if ($this->sharpening) { switch ($this->sharpening) { case "auto": # Faktor für die Breite $sw=100-(($new_size[0]*100)/$size[0]); # Faktor für Höhe $sh=100-(($new_size[1]*100)/$size[1]); # Mittelwert berechnen $this->sharpening=number_format((($sw+$sh)/200),2); break; default: /* Bereinigen von sharpening */ if (($this->sharpening>0) && ($this->sharpening<=1)) {} else $this->sharpening=0; break; } } # Schärfen durchführen if ($this->sharpening>0) { # Code aus http://www.php.net/manual/de/ref.image.php#51052 $pix=array(); $width=$new_size[0]; $height=$new_size[1]; for ($hc=0; $hc<$height; ++$hc) { for ($wc=0; $wc<$width; ++$wc) { $rgb = ImageColorAt($new_image, $wc, $hc); $pix[$hc][$wc][0]= $rgb >> 16; $pix[$hc][$wc][1]= $rgb >> 8 & 255; $pix[$hc][$wc][2]= $rgb & 255; } } $height--; $width--; for ($hc=1; $hc<$height; ++$hc) { $r5=$pix[$hc][0][0]; $g5=$pix[$hc][0][1]; $b5=$pix[$hc][0][2]; $hcc=$hc-1; for ($wc=1; $wc<$width; ++$wc) { $r=-($pix[$hcc][$wc][0]); $g=-($pix[$hcc][$wc][1]); $b=-($pix[$hcc][$wc][2]); $r-=$r5+$r5; $g-=$g5+$g5; $b-=$b5+$b5; $r5=$pix[$hc][$wc][0]; $g5=$pix[$hc][$wc][1]; $b5=$pix[$hc][$wc][2]; $r+=$r5*5; $g+=$g5*5; $b+=$b5*5; $r*=.5; $g*=.5; $b*=.5; $r=(($r-$r5)*$this->sharpening)+$r5; $g=(($g-$g5)*$this->sharpening)+$g5; $b=(($b-$b5)*$this->sharpening)+$b5; if ($r<0) $r=0; elseif ($r>255) $r=255; if ($g<0) $g=0; elseif ($g>255) $g=255; if ($b<0) $b=0; elseif ($b>255) $b=255; imagesetpixel($new_image,$wc,$hc,($r << 16)|($g << 8)|$b); } } imageinterlace($new_image,1); } # Zielgröße des Bildes bestimmen $tmp_width=$this->max_width; $tmp_height=$this->max_height; if (!$this->max_width) $tmp_width=$new_size[0]; if (!$this->max_height) $tmp_height=$new_size[1]; if ($this->padding) { $tmp_width+=(2*$this->padding); $tmp_height+=(2*$this->padding); } if ($this->border && $this->color) { $tmp_width+=(2*$this->border); $tmp_height+=(2*$this->border); } # Prüfen, ob Bildgröße angepasst werden muss # Die errechnete Göße muss sich vom verkleinerten Bild # unterscheiden und eine Hintergrundfarbe muss definiert sein. if ($tmp_width!=$new_size[0] || $tmp_height!=$new_size[1]) { # Bild in der neuen Endgröße erstellen $tmp_image=imagecreatetruecolor($tmp_width, $tmp_height); # Hintergrundfarbe definieren $bgcolor =ImageColorAllocate ($tmp_image, hexdec(substr($this->bgcolor,0,2)), hexdec(substr($this->bgcolor,2,2)), hexdec(substr($this->bgcolor,4,2))); # Randfarbe definieren $bordercolor =ImageColorAllocate ($tmp_image, hexdec(substr($this->color,0,2)), hexdec(substr($this->color,2,2)), hexdec(substr($this->color,4,2))); # Rahmen zeichnen ImageFilledRectangle ($tmp_image, 0, 0, $tmp_width, $tmp_height, $bordercolor); # Bild mit Hintergrundfarbe einfärben ImageFilledRectangle ($tmp_image, $this->border, $this->border, ($tmp_width-($this->border+1)), ($tmp_height-($this->border+1)), $bgcolor); # Startposition des eingefügten Bildes berechnen $x=floor(($tmp_width-$new_size[0])/2); $y=floor(($tmp_height-$new_size[1])/2); # altes Vorschaubild in neues kopieren ImageCopy ($tmp_image, $new_image, $x, $y, 0, 0, $new_size[0], $new_size[1]); ImageDestroy($new_image); $new_image=imagecreatetruecolor($tmp_width, $tmp_height); ImageCopy ($new_image, $tmp_image, 0, 0, 0, 0, $tmp_width, $tmp_height); ImageDestroy($tmp_image); } # Bilder auf die Festplatte schreiben, wenn Cache aktiv ist. if ($this->cache=="auto") { switch ($this->format) { case "GIF": ImageGif($new_image, $this->cachedir.$this->cachename); break; case "JPEG": ImageJpeg($new_image, $this->cachedir.$this->cachename,$this->jpegquality); break; case "PNG": ImagePng($new_image, $this->cachedir.$this->cachename); break; } # Bild von der Festplatte lesen und ausliefern. header("Content-type: image/".strtolower($this->format)); $output = fopen ($this->cachedir.$this->cachename, "rb"); fpassthru ($output); fclose ($output); # Speicher bereinigen ImageDestroy($new_image); ImageDestroy($old_image); return; } # Ausgabe von nicht gecachten Bildern header("Content-type: image/".strtolower($this->format)); call_user_func("image".$this->format, $new_image); # Speicher bereinigen ImageDestroy($new_image); ImageDestroy($old_image); return; } function getImageCacheName($name="") { if ($this->error) { $this->_error(); return; } # Setzten des Standard-Cache-Verzeichnis, wenn kein Verzeichnis # definiert wurde. $dir=explode("iwii.thumbnail.php",__FILE__); if (!$this->cachedir) $this->cachedir=$dir[0]."cache/"; if ($this->profile && file_exists($dir[0]."profiles/".$this->profile.".inc")) include_once($dir[0]."profiles/".$this->profile.".inc"); # Validierung vom $sharpening if ($this->sharpening=="auto") $this->sharpening="auto"; elseif ($this->sharpening>=1) $this->sharpening=1; elseif ($this->sharpening>0 && $this->sharpening<1) {} else $this->sharpening=0; # wenn Cache verwendet werden soll: if ($this->cache=="auto") { # Dateinamen für den Cache erstellen # Cachename setzt sich aus dem Path, Dateinanen und den Übergabe- # parametern zusammen. $this->cachename=$this->name; } # Prüfen, ob der Cache verwendet werden soll und wenn ja, # ob ein Bild für den Cache generiert werden soll oder das # Bild nur ausgelesen werden muss. if ($this->cache=="auto") { if (file_exists($this->cachedir.$this->cachename)) { # wenn Thumbnail jünger als die Originaldatei ist # das Thumbnail ausliefern und die Ausführung beenden. if (filemtime($this->cachedir.$this->cachename) >= filemtime($this->file)) { return $this->cachedir.$this->cachename; } } } $size= GetImageSize($this->file); $new_size= $this->_image_size($size[0], $size[1]); if (function_exists("ImageCreateTrueColor")) $new_image = ImageCreateTrueColor($new_size[0], $new_size[1]); else $new_image = ImageCreate($new_size[0], $new_size[1]); switch ($this->format) { case "GIF": $old_image = ImageCreateFromGif($this->file); break; case "JPEG": $old_image = ImageCreateFromJpeg($this->file); break; case "PNG": $old_image = ImageCreateFromPng($this->file); break; } ImageCopyResampled($new_image, $old_image, 0, 0, 0, 0, $new_size[0], $new_size[1], $size[0], $size[1]); # Berechnen des automatischen Schärfefaktors if ($this->sharpening) { switch ($this->sharpening) { case "auto": # Faktor für die Breite $sw=100-(($new_size[0]*100)/$size[0]); # Faktor für Höhe $sh=100-(($new_size[1]*100)/$size[1]); # Mittelwert berechnen $this->sharpening=number_format((($sw+$sh)/200),2); break; default: /* Bereinigen von sharpening */ if (($this->sharpening>0) && ($this->sharpening<=1)) {} else $this->sharpening=0; break; } } # Schärfen durchführen if ($this->sharpening>0) { # Code aus http://www.php.net/manual/de/ref.image.php#51052 $pix=array(); $width=$new_size[0]; $height=$new_size[1]; for ($hc=0; $hc<$height; ++$hc) { for ($wc=0; $wc<$width; ++$wc) { $rgb = ImageColorAt($new_image, $wc, $hc); $pix[$hc][$wc][0]= $rgb >> 16; $pix[$hc][$wc][1]= $rgb >> 8 & 255; $pix[$hc][$wc][2]= $rgb & 255; } } $height--; $width--; for ($hc=1; $hc<$height; ++$hc) { $r5=$pix[$hc][0][0]; $g5=$pix[$hc][0][1]; $b5=$pix[$hc][0][2]; $hcc=$hc-1; for ($wc=1; $wc<$width; ++$wc) { $r=-($pix[$hcc][$wc][0]); $g=-($pix[$hcc][$wc][1]); $b=-($pix[$hcc][$wc][2]); $r-=$r5+$r5; $g-=$g5+$g5; $b-=$b5+$b5; $r5=$pix[$hc][$wc][0]; $g5=$pix[$hc][$wc][1]; $b5=$pix[$hc][$wc][2]; $r+=$r5*5; $g+=$g5*5; $b+=$b5*5; $r*=.5; $g*=.5; $b*=.5; $r=(($r-$r5)*$this->sharpening)+$r5; $g=(($g-$g5)*$this->sharpening)+$g5; $b=(($b-$b5)*$this->sharpening)+$b5; if ($r<0) $r=0; elseif ($r>255) $r=255; if ($g<0) $g=0; elseif ($g>255) $g=255; if ($b<0) $b=0; elseif ($b>255) $b=255; imagesetpixel($new_image,$wc,$hc,($r << 16)|($g << 8)|$b); } } imageinterlace($new_image,1); } # Zielgröße des Bildes bestimmen $tmp_width=$this->max_width; $tmp_height=$this->max_height; if (!$this->max_width) $tmp_width=$new_size[0]; if (!$this->max_height) $tmp_height=$new_size[1]; if ($this->padding) { $tmp_width+=(2*$this->padding); $tmp_height+=(2*$this->padding); } if ($this->border && $this->color) { $tmp_width+=(2*$this->border); $tmp_height+=(2*$this->border); } # Prüfen, ob Bildgröße angepasst werden muss # Die errechnete Göße muss sich vom verkleinerten Bild # unterscheiden und eine Hintergrundfarbe muss definiert sein. if ($tmp_width!=$new_size[0] || $tmp_height!=$new_size[1]) { # Bild in der neuen Endgröße erstellen $tmp_image=imagecreatetruecolor($tmp_width, $tmp_height); # Hintergrundfarbe definieren $bgcolor =ImageColorAllocate ($tmp_image, hexdec(substr($this->bgcolor,0,2)), hexdec(substr($this->bgcolor,2,2)), hexdec(substr($this->bgcolor,4,2))); # Randfarbe definieren $bordercolor =ImageColorAllocate ($tmp_image, hexdec(substr($this->color,0,2)), hexdec(substr($this->color,2,2)), hexdec(substr($this->color,4,2))); # Rahmen zeichnen ImageFilledRectangle ($tmp_image, 0, 0, $tmp_width, $tmp_height, $bordercolor); # Bild mit Hintergrundfarbe einfärben ImageFilledRectangle ($tmp_image, $this->border, $this->border, ($tmp_width-($this->border+1)), ($tmp_height-($this->border+1)), $bgcolor); # Startposition des eingefügten Bildes berechnen $x=floor(($tmp_width-$new_size[0])/2); $y=floor(($tmp_height-$new_size[1])/2); # altes Vorschaubild in neues kopieren ImageCopy ($tmp_image, $new_image, $x, $y, 0, 0, $new_size[0], $new_size[1]); ImageDestroy($new_image); $new_image=imagecreatetruecolor($tmp_width, $tmp_height); ImageCopy ($new_image, $tmp_image, 0, 0, 0, 0, $tmp_width, $tmp_height); ImageDestroy($tmp_image); } # Bilder auf die Festplatte schreiben, wenn Cache aktiv ist. if ($this->cache=="auto") { switch ($this->format) { case "GIF": ImageGif($new_image, $this->cachedir.$this->cachename); break; case "JPEG": ImageJpeg($new_image, $this->cachedir.$this->cachename,$this->jpegquality); break; case "PNG": ImagePng($new_image, $this->cachedir.$this->cachename); break; } # Bild von der Festplatte lesen und ausliefern. # Speicher bereinigen ImageDestroy($new_image); ImageDestroy($old_image); return $this->cachedir.$this->cachename; } } } ?>