[ Index ]
 

Code source de Dotclear 2.0-beta6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/clearbricks/image/ -> class.image.tools.php (source)

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # This file is part of Clearbricks.
   4  # Copyright (c) 2006 Olivier Meunier and contributors. All rights
   5  # reserved.
   6  #
   7  # Clearbricks is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; either version 2 of the License, or
  10  # (at your option) any later version.
  11  # 
  12  # Clearbricks is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  # 
  17  # You should have received a copy of the GNU General Public License
  18  # along with Clearbricks; if not, write to the Free Software
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  #
  21  # ***** END LICENSE BLOCK *****
  22  
  23  # Some functions taken from BIG
  24  # https://dev.media-box.net/big/
  25  
  26  class imageTools
  27  {
  28      public $res;
  29      
  30  	public function __construct()
  31      {
  32          if (!function_exists('imagegd2')) {
  33              throw new Exception('GD is not installed');
  34          }
  35          $this->res = null;
  36      }
  37      
  38  	public function loadImage($f)
  39      {
  40          if (!file_exists($f)) {
  41              throw new Exception('Image doest not exists');
  42          }
  43          
  44          if (($info = @getimagesize($f)) !== false)
  45          {
  46              switch ($info[2])
  47              {
  48                  case 3 :
  49                      $this->res = @imagecreatefrompng($f);
  50                      break;
  51                  case 2 :
  52                      $this->res = @imagecreatefromjpeg($f);
  53                      break;
  54                  case 1 :
  55                      $this->res = @imagecreatefromgif($f);
  56                      break;
  57              }
  58          }
  59          
  60          if (!is_resource($this->res)) {
  61              throw new Exception('Unable to load image');
  62          }
  63      }
  64      
  65  	public function getW()
  66      {
  67          return imagesx($this->res);
  68      }
  69      
  70  	public function getH()
  71      {
  72          return imagesy($this->res);
  73      }
  74      
  75  	function output($type='png',$file=null,$qual=90)
  76      {
  77          if (!$file)
  78          {
  79              header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
  80              header('Pragma: no-cache');
  81              switch (strtolower($type))
  82              {
  83                  case 'png' :
  84                      header('Content-type: image/png');
  85                      imagepng($this->res);
  86                      return true;
  87                  case 'jpeg' :
  88                  case 'jpg':
  89                      header('Content-type: image/jpeg');
  90                      imagejpeg($this->res,NULL,$qual);
  91                      return true;
  92                  default :
  93                      return false;
  94              }
  95          }
  96          elseif (is_writable(dirname($file)))
  97          {
  98              switch(strtolower($type))
  99              {
 100                  case 'png' :
 101                      return imagepng($this->res,$file);
 102                  case 'jpeg' :
 103                  case 'jpg' :
 104                      return imagejpeg($this->res,$file,$qual);
 105                  default :
 106                      return false;
 107              }
 108          }
 109      }
 110      
 111      /**
 112      @function resize
 113      
 114      Resize image ressource
 115      
 116      @param mixed    WIDTH        Image width (px or percent)
 117      @param mixed    HEIGHT        Image height (px or percent)
 118      @param string    mode            Crop mode (force, crop, ratio)
 119      @param boolean    EXPAND        Allow resize of image
 120      */
 121  	function resize($WIDTH,$HEIGHT,$MODE='ratio',$EXPAND=false)
 122      {
 123          
 124          $imgWidth=$this->getW();
 125          $imgHeight=$this->getH();
 126          
 127          if(strpos($WIDTH,'%',0))
 128          $WIDTH=$imgWidth*$WIDTH/100;
 129          if(strpos($HEIGHT,'%',0))
 130          $HEIGHT=$imgHeight*$HEIGHT/100;
 131          
 132          $ratio=$imgWidth/$imgHeight;
 133          
 134          // guess resize ($_w et $_h)
 135          if($MODE=='ratio')
 136          {
 137              $_w=99999;
 138              if($HEIGHT>0)
 139              {
 140                  $_h=$HEIGHT;
 141                  $_w=$_h*$ratio;
 142              }
 143              if($WIDTH>0 && $_w>$WIDTH)
 144              {
 145                  $_w=$WIDTH;
 146                  $_h=$_w/$ratio;
 147              }
 148              
 149              if(!$EXPAND && $_w>$imgWidth)
 150              {
 151                  $_w=$imgWidth;
 152                  $_h=$imgHeight;
 153              }
 154          }
 155          else
 156          {
 157              // crop source image
 158              $_w=$WIDTH;
 159              $_h=$HEIGHT;
 160          }
 161          
 162          if($MODE=='force')
 163          {
 164              if($WIDTH>0)
 165              $_w=$WIDTH;
 166              else
 167              $_w=$HEIGHT*$ratio;
 168              
 169              if($HEIGHT>0)
 170              $_h=$HEIGHT;
 171              else
 172              $_h=$WIDTH/$ratio;
 173              
 174              if(!$EXPAND && $_w>$imgWidth)
 175              {
 176                  $_w=$imgWidth;
 177                  $_h=$imgHeight;
 178              }
 179              
 180              $cropW=$imgWidth;
 181              $cropH=$imgHeight;
 182              $decalW=0;
 183              $decalH=0;
 184          }
 185          else
 186          {
 187              // guess real viewport of image
 188              $innerRatio=$_w/$_h;
 189              if($ratio>=$innerRatio)
 190              {
 191                  $cropH=$imgHeight;
 192                  $cropW=$imgHeight*$innerRatio;
 193                  $decalH=0;
 194                  $decalW=($imgWidth-$cropW)/2;
 195              }
 196              else
 197              {
 198                  $cropW=$imgWidth;
 199                  $cropH=$imgWidth/$innerRatio;
 200                  $decalW=0;
 201                  $decalH=($imgHeight-$cropH)/2;
 202              }
 203          }
 204          
 205          $img2=imagecreatetruecolor($_w,$_h);
 206          imagecopyresampled($img2,$this->res,0,0,$decalW,$decalH,$_w,$_h,$cropW,$cropH);
 207          imagedestroy($this->res);
 208          $this->res=$img2;
 209          return true;
 210      }
 211  }
 212  ?>


Généré le : Fri Feb 23 22:16:06 2007 par Balluche grâce à PHPXref 0.7