<?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>Serbest Çağrışım &#187; image manipulation</title>
	<atom:link href="http://www.serbestcagrisim.com/archives/tag/image-manipulation/feed" rel="self" type="application/rss+xml" />
	<link>http://www.serbestcagrisim.com</link>
	<description>Soldan sağa doğru...</description>
	<lastBuildDate>Fri, 02 Jul 2010 10:23:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Better, faster, stronger.</title>
		<link>http://www.serbestcagrisim.com/archives/299</link>
		<comments>http://www.serbestcagrisim.com/archives/299#comments</comments>
		<pubDate>Tue, 30 Jun 2009 12:13:13 +0000</pubDate>
		<dc:creator>Omer</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[image manipulation]]></category>
		<category><![CDATA[squid]]></category>
		<category><![CDATA[squidguard]]></category>

		<guid isPermaLink="false">http://www.serbestcagrisim.com/?p=299</guid>
		<description><![CDATA[OK, most of you have read my work on Squid and SquidGuard against Facebook. Let&#8217;s make something clear, I have nothing against Facebook. Ok, I don&#8217;t like it much, but what I really don&#8217;t like is people who surf at it in working hours. So this script makes clear that they are being followed. Now, [...]]]></description>
			<content:encoded><![CDATA[<p>OK, most of you have read my work on Squid and SquidGuard against Facebook. Let&#8217;s make something clear, I have nothing against Facebook. Ok, I don&#8217;t like it much, but what I really don&#8217;t like is people who surf at it in working hours. So this script makes clear that they are being followed.</p>
<p>Now, its time to move one step further. Better, faster and a stronger solution for your corporate squid server. ImageMagick library is a nice library, but it remains slow if you have reckless users. We will keep on manipulating images, yes, but in a much faster way. Just keep on reading.</p>
<p><span id="more-299"></span>First lets install GD library for PHP5;</p>
<pre>sudo apt-get install php5-gd</pre>
<p>Yes, you should have a running web server available for this to work. Now lets setup our Squid and squidGuard against facebook. First add these lines to the end of your squid.conf file;</p>
<pre>redirect_program /usr/bin/squidGuard -c /etc/squid/squidGuard.conf
redirect_children 5</pre>
<p>Now make sure your squidGuard.conf file looks something like this;</p>
<pre>#
# CONFIG FILE FOR SQUIDGUARD
#

dbhome /home/squidguard/
logdir /var/log/squid/

src everyone {
        ip      192.168.1.0/255.255.255.0
}

dest facebook {
 expressionlist  facebook
 redirect        http://192.168.1.1/joker.php?url=%u
 log /var/log/squid/joker.log
}

acl {
  everyone within workhours {
    pass !facebook all
  } else {
    pass all
  }

  default {
    pass none
  }
}</pre>
<p>Now let&#8217;s create the file that catches the URL regex. I placed it in /home/squidguard this time, since I backup my whole /home directory everyday. It does not get lost between setups.</p>
<pre>.*facebook\.com.*(\.jpg|\.gif|\.png)
.*fbcdn\.net.*(\.jpg|\.gif|\.png)</pre>
<p>Let&#8217;s create our image manipulation script in our web root. You should name it to joker.php or whatever you want, just keep it in sync with your squidGuard.conf file.</p>
<pre name="code" class="php">&lt;?php
/*
joker.php

This is where we keep our tmp files.
You can set it to /tmp also.
*/
$path = "/var/www/joker";
if (!is_dir($path)) {
   mkdir($path,0755);
}

// Parse the URL line delivered from Squid.
$u = explode(" ",trim($_GET["url"]));
if (count($u) &gt; 0) {
   $url = $u[0];
} else {
   $url = $u;
}

// Temporary file name.
$temp = md5(time().microtime().$url);

$ff = fopen($url, "r");
$contents = stream_get_contents($ff);
fclose($ff);

$fz = fopen($path."/".$temp,"w");
fwrite($fz, $contents);
fclose($fz);

$file = $path."/".$temp;

// U shall not touch below this line.
// ------------------------------- //
// Yeah, that line above this. //
switch (substr($url,-4)) {
   case ".jpg":
      $source = imagecreatefromjpeg($file);
      $header = "image/jpeg";
   break;
   case ".png":
      $source = imagecreatefrompng($file);
      $header = "image/png";
   break;
   case ".gif":
      $source = imagecreatefromgif($file);
      $header = "image/gif";
   break;
}

list($width, $height) = getimagesize($file);
$bwimage= imagecreate($width, $height);

for ($c = 0; $c &lt; 256; $c++) {
   $palette[$c] = imagecolorallocate($bwimage,$c,$c,$c);
}

function yiq($r, $g, $b) {
   $gray = ($r + $g + $b) / 3;
   if ($gray &gt; 0x7F) {
      return 0xFF;
   } else {
      return 0x00;
   }
   return (($r*0.299)+($g*0.587)+($b*0.114));
}

/*
Reads the original colors pixel by pixel
*/
for ($y = 0; $y &lt; $height; $y++) {
   for ($x = 0; $x &lt; $width; $x++) {
      $rgb = imagecolorat($source, $x, $y);
      $r = ($rgb &gt;&gt; 16) &amp; 0xFF;
      $g = ($rgb &gt;&gt; 8) &amp; 0xFF;
      $b = $rgb &amp; 0xFF;

      /*
      This is where we actually use yiq to modify our rbg values,
      and then convert them to our grayscale palette
      */
      $gs = yiq($r, $g, $b);
      imagesetpixel($bwimage, $x, $y, $palette[$gs]);
   }
}

/*
Outputs a jpg image, but you can change this to png or gif if that
is what you are working with
*/
header("Content-type: ".$header);
imagejpeg($bwimage);
?&gt;</pre>
<p>Let&#8217;s restart our Squid;</p>
<pre>sudo /etc/init.d/squid restart</pre>
<p>Everything should be working fine right now, good luck now you facebook lovers!</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 631px; width: 1px; height: 1px;">&lt;?<br />
/*<br />
joker.php</p>
<p>This is where we keep our tmp files.<br />
You can set it to /tmp also.<br />
*/<br />
$path = &#8220;/home/www/html/joker&#8221;;</p>
<p>// Parse the URL line delivered from Squid.<br />
$u = explode(&#8221; &#8220;,trim($_GET["url"]));<br />
if (count($u) &gt; 0) {<br />
$url = $u[0];<br />
} else {<br />
$url = $u;<br />
}</p>
<p>// Temporary file name.<br />
$temp = md5(time().microtime().$url);</p>
<p>$ff = fopen($url, &#8220;r&#8221;);<br />
$contents = stream_get_contents($ff);<br />
fclose($ff);</p>
<p>$fz = fopen($path.&#8221;/&#8221;.$temp,&#8221;w&#8221;);<br />
fwrite($fz, $contents);<br />
fclose($fz);</p>
<p>$file = $path.&#8221;/&#8221;.$temp;</p>
<p>// U shall not touch below this line.<br />
// &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- //<br />
// Yeah, that line above this.<br />
switch (substr($url,-4)) {<br />
case &#8220;.jpg&#8221;:<br />
$source = imagecreatefromjpeg($file);<br />
$header = &#8220;image/jpeg&#8221;;<br />
break;<br />
case &#8220;.png&#8221;:<br />
$source = imagecreatefrompng($file);<br />
$header = &#8220;image/png&#8221;;<br />
break;<br />
case &#8220;.gif&#8221;:<br />
$source = imagecreatefromgif($file);<br />
$header = &#8220;image/gif&#8221;;<br />
break;<br />
}</p>
<p>list($width, $height) = getimagesize($file);<br />
$bwimage= imagecreate($width, $height);</p>
<p>for ($c = 0; $c &lt; 256; $c++) {<br />
$palette[$c] = imagecolorallocate($bwimage,$c,$c,$c);<br />
}</p>
<p>function yiq($r, $g, $b) {<br />
$gray = ($r + $g + $b) / 3;<br />
if ($gray &gt; 0x7F) {<br />
return 0xFF;<br />
} else {<br />
return 0&#215;00;<br />
}</p>
<p>return (($r*0.299)+($g*0.587)+($b*0.114));<br />
}</p>
<p>/*<br />
Reads the original colors pixel by pixel<br />
*/<br />
for ($y = 0; $y &lt; $height; $y++) {<br />
for ($x = 0; $x &lt; $width; $x++) {<br />
$rgb = imagecolorat($source, $x, $y);<br />
$r = ($rgb &gt;&gt; 16) &amp; 0xFF;<br />
$g = ($rgb &gt;&gt; 8) &amp; 0xFF;<br />
$b = $rgb &amp; 0xFF;</p>
<p>/*<br />
This is where we actually use yiq to modify our rbg values,<br />
and then convert them to our grayscale palette<br />
*/<br />
$gs = yiq($r, $g, $b);<br />
imagesetpixel($bwimage, $x, $y, $palette[$gs]);<br />
}<br />
}</p>
<p>/*<br />
Outputs a jpg image, but you can change this to png or gif if that<br />
is what you are working with<br />
*/<br />
header(&#8220;Content-type: &#8220;.$header);<br />
imagejpeg($bwimage);<br />
?&gt;</p></div>
<div id="crp_related"><h3>İlgili Yazılar:</h3><ul><li><a href="http://www.serbestcagrisim.com/archives/305" rel="bookmark" class="crp_title">Daha iyi, daha hızlı, daha güçlü.</a></li><li><a href="http://www.serbestcagrisim.com/archives/54" rel="bookmark" class="crp_title">Facebook fun with Squid</a></li><li><a href="http://www.serbestcagrisim.com/archives/49" rel="bookmark" class="crp_title">squid ile facebook eğlencesi II &#8211; daha hızlı çözümler</a></li><li><a href="http://www.serbestcagrisim.com/archives/24" rel="bookmark" class="crp_title">squid ile facebook eğlencesi</a></li><li><a href="http://www.serbestcagrisim.com/archives/345" rel="bookmark" class="crp_title">Yanlış Facebook uygulaması!</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.serbestcagrisim.com/archives/299/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
