[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/ -> functions_image.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id$
  10   */
  11  
  12  /**
  13   * Generates a thumbnail based on specified dimensions (supports png, jpg, and gif)
  14   * 
  15   * @param string the full path to the original image
  16   * @param string the directory path to where to save the new image
  17   * @param string the filename to save the new image as
  18   * @param integer maximum hight dimension
  19   * @param integer maximum width dimension
  20   * @return array thumbnail on success, error code 4 on failure
  21   */
  22  function generate_thumbnail($file, $path, $filename, $maxheight, $maxwidth)
  23  {
  24      if(!function_exists("imagecreate"))
  25      {
  26          $thumb['code'] = 3;
  27          return $thumb;
  28      }
  29      
  30      $imgdesc = getimagesize($file);
  31      $imgwidth = $imgdesc[0];
  32      $imgheight = $imgdesc[1];
  33      $imgtype = $imgdesc[2];
  34      $imgattr = $imgdesc[3];
  35      $imgbits = $imgdesc['bits'];
  36      $imgchan = $imgdesc['channels'];
  37      
  38      if($imgwidth == 0 || $imgheight == 0)
  39      {
  40          $thumb['code'] = 3;
  41          return $thumb;
  42      }        
  43      if(($imgwidth >= $maxwidth) || ($imgheight >= $maxheight))
  44      {
  45          check_thumbnail_memory($imgwidth, $imgheight, $imgtype, $imgbits, $imgchan);
  46          
  47          if($imgtype == 3)
  48          {
  49              if(@function_exists("imagecreatefrompng"))
  50              {
  51                  $im = @imagecreatefrompng($file);
  52              }
  53          }
  54          elseif($imgtype == 2)
  55          {
  56              if(@function_exists("imagecreatefromjpeg"))
  57              {
  58                  $im = @imagecreatefromjpeg($file);
  59              }
  60          }
  61          elseif($imgtype == 1)
  62          {
  63              if(@function_exists("imagecreatefromgif"))
  64              {
  65                  $im = @imagecreatefromgif($file);
  66              }
  67          }
  68          else
  69          {
  70              $thumb['code'] = 3;
  71              return $thumb;
  72          }
  73          if(!$im)
  74          {
  75              $thumb['code'] = 3;
  76              return $thumb;
  77          }
  78          $scale = scale_image($imgwidth, $imgheight, $maxwidth, $maxheight);
  79          $thumbwidth = $scale['width'];
  80          $thumbheight = $scale['height'];
  81          $thumbim = @imagecreatetruecolor($thumbwidth, $thumbheight);
  82          
  83          if(!$thumbim)
  84          {
  85              $thumbim = @imagecreate($thumbwidth, $thumbheight);
  86              $resized = true;
  87          }
  88  
  89          // Attempt to preserve the transparency if there is any
  90          if($imgtype == 3)
  91          {
  92              // A PNG!
  93              imagealphablending($thumbim, false);
  94              imagefill($thumbim, 0, 0, imagecolorallocatealpha($thumbim, 0, 0, 0, 127));
  95  
  96              // Save Alpha...
  97              imagesavealpha($thumbim, true);
  98          }
  99          elseif($imgtype == 1)
 100          {
 101              // Transparent GIF?
 102              $trans_color = imagecolortransparent($im);
 103              if($trans_color >= 0 && $trans_color < imagecolorstotal($im))
 104              {
 105                  $trans = imagecolorsforindex($im, $trans_color);
 106                  $new_trans_color = imagecolorallocate($thumbim, $trans['red'], $trans['blue'], $trans['green']);
 107                  imagefill($thumbim, 0, 0, $new_trans_color);
 108                  imagecolortransparent($thumbim, $new_trans_color);
 109              }
 110          }
 111  
 112          if(!isset($resized))
 113          {
 114              @imagecopyresampled($thumbim, $im, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imgwidth, $imgheight);
 115          }
 116          else
 117          {
 118              @imagecopyresized($thumbim, $im, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imgwidth, $imgheight);
 119          }
 120          @imagedestroy($im);
 121          if(!function_exists("imagegif") && $imgtype == 1)
 122          {
 123              $filename = str_replace(".gif", ".jpg", $filename);
 124          }
 125          switch($imgtype)
 126          {
 127              case 1:
 128                  if(function_exists("imagegif"))
 129                  {
 130                      @imagegif($thumbim, $path."/".$filename);
 131                  }
 132                  else
 133                  {
 134                      @imagejpeg($thumbim, $path."/".$filename);
 135                  }
 136                  break;
 137              case 2:
 138                  @imagejpeg($thumbim, $path."/".$filename);
 139                  break;
 140              case 3:
 141                  @imagepng($thumbim, $path."/".$filename);
 142                  break;
 143          }
 144          @my_chmod($path."/".$filename, '0644');
 145          @imagedestroy($thumbim);
 146          $thumb['code'] = 1;
 147          $thumb['filename'] = $filename;
 148          return $thumb;
 149      }
 150      else
 151      {
 152          return array("code" => 4);
 153      }
 154  }
 155  
 156  /**
 157   * Attempts to allocate enough memory to generate the thumbnail
 158   * 
 159   * @param integer hight dimension
 160   * @param integer width dimension
 161   * @param string one of the IMAGETYPE_XXX constants indicating the type of the image
 162   * @param string the bits area the number of bits for each color
 163   * @param string the channels - 3 for RGB pictures and 4 for CMYK pictures
 164   */
 165  function check_thumbnail_memory($width, $height, $type, $bitdepth, $channels)
 166  {
 167      if(!function_exists("memory_get_usage"))
 168      {
 169          return false;
 170      }
 171  
 172      $memory_limit = @ini_get("memory_limit");
 173      if(!$memory_limit || $memory_limit == -1)
 174      {
 175          return false;
 176      }
 177  
 178      $limit = preg_match("#^([0-9]+)\s?([kmg])b?$#i", trim(my_strtolower($memory_limit)), $matches);
 179      $memory_limit = 0;
 180      if($matches[1] && $matches[2])
 181      {
 182          switch($matches[2])
 183          {
 184              case "k":
 185                  $memory_limit = $matches[1] * 1024;
 186                  break;
 187              case "m":
 188                  $memory_limit = $matches[1] * 1048576;
 189                  break;
 190              case "g":
 191                  $memory_limit = $matches[1] * 1073741824;
 192          }
 193      }
 194      $current_usage = memory_get_usage();
 195      $free_memory = $memory_limit - $current_usage;
 196      
 197      $thumbnail_memory = round(($width * $height * $bitdepth * $channels / 8) * 5);
 198      $thumbnail_memory += 2097152;
 199      
 200      if($thumbnail_memory > $free_memory)
 201      {
 202          if($matches[1] && $matches[2])
 203          {
 204              switch($matches[2])
 205              {
 206                  case "k":
 207                      $memory_limit = ceil((($memory_limit+$thumbnail_memory) / 1024))."K";
 208                      break;
 209                  case "m":
 210                      $memory_limit = ceil((($memory_limit+$thumbnail_memory) / 1048576))."M";
 211                      break;
 212                  case "g":
 213                      $memory_limit = ceil((($memory_limit+$thumbnail_memory) / 1073741824))."G";
 214              }
 215          }
 216          
 217          @ini_set("memory_limit", $memory_limit);
 218      }
 219  }
 220  
 221  /**
 222   * Figures out the correct dimensions to use
 223   * 
 224   * @param integer current hight dimension
 225   * @param integer current width dimension
 226   * @param integer max hight dimension
 227   * @param integer max width dimension
 228   * @return array correct height & width
 229   */
 230  function scale_image($width, $height, $maxwidth, $maxheight)
 231  {
 232      $width = intval($width);
 233      $height = intval($height);
 234      
 235      if(!$width) $width = $maxwidth;
 236      if(!$height) $height = $maxheight;
 237      
 238      $newwidth = $width;
 239      $newheight = $height;
 240  
 241      if($width > $maxwidth)
 242      {
 243          $newwidth = $maxwidth;
 244          $newheight = ceil(($height*(($maxwidth*100)/$width))/100);
 245          $height = $newheight;
 246          $width = $newwidth;
 247      }
 248      if($height > $maxheight)
 249      {
 250          $newheight = $maxheight;
 251          $newwidth = ceil(($width*(($maxheight*100)/$height))/100);
 252      }
 253      $ret['width'] = $newwidth;
 254      $ret['height'] = $newheight;
 255      return $ret;
 256  }
 257  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1