Plume CMS PHP Cross Reference Content Management Systems

Source: /manager/extinc/lib.image.php - 200 lines - 5467 bytes - Summary - Text - Print

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # Version: MPL 1.1/GPL 2.0/LGPL 2.1
   4  #
   5  # The contents of this file are subject to the Mozilla Public License Version
   6  # 1.1 (the "License"); you may not use this file except in compliance with
   7  # the License. You may obtain a copy of the License at
   8  # http://www.mozilla.org/MPL/
   9  #
  10  # Software distributed under the License is distributed on an "AS IS" basis,
  11  # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12  # for the specific language governing rights and limitations under the
  13  # License.
  14  #
  15  # The Original Code is DotClear Weblog.
  16  #
  17  # The Initial Developer of the Original Code is
  18  # Olivier Meunier.
  19  # Portions created by the Initial Developer are Copyright (C) 2003
  20  # the Initial Developer. All Rights Reserved.
  21  #
  22  # Contributor(s):
  23  #
  24  # Alternatively, the contents of this file may be used under the terms of
  25  # either the GNU General Public License Version 2 or later (the "GPL"), or
  26  # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27  # in which case the provisions of the GPL or the LGPL are applicable instead
  28  # of those above. If you wish to allow use of your version of this file only
  29  # under the terms of either the GPL or the LGPL, and not to allow others to
  30  # use your version of this file under the terms of the MPL, indicate your
  31  # decision by deleting the provisions above and replace them with the notice
  32  # and other provisions required by the GPL or the LGPL. If you do not delete
  33  # the provisions above, a recipient may use your version of this file under
  34  # the terms of any one of the MPL, the GPL or the LGPL.
  35  #
  36  # ***** END LICENSE BLOCK *****
  37  
  38  /**
  39  Ouvre une image pour activer le pointeur de ressourse $img
  40  
  41  @proto function openImg
  42  
  43  @param ressource img Pointeur de ressource
  44  @param string url Chemin vers l'image
  45  */
  46  function cropImg($uri,$file,$w=120,$h=200)
  47  {
  48      if (!file_exists($uri)) {
  49          return false;
  50      }
  51      
  52      if (($size = @getimagesize($uri)) === false) {
  53          return false;
  54      }
  55      
  56      $type = $size[2];
  57      $H = $size[1];
  58      $W = $size[0];
  59      
  60      if ($type == '1') {
  61          $function = 'imagecreatefromgif';
  62      } elseif ($type == '2') {
  63          $function = 'imagecreatefromjpeg';
  64      } elseif ($type == '3') {
  65          $function = 'imagecreatefrompng';
  66      } else {
  67          return false;
  68      }
  69      if (!function_exists($function)) {
  70          return false;
  71      }
  72      
  73      if (($img = @$function($uri)) == false) {
  74          return false;
  75      }
  76      $rB = $H/$W;
  77      $rS = $h/$w;
  78      if (($H > $h) && ($W > $w)) {
  79          if ($rB > $rS) {
  80              $height = $h;
  81              $width  = $height/$rB;
  82          } else {
  83              $width = $w;
  84              $height = $width*$rB; 
  85          }
  86      } elseif ($H > $h) {
  87          $height = $h;
  88          $width  = $height/$rB;
  89      } elseif ($W > $w) {
  90          $width = $w;
  91          $height = $width*$rB; 
  92      } else {
  93          $height = $H;
  94          $width  = $W;
  95      } 
  96      
  97      /*$W = $size[0];
  98      $H = $size[1];
  99      $z = $W/$w;
 100      $h = $H/$z;*/
 101      $zx = $W/$width;
 102      $zy = $H/$height;
 103  
 104      if (gd_version() >= 2) {
 105          if ( ($img2 = imagecreatetruecolor(round($width),round($height)))  === false) {
 106              return false;
 107          }
 108      } else {
 109          if ( ($img2 = ImageCreate(round($width),round($height)))  === false) {
 110              return false;
 111          }    
 112      }
 113      
 114      imageCopyResampleBicubic($img2,$img,0,0,0,0,$width,$height,$zx,$zy);
 115      
 116      if (@imagejpeg($img2,$file,80) === false) {
 117          return false;
 118      }
 119      imagedestroy($img2);
 120  }
 121  
 122  function imageCopyResampleBicubic(&$dst, &$src, $dstx, $dsty, $srcx, $srcy, $w, $h, $zoomX, $zoomY = '')
 123  {
 124      if (!$zoomY) {
 125          $zoomY = $zoomX;
 126      }
 127      
 128      $palsize = ImageColorsTotal($src);
 129      
 130      for ($i = 0; $i<$palsize; $i++)
 131      {
 132          $colors = ImageColorsForIndex($src, $i);
 133          ImageColorAllocate($dst, $colors['red'], $colors['green'], $colors['blue']);
 134      }
 135      
 136      $zoomX2 = (int)($zoomX/2);
 137      $zoomY2 = (int)($zoomY/2);
 138      
 139      $dstX = imagesx($dst);
 140      $dstY = imagesy($dst);
 141      $srcX = imagesx($src);
 142      $srcY = imagesy($src);
 143      
 144      for ($j = 0; $j<($h-$dsty); $j++)
 145      {
 146          $sY = (int)($j*$zoomY)+$srcy;
 147          $y13 = $sY+$zoomY2;
 148          $dY = $j+$dsty;
 149          
 150          if (($sY >= $srcY) or ($dY >= $dstY) or ($y13 >= $srcY)) {
 151              break 1;
 152          }
 153          
 154          for ($i = 0; $i<($w-$dstx); $i++)
 155          {
 156              $sX = (int)($i*$zoomX)+$srcx;
 157              $x34 = $sX+$zoomX2;
 158              $dX = $i+$dstx;
 159              
 160              if (($sX >= $srcX) or ($dX >= $dstX) or ($x34 >= $srcX)) {
 161                  break 1;
 162              }
 163              
 164              $c1 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $y13));
 165              $c2 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $sY));
 166              $c3 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $y13));
 167              $c4 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $sY));
 168              
 169              $r = ($c1['red']+$c2['red']+$c3['red']+$c4['red'])/4;
 170              $g = ($c1['green']+$c2['green']+$c3['green']+$c4['green'])/4;
 171              $b = ($c1['blue']+$c2['blue']+$c3['blue']+$c4['blue'])/4;
 172              
 173              ImageSetPixel($dst, $dX, $dY, ImageColorClosest($dst, $r, $g, $b));
 174          }
 175      }
 176  }
 177  
 178  
 179  function gd_version() 
 180  {
 181     static $gd_version_number = null;
 182     if ($gd_version_number === null) {
 183         // Use output buffering to get results from phpinfo()
 184         // without disturbing the page we're in.  Output
 185         // buffering is "stackable" so we don't even have to
 186         // worry about previous or encompassing buffering.
 187         ob_start();
 188         phpinfo(8);
 189         $module_info = ob_get_contents();
 190         ob_end_clean();
 191         if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i",
 192                 $module_info,$matches)) {
 193             $gd_version_number = $matches[1];
 194         } else {
 195             $gd_version_number = 0;
 196         }
 197     }
 198     return $gd_version_number;
 199  }
 200  ?>

title

Description

title

Description

title

Description

title

title

Body