<?php

/***
 * resizeImage (function)
 *
 * @Author: Henry v.d. Berg info@henryvdberg.nl
 *
 * Deze functie wijzigt de grootte van een JPG/PNG/GIF plaatje
 *
 * @param string:   $sSrcPath:      Locatie van het originele bestand, eindigend op de bestandsnaam incl. extentie
 * @param string:   $sDestPath:     Locatie waar het ge-resized bestand wordt opgeslagen, ZONDER bestandsnaam of extentie, eindigend op '/'
 * @param int:      $iDestWidth:    Maximale breedte van het uiteidelijke bestand
 * @param int:      $iDestHeight:   Maximale hoogte van het uiteindelijke bestand
 * @param int:      $iDestQuality:  De kwaliteit van het uiteindelijke bestand in procenten, standaard 75%
 *
 * @return boolean: TRUE or FALSE
 ***/

function resizeImage($sSrcPath, $sDestPath, $iDestWidth, $iDestHeight, $iDestQuality = 75) {
  $aSrcMime = getimagesize($sSrcPath);
  $aSrcName = array_pop(split("[/]", $sSrcPath));
  $aSrcExtention = strtolower(array_pop(split("[.]", $sSrcPath)));
  $sDestPath = $sDestPath . $aSrcName;

  switch ($aSrcMime["mime"]) {
    case "image/gif":
      if ($aSrcExtention == "gif") {
        $sSrcData = imagecreatefromgif($sSrcPath);
      }
      else {
        return false;
      }
    break;
    case "image/png":
      if ($aSrcExtention == "png") {
        $sSrcData = imagecreatefrompng($sSrcPath);
      }
      else {
        return false;
      }
    break;
    case "image/jpeg":
      if ( ($aSrcExtention == "jpg") || ($aSrcExtention == "jpeg") ) {
        $sSrcData = imagecreatefromjpeg($sSrcPath);
      }
      else {
        return false;
      }
    break;
    default:
      return false;
    break;
  }
 
  if ($sSrcData) {
    $iSrcWidth = imagesx($sSrcData);
    $iSrcHeight = imagesy($sSrcData);
    $fSrcRatio = $iSrcWidth/$iSrcHeight;
    $fDestRatio = $iDestWidth/$iDestHeight;

    if ($fDestRatio > $fSrcRatio){
      $iDestHeight = $iDestHeight;
      $iDestWidth = $iDestHeight*$fSrcRatio;
    }
    else {
      $iDestWidth = $iDestWidth;
      $iDestHeight = $iDestWidth/$fSrcRatio;
    }

    $sDestData = imagecreatetruecolor($iDestWidth, $iDestHeight);

    if (imagecopyresampled($sDestData, $sSrcData, 0, 0, 0, 0, $iDestWidth, $iDestHeight, $iSrcWidth, $iSrcHeight)) {

      switch ($aSrcMime["mime"]) {
        case "image/gif":
          if ($aSrcExtention == "gif") {

            if(imagegif($sDestData, $sDestPath, $iDestQuality)) {
              imagedestroy($sSrcData);
              return true;
            }
            else {
              return false;
            }

          }
          else {
            return false;
          }
        break;
        case "image/png":
          if ($aSrcExtention == "png") {

            if(imagepng($sDestData, $sDestPath, $iDestQuality)) {
              imagedestroy($sSrcData);
              return true;
            }
            else {
              return false;
            }

          }
          else {
            return false;
          }
        break;
        case "image/jpeg":
          if ( ($aSrcExtention == "jpg") || ($aSrcExtention == "jpeg") ) {

            if(imagejpeg($sDestData, $sDestPath, $iDestQuality)) {
              imagedestroy($sSrcData);
              return true;
            }
            else {
              return false;
            }

          }
          else {
            return false;
          }
        break;
        default:
          return false;
        break;
      }

    }
    else {
      return false;
    }

  }
  else {
    return false;
  }

}
?>