Stillcetek.com
Struggle to Move Better
Google
 
Home | Gallery | Project | Contact | About       
 
 
 
About
Lukman, lahir di Kalianda, Lampung Selatan 1984, selesai pendidikan tingkat atas di SMA Islam di Kalianda, kemudian melanjutkan ke Perguruan Tinggi Ilmu Komputer AMIK Dian Cipta Cendikia Lampung ... >>
Recently
Categories
Archives
link blog

Aku punya koleksi fhoto dan kepengen tak upload ke stillcetek gallery, tapi ukuran fhoto-fhoto itu ada yang lebih dari 2000 bahkan 3000 pixel, wah kalo tak upload mentah-mentah bisa empot-empotan space stillcetek. Pengennya di resize jadi 600 pixel aja semua. Kalo pake adobe photoshop di kecilin satu-satu lama. terus coba minta bantuan mbah google nemu deh beberapa contoh untuk meresize image pake php. sedikit dimodif jadi deh sesuai dengan yang aku butuhkan.
Pastikan apache support dengan GD, Graphics library untuk mengolah image ala programmer.

ini nih fungsinya :

function createThumbnail($imageDirectory, $imageName,
                         $thumbName, $thumbsize, $thumbDirectory="")
{
  $fileType = strtolower(substr($imageName, -3));
  if ($fileType=='jpg')
  {
    $srcImg=imagecreatefromjpeg($imageDirectory.'/'.$imageName);
  }
  elseif ($fileType=='png')
  {
    $srcImg=imagecreatefrompng($imageDirectory.'/'.$imageName);
  }
  else
  {
    echo "<br>No Support images".$imageDirectory.'/'.$imageName;
  }
  if ($fileType=='jpg' || $fileType=='png')
  {
  $oldWidth = imageSX($srcImg);
  $oldHeight = imageSY($srcImg);

if ($oldWidth > $oldHeight)
  {
    $newWidth = $thumbsize;
    $newHeight = $oldHeight*($thumbsize/$oldWidth);
  } 

  if ($oldWidth < $oldHeight)
  {
    $newWidth = $oldWidth*($thumbsize/$oldHeight);
    $newHeight = $thumbsize;
  } 

  if ($oldWidth == $oldHeight)
  {
    $newWidth = $thumbsize;
    $newHeight = $thumbsize;
  }

  $thumbImg=ImageCreateTrueColor($newWidth,$newHeight);
  imagecopyresampled($thumbImg,$srcImg,0,0,0,0,$newWidth,
                     $newHeight,$oldWidth,$oldHeight);

  if ($thumbDirectory==""){
    imagejpeg($thumbImg,'',$thumbsize);
  }else{
    imagejpeg($thumbImg,$thumbDirectory.'/'.$thumbName.$imageName);
  }
  }
}

Fungsi ini hanya bisa meresize images jpg dan png.

Keterangan parameter :

  • $imageDirectory : Folder letak source images
  • $imageName : Nama images yang akan diresize
  • $thumbName : Pemberian nama awal thumbnail
  • $thumbsize : Ukuran thumbnail (pixel)
  • $thumbDirectory : letak folder hasil thumbnail, Jika parameter $thumbDirectory null maka fungsi akan menghasilkan kode binary dari hasil resize, yang bisa dimasukkan kedalam database.

Contoh:

createThumbnail("img/", "sea.jpg", "thumb_", 600,"thumbs/" );

Jika banyak foto yang mau diresize, berikut contohnya:

Fungsi untuk mendapatkan list images di directory :

function getListImages($dir)
{
 $images = array();
 if (is_dir($dir)) {
  if ($d = opendir($dir)) {
   while (($file = readdir($d)) !== false) {
    if ($file!="." && $file!="..")
    {
      $images[] = $file;
    }
   }
  closedir($d);
  }
}
return $images;
}

Pemakaian kedua fungsi diatas :

$sourceImages = "C:/Data/Fhoto/album1";
$thumbImages = "gallery/thumbs/album1";
$thumbSize = 600;
$thumbName = "thumb_";

$getImages=getListImages($sourceImages);

if (is_array($getImages))
{
  foreach($getImages as $i=> $img)
  {
   if (!file_exists("$thumbImages/$img")){
     createThumbnail($sourceImages, $img, $thumbName,
                     $thumbSize,$thumbImages);
   }
  }
  echo "$i thumbs has been created";
 }
 else
 {
   echo "No Images found!";
 }

Demikian semoga bermanfaat.

Author : Lukman

Comments

Leave a Reply

   
   
   
   
   
   
       
   
           

CAPTCHA image

Visitor
 
free-counter
 
Shoutbox
recent comment