<?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>Jakk's blog &#187; General</title>
	<atom:link href="http://lab.araigun.com/category/general/feed/" rel="self" type="application/rss+xml" />
	<link>http://lab.araigun.com</link>
	<description>Keep mine in this blog.</description>
	<lastBuildDate>Sun, 07 Jun 2009 13:16:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Image Magick for image processing</title>
		<link>http://lab.araigun.com/2009/03/06/using-image-magick-for-image-processing/</link>
		<comments>http://lab.araigun.com/2009/03/06/using-image-magick-for-image-processing/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 11:17:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[image processing]]></category>

		<guid isPermaLink="false">http://lab.araigun.com/?p=71</guid>
		<description><![CDATA[Image Magick คำนี่ผมไม่ได้สะกดผิดหรอกนะ แต่มันเป็นชื่อ library ที่ใช้สำหรับ image processing เหมือนกับ GD library แต่ผมคิดว่า performance ของ Image Magick ดีกว่าและใช้จัดการ resource อย่างเช่น memory ได้ดีกว่า Image Magick สามารถใช้ได้ทั้ง command line หรือ interface สำหรับภาษาต่างๆเช่น PHP, JAVA, .NET และอื่นๆ ปัจจุบันมีถึง version 6 สามารถ download และหาดู document ได้จาก website ทางการ : http://www.imagemagick.org แต่version ที่ผมจะนำมาใช้เป็นตัวอย่างเป็น version 4 ซึ่งเป็น statically linked  binary (อันนี้ใครรู้วิธี [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Image Magick </strong>คำนี่ผมไม่ได้สะกดผิดหรอกนะ แต่มันเป็นชื่อ library ที่ใช้สำหรับ image processing เหมือนกับ GD library แต่ผมคิดว่า performance ของ Image Magick ดีกว่าและใช้จัดการ resource อย่างเช่น memory ได้ดีกว่า Image Magick สามารถใช้ได้ทั้ง command line หรือ interface สำหรับภาษาต่างๆเช่น PHP, JAVA, .NET และอื่นๆ ปัจจุบันมีถึง version 6 สามารถ download และหาดู document ได้จาก website ทางการ : <a href="http://www.imagemagick.org" target="_blank">http://www.imagemagick.org</a> <span id="more-71"></span>แต่version ที่ผมจะนำมาใช้เป็นตัวอย่างเป็น version 4 ซึ่งเป็น statically linked  binary (อันนี้ใครรู้วิธี compile สอนผมที)<br />
<strong><br />
test resize 440&#215;504 image by Image Magick</strong><br />
<strong>code here:</strong><br />
<code lang="php[lines]"><?php<br />
function resize($img, $w,  $newfilename) {<br />
	$path_exe = "convert $img -resize \"$w\" $newfilename";<br />
	exec($path_exe);<br />
}<br />
$time_start = microtime(true);<br />
$mem_before = memory_get_usage();//this function avaliable in PHP 5.2+</p>
<p>resize('alucard.jpg', 100, 'alucard_0.jpg');</p>
<p>$mem_after = memory_get_peak_usage();//this function avaliable in PHP 5.2+<br />
$time_end = microtime(true);<br />
$time = $time_end - $time_start;<br />
print '<br />
<table>
<tr align="center" valign="middle">
<td ><image src="alucard.jpg"/></td>
<td > before</td>
</tr>
<tr align="center" valign="middle">
<td ><image src="alucard_0.jpg"/></td>
<td > after</td>
</tr>
</table>
<p>';<br />
print "parse time: $time seconds<br/>";<br />
print "memory usage before resize: $mem_before<br/> memory peak while resize: $mem_after <br/> memory usage for resize:".($mem_after-$mem_before);<br />
?></code><br />
<strong>result here:</strong><br />
<img src="http://lab.araigun.com/wp-content/uploads/2009/03/m.png" alt="result" title="result" width="504" height="729" class="alignnone size-full wp-image-75" /></p>
<p><strong>test resize 440×504 image by GD lib</strong><br />
<strong>code here:</strong><br />
<code lang="php[lines]"><?<br />
function resize($img, $w, $newfilename) {</p>
<p> //Get Image size info<br />
 	$imgInfo = getimagesize($img);<br />
	$format = ereg_replace(".*\.(.*)$","\\1",$img);<br />
	$format = strtolower($format);</p>
<p> 	switch ($format) {<br />
		case "gif": 	$im = imagecreatefromgif($img); break;<br />
		case "jpg":<br />
		case "jpeg":	$im = imagecreatefromjpeg($img);  break;<br />
		case "png": 	$im = imagecreatefrompng($img); break;<br />
		default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;<br />
 	}<br />
	//calculate size<br />
	if($imgInfo[0]>=$imgInfo[1]){<br />
		$nWidth = $w;<br />
		$nHeight = ($nWidth/$imgInfo[0])*$imgInfo[1];<br />
	}else{<br />
		$nHeight = $w;<br />
		$nWidth = ($nHeight/$imgInfo[1])*$imgInfo[0];<br />
	}<br />
 	$nWidth = round($nWidth);<br />
 	$nHeight = round($nHeight);<br />
	$newImg = imagecreatetruecolor($nWidth, $nHeight);</p>
<p> // Check if this image is PNG or GIF, then set if Transparent<br />
	if(($imgInfo[2]==3)){<br />
		imagealphablending($newImg, false);<br />
		imagesavealpha($newImg,true);<br />
		$transparent = imagecolortransparent($im);<br />
		imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);<br />
	}elseif(($imgInfo[2] == 1)){<br />
		imagetruecolortopalette($newImg, false, 255);<br />
		$idx = imagecolortransparent($im);<br />
		$rgba = imagecolorsforindex($im,idx);<br />
		$transparent = imagecolorallocatealpha($newImg, $rgba['red'], $rgba['green'], $rgba['blue'],$rgba['alpha']);<br />
		imagecolortransparent($newImg, $transparent);<br />
		imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);<br />
	}<br />
 	imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);</p>
<p> //Generate the file, and rename it to $newfilename<br />
	switch ($imgInfo[2]) {<br />
		case 1: imagegif($newImg,$newfilename); break;<br />
		case 2: imagejpeg($newImg,$newfilename);  break;<br />
		case 3: imagepng($newImg,$newfilename); break;<br />
		default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;<br />
	}<br />
	imagedestroy($im);<br />
	return $newfilename;<br />
}<br />
$time_start = microtime(true);<br />
$mem_before = memory_get_usage();//this function avaliable in PHP 5.2+</p>
<p>resize('alucard.jpg', 100, 'alucard_1.jpg');</p>
<p>$mem_after = memory_get_peak_usage();//this function avaliable in PHP 5.2+<br />
$time_end = microtime(true);<br />
$time = $time_end - $time_start;<br />
print '<br />
<table>
<tr align="center" valign="middle">
<td ><image src="alucard.jpg"/></td>
<td > before</td>
</tr>
<tr align="center" valign="middle">
<td ><image src="alucard_1.jpg"/></td>
<td > after</td>
</tr>
</table>
<p>';<br />
print "parse time: $time seconds<br/>";<br />
print "memory usage before resize: $mem_before<br/> memory peak while resize: $mem_after <br/> memory usage for resize:".($mem_after-$mem_before);<br />
?></code><br />
<strong>result here:</strong><img src="http://lab.araigun.com/wp-content/uploads/2009/03/g.png" alt="result" title="result" width="497" height="711" class="alignnone size-full wp-image-77" /></p>
<p><strong>source code including  statically linked Image Magick V4: </strong><a href='http://lab.araigun.com/wp-content/uploads/2009/03/test.zip'>source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lab.araigun.com/2009/03/06/using-image-magick-for-image-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
