<?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>arif.suparlan.com &#187; image</title>
	<atom:link href="http://arif.suparlan.com/tag/image/feed" rel="self" type="application/rss+xml" />
	<link>http://arif.suparlan.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 31 Jan 2012 15:38:37 +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>Resize gambar proporsional &amp; posisi tengah dengan CSS &amp; Javascript (Update)</title>
		<link>http://arif.suparlan.com/2010/08/18/resize-proporsional-posisi-center-dengan-css-javascript?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=resize-proporsional-posisi-center-dengan-css-javascript</link>
		<comments>http://arif.suparlan.com/2010/08/18/resize-proporsional-posisi-center-dengan-css-javascript#comments</comments>
		<pubDate>Wed, 18 Aug 2010 15:16:46 +0000</pubDate>
		<dc:creator>Arif</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Iseng]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[center]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[overflow hidden]]></category>
		<category><![CDATA[resize]]></category>
		<category><![CDATA[scale]]></category>
		<category><![CDATA[skala]]></category>

		<guid isPermaLink="false">http://arif.suparlan.com/?p=259</guid>
		<description><![CDATA[Saya hampir gila mencoba me-resize dan memposisikan tengah beberapa gambar di dalam div yang mempunyai property overflow: hidden. Terlalu berlebihan ya? hehehe&#8230; Tapi memang sulit. Ukuran gambar tersebut berbeda-beda, portrait atau landscape, yang jelas dia lebih besar dari ukuran div &#8230; <a href="http://arif.suparlan.com/2010/08/18/resize-proporsional-posisi-center-dengan-css-javascript">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Saya hampir gila mencoba me-<em>resize </em>dan memposisikan tengah beberapa gambar di dalam <em>div </em>yang mempunyai <em>property overflow: hidden</em>. Terlalu berlebihan ya? hehehe&#8230; Tapi memang sulit. Ukuran gambar tersebut berbeda-beda, portrait atau landscape, yang jelas dia lebih besar dari ukuran div itu. Saya belum bisa ngakalinnya bermodalkan CSS saja.<span id="more-259"></span></p>
<p>Meskipun mentok dengan CSS, saya akhirnya ngakalin dengan bantuan Javascript. Dengan memanfaatkan Javascript saya bisa untuk merubah ukuran dan posisi gambar secara dinamis. Bisa lebih dari itu sih, tapi untuk pemula seperti saya ini bisa sangat membantu melayout halaman.</p>
<p>Dulu saya pernah buat script untuk <a href="http://arif.suparlan.com/2009/09/11/resize-crop-gambar-proporsional-di-codeigniter" target="_blank">Resize &amp; crop gambar dengan Codeigniter</a>. Nah script ini memakai hitung-hitungan yang sama. Kalau Codeigniter kalkulasinya oleh PHP di sisi server, yang ini di sisi client/browser. Cara ini bisa dipermudah dengan menggunakan framework Javascript. Saya belum coba, tapi kalau ada cara yang lebih mudah lagi, mohon dipost di sini. Makasih.</p>
<p>Di bawah berikut kira-kira contoh scriptnya. Kedua gambar yang berukuran besar dengan orientasi yang berbeda, portrait dan landscape ini saya letakkan sebagai icon dalam div dengan property overflow hidden. Prosesnya mudah, tetapi agak panjang, yaitu membandingkan rasio dua ukuran untuk menentukan orientasi kemudian me-resize dan menghitung posisi tengah. Ini contohnya: <a href="http://arif.suparlan.com/demo/resize_position_center/" target="_blank">Demo</a>.</p>
<pre>&lt;html&gt;
&lt;head&gt;
 &lt;title&gt;Resize proporsional &amp; posisi center dengan Javascript&lt;/title&gt;
&lt;style type="text/css"&gt;
.test {
 border:1px solid;
 margin-top:100px;
 margin-left:250px;
 width:150px;
 height:150px;
 overflow: hidden;
 float: left;
}

.test img {
}

&lt;/style&gt;

&lt;script type="text/javascript"&gt;
 window.onload = function() {
 var $images = document.getElementsByTagName('img');
 $x=150;
 $y=150;

 for($i=0; $i&lt;$images.length; $i++)
 {
 $new_size = resize_min($images[$i].width, $images[$i].height, $x, $y);

 $images[$i].style.width = $new_size[0];
 $images[$i].style.height = $new_size[1];
 $images[$i].style.marginLeft = -(($new_size[0]-$x)*.25) + "px";
 $images[$i].style.marginTop = -(($new_size[1]-$y)*.25) + "px";
 }

 function resize_min($x0, $y0, $x1, $y1) {
 $xs=$x0/$x1;
 $ys=$y0/$y1;

 if ($ys&gt;$xs){
 $x2 = $x1;
 $y2 = $x1 * ($y0/$x0);
 }
 else {
 $x2 = $y1 * ($x0/$y0);
 $y2 = $y1;
 }

 return [$x2, $y2];
 }
 }
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Resize proporsional &amp; posisi center dengan Javascript&lt;/h1&gt;

&lt;div&gt;
 &lt;img src="img01.jpg" /&gt;
&lt;/div&gt;
&lt;div&gt;
 &lt;img src="img02.jpg" /&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Update berikut untuk penggunaan dengan jQuery. Ada beberapa tambahan script untuk mengukur dimensi ketika dalam keadaan gambar hidden.</p>
<pre>$(document).ready(function() {
 $('img').each(function() {
 var x = 150;
 var y = 150;
 var ratio = x/y;

 if ($(this).height() &gt; 0) {
 var width = $(this).width();
 var height = $(this).height();
 } else {
 var elem_clone = $(this).clone()
 .attr("id", false)
 .css({visibility:"hidden", display:"block", position:"absolute"});
 $("body").append(elem_clone);
 var width = elem_clone.width();
 var height = elem_clone.height();
 elem_clone.remove();    
 }

 $new_size = resize_min(width, height, x, y);

 $(this).css("width", $new_size[0]);
 $(this).css("height", $new_size[1]);
 $(this).css("marginLeft", -(($new_size[0]-x)*.5) + "px");
 $(this).css("marginTop", -(($new_size[1]-y)*.5) + "px");
 });
 });</pre>
]]></content:encoded>
			<wfw:commentRss>http://arif.suparlan.com/2010/08/18/resize-proporsional-posisi-center-dengan-css-javascript/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Resize &amp; Crop Gambar Proporsional di Codeigniter</title>
		<link>http://arif.suparlan.com/2009/09/11/resize-crop-gambar-proporsional-di-codeigniter?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=resize-crop-gambar-proporsional-di-codeigniter</link>
		<comments>http://arif.suparlan.com/2009/09/11/resize-crop-gambar-proporsional-di-codeigniter#comments</comments>
		<pubDate>Fri, 11 Sep 2009 06:38:13 +0000</pubDate>
		<dc:creator>Arif</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[crop]]></category>
		<category><![CDATA[gambar]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[maintain_ratio]]></category>
		<category><![CDATA[resize]]></category>
		<category><![CDATA[scale]]></category>
		<category><![CDATA[skala]]></category>

		<guid isPermaLink="false">http://arif.suparlan.com/?p=137</guid>
		<description><![CDATA[Beberapa hari yang lalu saya memerlukan sebuah class atau library yang sederhana untuk melakukan upload gambar dan sekaligus menyesuaikan ukuran yang saya mau. Dulu saya pernah buat fungsi yang sama, waktu pertama kali kenal GD-nya PHP. Tapi sudah lupa bentuknya &#8230; <a href="http://arif.suparlan.com/2009/09/11/resize-crop-gambar-proporsional-di-codeigniter">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Beberapa hari yang lalu saya memerlukan sebuah class atau library yang sederhana untuk melakukan upload gambar dan sekaligus menyesuaikan ukuran yang saya mau. Dulu saya pernah buat fungsi yang sama, waktu pertama kali kenal GD-nya PHP. Tapi sudah lupa bentuknya gimana. Yang sekarang saya buat adalah library extension untuk Codeigniter.<span id="more-137"></span></p>
<p>Codeigniter sudah punya library untuk upload, jadi upload tidak perlu dibahas lagi. Codeigniter juga sudah punya class untuk memanipulasi gambar diantaranya untuk melakukan Image Resizing, Thumbnail Creation, Image Cropping, dan lain-lain. Tapi untuk melakukan perubahan ukuran gambar dengan skala yang proporsional dengan ukuran yang spesifik, agak sedikit ada hitung-hitungannya.</p>
<p>Pertama kali, dengan membandingkan rasio ukuran awal dan ukuran yang baru, dapat menentukan menggunakan x atau y untuk menjaga proporsi gambar. Kalau mau meng-crop tepat di tengah-tengah gambar, maka axis x dan y juga ikut dihitung. Kecuali kalau cropnya mau dilakukan dari sebelah kiri-atas, axisnya tinggal diset x=0 &amp; y=0. Lebih jelasnya lihat code-nya (my_image.php).</p>
<pre>&lt;?php
if (!defined('BASEPATH')) exit('No direct script access permitted.');

class My_image
{
	var $CI;
	function My_image()
	{
    //parent::CI_Image_lib();
  }  

	function resize_crop($config, $resize_width=200, $resize_height=200)
	{
    if ($config)
    {
      $CI =&amp; get_instance();

      $CI-&gt;load-&gt;library('image_lib');
      $CI-&gt;load-&gt;library('baseintencoder');

      $img_size = getimagesize($config['source_image']);

      $t_ratio = $resize_width/$resize_height;
      $o_width = $img_size[0];
      $o_height = $img_size[1];
      if ($t_ratio &gt; $o_width/$o_height)
      {
        $config['width'] = $resize_width;
        $config['height'] = round( $resize_width * ($o_height / $o_width));
        $y_axis = round(($config['height']/2) - ($resize_height/2));
        $x_axis = 0;
      }
      else
      {
        $config['width'] = round( $resize_height * ($o_width / $o_height));
        $config['height'] = $resize_height;
        $y_axis = 0;
        $x_axis = round(($config['width']/2) - ($resize_width/2));
      }

      $source_img01 = $config['new_image'];

      $CI-&gt;image_lib-&gt;clear();
      $CI-&gt;image_lib-&gt;initialize($config);
      $CI-&gt;image_lib-&gt;resize();

      $config['image_library'] = 'gd2';
      $config['source_image'] = $source_img01;
      $config['create_thumb'] = false;
      $config['maintain_ratio'] = true;
      $config['width'] = $resize_width;
      $config['height'] = $resize_height;
      $config['y_axis'] = $y_axis ;
      $config['x_axis'] = $x_axis ;

      $CI-&gt;image_lib-&gt;clear();
      $CI-&gt;image_lib-&gt;initialize($config);
      $CI-&gt;image_lib-&gt;crop();

      return $config['new_image'];
    }
  }
}

?&gt;</pre>
<p>Memanggil fungsinya seperti ini:</p>
<pre>$config['image_library'] = 'gd2';
$config['source_image'] = $nama_source_image;
$config['new_image'] = $nama_hasil_image;

$this-&gt;load-&gt;library('my_image');
$this-&gt;my_image-&gt;resize_crop($config, '100', '200');</pre>
<p>Saya termasuk noob di Codeigniter, mohon ma&#8217;af kalau ada yang salah tolong dikoreksi&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://arif.suparlan.com/2009/09/11/resize-crop-gambar-proporsional-di-codeigniter/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

