Dokumentation REDAXO

R3 /

Aktionen: Aktion Resize - Thumbnail generieren

>> Aktionsübersicht

Beispiel 2: Aktion Resize - Thumbnail generieren

Ein anderes Beispiel für Aktionen. Hier wird aus einem Bild, welches über ein Modul ausgewählt wurde, ein Thumbnail generiert und gespeichert. Dafür muss zunächst ein Ordner für die Thumbnails erstellt werden in welchem die Bilder gespeichert werden. In diesem Fall werden nur JPG s übernommen.

Beim Erstellen der Aktion bitte "pre" auswählen und "add" sowie "edit", damit vor jeder Speicherung bei Hinzufügen und Editieren des Modul die Generierung anläuft.

In diesem Fall wirde ein Ordner "/files/thumbs115" benötigt. Die Kompressum wird über $imgcomp eingestellt.

<?

function resizeJPGImage($id,$forcedwidth, $forcedheight,
 	$sourcefile, $destfile, $imgcomp = 10) 
{
	global $REX,$REX_ACTION;

	// ----- dateinamen check
	if ($REX_ACTION['FILE'][$id] == "") return false;
	$len = strlen($REX_ACTION[FILE][$id]);
	$ext = substr($REX_ACTION[FILE][$id],$len-4,4);
	if ($ext != ".jpg")
	{
		$REX_ACTION['MSG'] .= "Leider ist Datei $id kein JPG.<br>";
		return false;
	}

	// ----- resize
	$g_imgcomp = 100 - $imgcomp;
	if (file_exists($sourcefile))
	{
		$g_is = getimagesize($sourcefile);
		$width = $g_is[0];
		$height = $g_is[1];



		if (($width / $height) >= ($forcedwidth / $forcedheight)) 
		{
			$g_iw = $forcedwidth;
			$g_ih = ($forcedwidth / $width) * $height;
		} else {
			$g_ih = $forcedheight;
			$g_iw = ($forcedheight / $height) * $width;
		}
		// sanity check: don't resize images which are smaller than thumbs
		if ($width < $forcedwidth && $height < $forcedheight) 
		{
			$g_iw = $width;
			$g_ih = $height;
		}

		$img_src = imagecreatefromjpeg($sourcefile);
		$img_dst = imagecreatetruecolor($g_iw, $g_ih);
		imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $width, $height);

		// ----- linie zeichnen
		$points[] = 0;
		$points[] = 0;
		$points[] = $g_iw-1;
		$points[] = 0;
		$points[] = $g_iw-1;
		$points[] = $g_ih-1;
		$points[] = 0;
		$points[] = $g_ih-1;
		$points[] = 0;
		$points[] = 0;
		$color = imagecolorallocate ($img_dst, 217, 235, 145 );
		imagepolygon ($img_dst, $points, round(count($points)/2), $color );
		// ----- / linie zeichnen

		imagejpeg($img_dst, $destfile, $g_imgcomp);
		imagedestroy($img_dst);
		imagedestroy($img_src);

		return true;
	}else
	{
		$REX_ACTION[MSG] .= "Datei $id ist nicht gefunden worden<br>";

		return false;
	}
}

if (!resizeJPGImage(1,115, 80, $REX['INCLUDE_PATH']."/../../files/".$REX_ACTION['FILE'][1],
$REX['INCLUDE_PATH']."/../../files/thumbs115/".
$REX_ACTION['FILE'][1], 0)){

	$REX_ACTION['FILE'][1] = "";
	// $REX_ACTION['MSG'] = "Datei 1 ist kein JPG.";
}

?>

Jan

Page last modified on 13.06.2006 14:25 Uhr