Image.php

浏览该文件的文档。
00001 <?php
00003 // FleaPHP Framework
00004 //
00005 // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
00006 //
00007 // 许可协议,请查看源代码中附带的 LICENSE.txt 文件,
00008 // 或者访问 http://www.fleaphp.org/ 获得详细信息。
00010 
00032 class FLEA_Helper_Image
00033 {
00039     var $_handle = null;
00040 
00050     function FLEA_Helper_Image($handle)
00051     {
00052         $this->_handle = $handle;
00053     }
00054 
00070     function & createFromFile($filename, $fileext = null)
00071     {
00072         if (is_null($fileext)) {
00073             $fileext = pathinfo($filename, PATHINFO_EXTENSION);
00074         }
00075         $fileext = strtolower($fileext);
00076         $ext2functions = array(
00077             'jpg' => 'imagecreatefromjpeg',
00078             'jpeg' => 'imagecreatefromjpeg',
00079             'png' => 'imagecreatefrompng',
00080             'gif' => 'imagecreatefromgif',
00081         );
00082         if (!isset($ext2functions[$fileext])) {
00083             FLEA::loadClass('FLEA_Exception_NotImplemented');
00084             __THROW(new FLEA_Exception_NotImplemented('imagecreatefrom' . $fileext));
00085             return false;
00086         }
00087 
00088         $handle = $ext2functions[$fileext]($filename);
00089         $img =& new FLEA_Helper_Image($handle);
00090         return $img;
00091     }
00092 
00099     function resize($width, $height)
00100     {
00101         if (is_null($this->_handle)) { return; }
00102         $dest = imagecreatetruecolor($width, $height);
00103         imagecopyresized($dest, $this->_handle, 0, 0, 0, 0,
00104                 $width, $height, imagesx($this->_handle), imagesy($this->_handle));
00105         imagedestroy($this->_handle);
00106         $this->_handle = $dest;
00107     }
00108 
00115     function resampled($width, $height)
00116     {
00117         if (is_null($this->_handle)) { return; }
00118         $dest = imagecreatetruecolor($width, $height);
00119         imagecopyresampled($dest, $this->_handle, 0, 0, 0, 0,
00120                 $width, $height, imagesx($this->_handle), imagesy($this->_handle));
00121         imagedestroy($this->_handle);
00122         $this->_handle = $dest;
00123     }
00124 
00133     function resizeCanvas($width, $height, $pos = 'center', $bgcolor = '0xffffff')
00134     {
00135         if (is_null($this->_handle)) { return; }
00136         $dest = imagecreatetruecolor($width, $height);
00137         $sx = imagesx($this->_handle);
00138         $sy = imagesy($this->_handle);
00139 
00140         // 根据 pos 属性来决定如何定位原始图片
00141         switch (strtolower($pos)) {
00142         case 'left':
00143             $ox = 0;
00144             $oy = ($height - $sy) / 2;
00145             break;
00146         case 'right':
00147             $ox = $width - $sx;
00148             $oy = ($height - $sy) / 2;
00149             break;
00150         case 'top':
00151             $ox = ($width - $sx) / 2;
00152             $oy = 0;
00153             break;
00154         case 'bottom':
00155             $ox = ($width - $sx) / 2;
00156             $oy = $height - $sy;
00157             break;
00158         case 'top-left':
00159             $ox = $oy = 0;
00160             break;
00161         case 'top-right':
00162             $ox = $width - $sx;
00163             $oy = 0;
00164             break;
00165         case 'bottom-left':
00166             $ox = 0;
00167             $oy = $height - $sy;
00168             break;
00169         case 'bottom-right':
00170             $ox = $width - $sx;
00171             $oy = $height - $sy;
00172             break;
00173         default:
00174             $ox = ($width - $sx) / 2;
00175             $oy = ($height - $sy) / 2;
00176         }
00177 
00178         list($r, $g, $b) = $this->extractColor($bgcolor, '0xffffff');
00179         $bgcolor = imagecolorallocate($dest, $r, $g, $b);
00180         imagefilledrectangle($dest, 0, 0, $width, $height, $bgcolor);
00181         imagecolordeallocate($dest, $bgcolor);
00182 
00183         imagecopy($dest, $this->_handle, $ox, $oy, 0, 0, $sx, $sy);
00184         imagedestroy($this->_handle);
00185         $this->_handle = $dest;
00186     }
00187 
00196     function crop($width, $height, $highQuality = true, $nocut = null)
00197     {
00198         if (is_null($this->_handle)) { return; }
00199         $dest = imagecreatetruecolor($width, $height);
00200         $sx = imagesx($this->_handle);
00201         $sy = imagesy($this->_handle);
00202         $ratio = doubleval($width) / doubleval($sx);
00203 
00204         if (!is_array($nocut)) {
00205             if ($nocut) {
00206                 $nocut = array('enabled' => true, 'pos' => 'center', 'bgcolor' => '0xffffff');
00207             } else {
00208                 $nocut = array('enabled' => false);
00209             }
00210         } else {
00211             $nocut['enabled'] = isset($nocut['enabled']) ? $nocut['enabled']: true;
00212             $nocut['pos'] = isset($nocut['pos']) ? $nocut['pos']: 'center';
00213             $nocut['bgcolor'] = isset($nocut['bgcolor']) ? $nocut['bgcolor']: '0xffffff';
00214         }
00215 
00216         if ($nocut['enabled']) {
00217             // 求缩放后的最大宽度和高度
00218             if ($sy * $ratio > $height) {
00219                 $ratio = doubleval($height) / doubleval($sy);
00220             }
00221             $dx = $sx * $ratio;
00222             $dy = $sy * $ratio;
00223 
00224             // 根据 pos 属性来决定如何定位原始图片
00225             switch (strtolower($nocut['pos'])) {
00226             case 'left':
00227                 $ox = 0;
00228                 $oy = ($height - $sy * $ratio) / 2;
00229                 break;
00230             case 'right':
00231                 $ox = $width - $sx * $ratio;
00232                 $oy = ($height - $sy * $ratio) / 2;
00233                 break;
00234             case 'top':
00235                 $ox = ($width - $sx * $ratio) / 2;
00236                 $oy = 0;
00237                 break;
00238             case 'bottom':
00239                 $ox = ($width - $sx * $ratio) / 2;
00240                 $oy = $height - $sy * $ratio;
00241                 break;
00242             case 'top-left':
00243                 $ox = $oy = 0;
00244                 break;
00245             case 'top-right':
00246                 $ox = $width - $sx * $ratio;
00247                 $oy = 0;
00248                 break;
00249             case 'bottom-left':
00250                 $ox = 0;
00251                 $oy = $height - $sy * $ratio;
00252                 break;
00253             case 'bottom-right':
00254                 $ox = $width - $sx * $ratio;
00255                 $oy = $height - $sy * $ratio;
00256                 break;
00257             default:
00258                 $ox = ($width - $sx * $ratio) / 2;
00259                 $oy = ($height - $sy * $ratio) / 2;
00260             }
00261 
00262             list($r, $g, $b) = $this->extractColor($nocut['bgcolor'], '0xffffff');
00263             $bgcolor = imagecolorallocate($dest, $r, $g, $b);
00264             imagefilledrectangle($dest, 0, 0, $width, $height, $bgcolor);
00265             imagecolordeallocate($dest, $bgcolor);
00266 
00267             $args = array($dest, $this->_handle, $ox, $oy, 0, 0, $dx, $dy, $sx, $sy);
00268         } else {
00269             // 允许图像溢出
00270             if ($sy * $ratio < $height) {
00271                 // 当按照比例缩放后的图像高度小于要求的高度时,只有放弃原始图像右边的部分内容
00272                 $ratio = doubleval($sy) / doubleval($height);
00273                 $sx = $width * $ratio;
00274             } elseif ($sy * $ratio > $height) {
00275                 // 当按照比例缩放后的图像高度大于要求的高度时,只有放弃原始图像底部的部分内容
00276                 $ratio = doubleval($sx) / doubleval($width);
00277                 $sy = $height * $ratio;
00278             }
00279 
00280             $args = array($dest, $this->_handle, 0, 0, 0, 0, $width, $height, $sx, $sy);
00281         }
00282 
00283         if ($highQuality) {
00284             call_user_func_array('imagecopyresampled', $args);
00285         } else {
00286             call_user_func_array('imagecopyresized', $args);
00287         }
00288 
00289         imagedestroy($this->_handle);
00290         $this->_handle = $dest;
00291     }
00292 
00299     function saveAsJpeg($filename, $quality = 80)
00300     {
00301         imagejpeg($this->_handle, $filename, $quality);
00302     }
00303 
00309     function saveAsPng($filename)
00310     {
00311         imagepng($this->_handle, $filename);
00312     }
00313 
00319     function saveAsGif($filename)
00320     {
00321         imagegif($this->_handle, $filename);
00322     }
00323 
00327     function destory()
00328     {
00329         imagedestroy($this->_handle);
00330         $this->_handle = null;
00331     }
00332 
00341     function extractColor($color, $defualt = 'ffffff')
00342     {
00343         $color = strtolower($color);
00344         if (substr($color, 0, 2) == '0x') {
00345             $color = substr($color, 2);
00346         } elseif (substr($color, 0, 1) == '#') {
00347             $color = substr($color, 1);
00348         }
00349         $l = strlen($color);
00350         if ($l == 3) {
00351             $r = hexdec(substr($color, 0, 1));
00352             $g = hexdec(substr($color, 1, 1));
00353             $b = hexdec(substr($color, 2, 1));
00354             return array($r, $g, $b);
00355         } elseif ($l != 6) {
00356             $color = $defualt;
00357         }
00358 
00359         $r = hexdec(substr($color, 0, 2));
00360         $g = hexdec(substr($color, 2, 2));
00361         $b = hexdec(substr($color, 4, 2));
00362         return array($r, $g, $b);
00363     }
00364 }

Generated at Sat Feb 2 15:18:50 2008 for FleaPHP by  doxygen 1.5.3