<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" ><channel><title>PHP Gangsta - Der PHP Blog mit Praxisbezug &#187; gd</title> <atom:link href="http://www.phpgangsta.de/tag/gd/feed" rel="self" type="application/rss+xml" /><link>http://www.phpgangsta.de</link> <description>Ein PHP Blog mit Themen aller Art. Manchmal vergewaltige ich PHP...</description> <lastBuildDate>Thu, 02 Feb 2012 22:22:51 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Thumbnails erstellen mit PHP</title><link>http://www.phpgangsta.de/thumbnails-erstellen-mit-php</link> <comments>http://www.phpgangsta.de/thumbnails-erstellen-mit-php#comments</comments> <pubDate>Sun, 28 Jun 2009 20:36:30 +0000</pubDate> <dc:creator>Michael Kliewe</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[gd]]></category> <category><![CDATA[image]]></category> <category><![CDATA[thumbnail]]></category><guid isPermaLink="false">http://www.phpgangsta.de/?p=115</guid> <description><![CDATA[Da es im Zend Framework keine schöne Klasse für diese Aufgabe gibt (Zend_Image gab es mal als Proposal, wurde aber abgelehnt), muss man anderweitig eine Lösung finden. Ich habe dazu eine uralte Funktion, die aber nach wie vor fabelhaft funktioniert. Man übergibt einfach nur den Pfad zur Quelldatei (häufig ist das eine hochgeladene Datei, es [...]<br/><br/> Keine ähnlichen Artikel.]]></description> <content:encoded><![CDATA[<p>Da es im Zend Framework keine schöne Klasse für diese Aufgabe gibt (<a href="http://framework.zend.com/wiki/display/ZFPROP/Zend_Image+-+Eric+Potvin" target="_blank">Zend_Image</a> gab es mal als Proposal, wurde aber abgelehnt), muss man anderweitig eine Lösung finden.</p><p>Ich habe dazu eine uralte Funktion, die aber nach wie vor fabelhaft funktioniert. Man übergibt einfach nur den Pfad zur Quelldatei (häufig ist das eine hochgeladene Datei, es kann sowohl ein jpg als auch ein gif sein), ein Zielverzeichnis (häufig ein Ordner im webroot), sowie Informationen über die Zieldatei: Dateiname, Endung, maximale Höhe und Breite.</p><p>Ein Beispiel würde so aussehen:</p><pre class="brush: php; title: ; notranslate">Util::createThumbnailAndSave($_FILES['uploadfile']['tmp_name'], 'images/uploads', $_FILES['uploadfile']['name'], 'jpg', 250, 250)</pre><p>Danach schreibt man die Informationen über die Datei natürlich noch in eine Datenbank, damit man sie auf der Webseite einbinden kann.</p><p>Und natürlich möchte ich euch meine kleine Funktion nicht vorenthalten. Es wird kein <a href="http://de.php.net/manual/en/book.imagick.php" target="_blank">Imagick</a> benötigt, sondern nur eine &#8220;normale&#8221; PHP-Installation incl. aktivierten <a href="http://de.php.net/manual/en/ref.image.php" target="_blank">GD-Funktionen</a>.</p><pre class="brush: php; title: ; notranslate">public static function createThumbnailAndSave($filePath, $targetDir, $targetFilename, $targetExtension, $targetWidth, $targetHeight)
{
	$imageinfo = getimagesize($filePath);

	// check if source image is smaller than target dimensions
	if ($imageinfo[0] &gt; $targetWidth || $imageinfo[1] &gt; $targetHeight)
	{
		if ($imageinfo[2] == 1) {       // GIF
			$srcImg = imagecreatefromgif($filePath);
		}
		elseif ($imageinfo[2] == 2) {   // JPG
			$srcImg = imagecreatefromjpeg($filePath);
		}
		$widthDivisor = $imageinfo[0] / $targetWidth;
		$heightDivisor = $imageinfo[1] / $targetHeight;
		if ($widthDivisor &gt; $heightDivisor)
		{
			$dstImg = imagecreatetruecolor($targetWidth, $imageinfo[1] / $widthDivisor);
		}
		else
		{
			$dstImg = imagecreatetruecolor($imageinfo[0] / $heightDivisor, $targetHeight);
		}

		imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, imagesx($dstImg), imagesy($dstImg), imagesx($srcImg), imagesy($srcImg));

		// Save to harddisc and delete from RAM
		imagejpeg($dstImg, $targetDir . &quot;/&quot; . $targetFilename . &quot;.&quot; . $targetExtension, 95);
		$fsize = filesize($targetDir . &quot;/&quot; . $targetFilename . &quot;.&quot; . $targetExtension);
		if ($fsize &gt; 50000)
		{
			imagejpeg($dstImg, $targetDir . &quot;/&quot; . $targetFilename . &quot;.&quot; . $targetExtension, 60);
		}
		imagedestroy($dstImg);
		imagedestroy($srcImg);
	}
	else {
		copy($filePath, $targetDir . &quot;/&quot; . $targetFilename . &quot;.&quot; . $targetExtension);
	}
	chmod($targetDir . &quot;/&quot; . $targetFilename . &quot;.&quot; . $targetExtension, 0777);
}</pre><p>Eigentlich könnte ich sie mal auf Aktualität überprüfen, heutzutage würde ich sie wahrscheinlich auch etwas anders schreiben. Aber für meine paar kleinen privaten Homepages reicht es. Alternativen sind natürlich fertige Klassen von <a href="http://www.phpclasses.org">phpclasses.org</a> etc,</p> <br/><br/><p>Keine ähnlichen Artikel.</p>]]></content:encoded> <wfw:commentRss>http://www.phpgangsta.de/thumbnails-erstellen-mit-php/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: basic
Database Caching 1/10 queries in 0.025 seconds using disk: basic
Object Caching 519/532 objects using disk: basic

Served from: www.phpgangsta.de @ 2012-02-07 23:51:24 -->
