PageRenderTime 179ms CodeModel.GetById 152ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Examples/Rbac/Lib/ORG/Util/Image.class.php

http://thinkphp.googlecode.com/
PHP | 569 lines | 343 code | 30 blank | 196 comment | 86 complexity | 8847cd43b83af242013a9acc1f0c6db4 MD5 | raw file
  1<?php
  2
  3// +----------------------------------------------------------------------
  4// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  5// +----------------------------------------------------------------------
  6// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  7// +----------------------------------------------------------------------
  8// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  9// +----------------------------------------------------------------------
 10// | Author: liu21st <liu21st@gmail.com>
 11// +----------------------------------------------------------------------
 12// $Id: Image.class.php 2601 2012-01-15 04:59:14Z liu21st $
 13
 14/**
 15  +------------------------------------------------------------------------------
 16 * ??????
 17  +------------------------------------------------------------------------------
 18 * @category   ORG
 19 * @package  ORG
 20 * @subpackage  Util
 21 * @author    liu21st <liu21st@gmail.com>
 22 * @version   $Id: Image.class.php 2601 2012-01-15 04:59:14Z liu21st $
 23  +------------------------------------------------------------------------------
 24 */
 25class Image {
 26
 27    /**
 28      +----------------------------------------------------------
 29     * ??????
 30     *
 31      +----------------------------------------------------------
 32     * @static
 33     * @access public
 34      +----------------------------------------------------------
 35     * @param string $image ?????
 36      +----------------------------------------------------------
 37     * @return mixed
 38      +----------------------------------------------------------
 39     */
 40
 41    static function getImageInfo($img) {
 42        $imageInfo = getimagesize($img);
 43        if ($imageInfo !== false) {
 44            $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
 45            $imageSize = filesize($img);
 46            $info = array(
 47                "width" => $imageInfo[0],
 48                "height" => $imageInfo[1],
 49                "type" => $imageType,
 50                "size" => $imageSize,
 51                "mime" => $imageInfo['mime']
 52            );
 53            return $info;
 54        } else {
 55            return false;
 56        }
 57    }
 58
 59    /**
 60      +----------------------------------------------------------
 61     * ???????
 62      +----------------------------------------------------------
 63     * @static public
 64      +----------------------------------------------------------
 65     * @param string $source ????
 66     * @param string $water  ????
 67     * @param string $$savename  ?????????
 68     * @param string $alpha  ??????
 69      +----------------------------------------------------------
 70     * @return string
 71      +----------------------------------------------------------
 72     * @throws ThinkExecption
 73      +----------------------------------------------------------
 74     */
 75    static public function water($source, $water, $savename=null, $alpha=80) {
 76        //????????
 77        if (!file_exists($source) || !file_exists($water))
 78            return false;
 79
 80        //????
 81        $sInfo = self::getImageInfo($source);
 82        $wInfo = self::getImageInfo($water);
 83
 84        //????????????????
 85        if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height'])
 86            return false;
 87
 88        //????
 89        $sCreateFun = "imagecreatefrom" . $sInfo['type'];
 90        $sImage = $sCreateFun($source);
 91        $wCreateFun = "imagecreatefrom" . $wInfo['type'];
 92        $wImage = $wCreateFun($water);
 93
 94        //?????????
 95        imagealphablending($wImage, true);
 96
 97        //????,?????????
 98        $posY = $sInfo["height"] - $wInfo["height"];
 99        $posX = $sInfo["width"] - $wInfo["width"];
100
101        //??????
102        imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height'], $alpha);
103
104        //????
105        $ImageFun = 'Image' . $sInfo['type'];
106        //???????????????????
107        if (!$savename) {
108            $savename = $source;
109            @unlink($source);
110        }
111        //????
112        $ImageFun($sImage, $savename);
113        imagedestroy($sImage);
114    }
115
116    function showImg($imgFile, $text='', $x='10', $y='10', $alpha='50') {
117        //????????
118        //2007/6/26 ?????????$text??????????
119        $info = Image::getImageInfo($imgFile);
120        if ($info !== false) {
121            $createFun = str_replace('/', 'createfrom', $info['mime']);
122            $im = $createFun($imgFile);
123            if ($im) {
124                $ImageFun = str_replace('/', '', $info['mime']);
125                //????
126                if (!empty($text)) {
127                    $tc = imagecolorallocate($im, 0, 0, 0);
128                    if (is_file($text) && file_exists($text)) {//??$text???????
129                        // ??????
130                        $textInfo = Image::getImageInfo($text);
131                        $createFun2 = str_replace('/', 'createfrom', $textInfo['mime']);
132                        $waterMark = $createFun2($text);
133                        //$waterMark=imagecolorallocatealpha($text,255,255,0,50);
134                        $imgW = $info["width"];
135                        $imgH = $info["width"] * $textInfo["height"] / $textInfo["width"];
136                        //$y	=	($info["height"]-$textInfo["height"])/2;
137                        //?????????????????????
138                        imagecopymerge($im, $waterMark, $x, $y, 0, 0, $textInfo['width'], $textInfo['height'], $alpha);
139                    } else {
140                        imagestring($im, 80, $x, $y, $text, $tc);
141                    }
142                    //ImageDestroy($tc);
143                }
144                //????
145                if ($info['type'] == 'png' || $info['type'] == 'gif') {
146                    imagealphablending($im, FALSE); //?????????
147                    imagesavealpha($im, TRUE); //??????? alpha ????
148                }
149                Header("Content-type: " . $info['mime']);
150                $ImageFun($im);
151                @ImageDestroy($im);
152                return;
153            }
154
155            //????
156            $ImageFun($sImage, $savename);
157            imagedestroy($sImage);
158            //?????????????????PNG??
159            $im = imagecreatetruecolor(80, 30);
160            $bgc = imagecolorallocate($im, 255, 255, 255);
161            $tc = imagecolorallocate($im, 0, 0, 0);
162            imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
163            imagestring($im, 4, 5, 5, "no pic", $tc);
164            Image::output($im);
165            return;
166        }
167    }
168
169    /**
170      +----------------------------------------------------------
171     * ?????
172      +----------------------------------------------------------
173     * @static
174     * @access public
175      +----------------------------------------------------------
176     * @param string $image  ??
177     * @param string $type ????
178     * @param string $thumbname ??????
179     * @param string $maxWidth  ??
180     * @param string $maxHeight  ??
181     * @param string $position ???????
182     * @param boolean $interlace ??????
183      +----------------------------------------------------------
184     * @return void
185      +----------------------------------------------------------
186     */
187    static function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
188        // ??????
189        $info = Image::getImageInfo($image);
190        if ($info !== false) {
191            $srcWidth = $info['width'];
192            $srcHeight = $info['height'];
193            $type = empty($type) ? $info['type'] : $type;
194            $type = strtolower($type);
195            $interlace = $interlace ? 1 : 0;
196            unset($info);
197            $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // ??????
198            if ($scale >= 1) {
199                // ??????????
200                $width = $srcWidth;
201                $height = $srcHeight;
202            } else {
203                // ?????
204                $width = (int) ($srcWidth * $scale);
205                $height = (int) ($srcHeight * $scale);
206            }
207
208            // ????
209            $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
210            $srcImg = $createFun($image);
211
212            //?????
213            if ($type != 'gif' && function_exists('imagecreatetruecolor'))
214                $thumbImg = imagecreatetruecolor($width, $height);
215            else
216                $thumbImg = imagecreate($width, $height);
217
218            // ????
219            if (function_exists("ImageCopyResampled"))
220                imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
221            else
222                imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
223            if ('gif' == $type || 'png' == $type) {
224                //imagealphablending($thumbImg, false);//?????????
225                //imagesavealpha($thumbImg,true);//??????? alpha ????
226                $background_color = imagecolorallocate($thumbImg, 0, 255, 0);  //  ??????
227                imagecolortransparent($thumbImg, $background_color);  //  ????????????????????
228            }
229
230            // ?jpeg????????
231            if ('jpg' == $type || 'jpeg' == $type)
232                imageinterlace($thumbImg, $interlace);
233
234            //$gray=ImageColorAllocate($thumbImg,255,0,0);
235            //ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
236            // ????
237            $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
238            $imageFun($thumbImg, $thumbname);
239            imagedestroy($thumbImg);
240            imagedestroy($srcImg);
241            return $thumbname;
242        }
243        return false;
244    }
245
246    /**
247      +----------------------------------------------------------
248     * ????????????
249      +----------------------------------------------------------
250     * @static
251     * @access public
252      +----------------------------------------------------------
253     * @param string $string  ???
254     * @param string $size  ???? width,height ?? array(width,height)
255     * @param string $font  ???? fontface,fontsize ?? array(fontface,fontsize)
256     * @param string $type ???? ??PNG
257     * @param integer $disturb ???? 1 ??? 2 ??? 3 ???? 0 ???
258     * @param bool $border  ????? array(color)
259      +----------------------------------------------------------
260     * @return string
261      +----------------------------------------------------------
262     */
263    static function buildString($string, $rgb=array(), $filename='', $type='png', $disturb=1, $border=true) {
264        if (is_string($size))
265            $size = explode(',', $size);
266        $width = $size[0];
267        $height = $size[1];
268        if (is_string($font))
269            $font = explode(',', $font);
270        $fontface = $font[0];
271        $fontsize = $font[1];
272        $length = strlen($string);
273        $width = ($length * 9 + 10) > $width ? $length * 9 + 10 : $width;
274        $height = 22;
275        if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
276            $im = @imagecreatetruecolor($width, $height);
277        } else {
278            $im = @imagecreate($width, $height);
279        }
280        if (empty($rgb)) {
281            $color = imagecolorallocate($im, 102, 104, 104);
282        } else {
283            $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
284        }
285        $backColor = imagecolorallocate($im, 255, 255, 255);    //???????
286        $borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
287        $pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));                 //???
288
289        @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
290        @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
291        @imagestring($im, 5, 5, 3, $string, $color);
292        if (!empty($disturb)) {
293            // ????
294            if ($disturb = 1 || $disturb = 3) {
295                for ($i = 0; $i < 25; $i++) {
296                    imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
297                }
298            } elseif ($disturb = 2 || $disturb = 3) {
299                for ($i = 0; $i < 10; $i++) {
300                    imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $pointColor);
301                }
302            }
303        }
304        Image::output($im, $type, $filename);
305    }
306
307    /**
308      +----------------------------------------------------------
309     * ???????
310      +----------------------------------------------------------
311     * @static
312     * @access public
313      +----------------------------------------------------------
314     * @param string $length  ??
315     * @param string $mode  ??
316     * @param string $type ????
317     * @param string $width  ??
318     * @param string $height  ??
319      +----------------------------------------------------------
320     * @return string
321      +----------------------------------------------------------
322     */
323    static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify') {
324        import('@.ORG.Util.String');
325        $randval = String::randString($length, $mode);
326        $_SESSION[$verifyName] = md5($randval);
327        $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
328        if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
329            $im = imagecreatetruecolor($width, $height);
330        } else {
331            $im = imagecreate($width, $height);
332        }
333        $r = Array(225, 255, 255, 223);
334        $g = Array(225, 236, 237, 255);
335        $b = Array(225, 236, 166, 125);
336        $key = mt_rand(0, 3);
337
338        $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);    //???????
339        $borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
340        imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
341        imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
342        $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
343        // ??
344        for ($i = 0; $i < 10; $i++) {
345            imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);
346        }
347        for ($i = 0; $i < 25; $i++) {
348            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
349        }
350        for ($i = 0; $i < $length; $i++) {
351            imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
352        }
353        Image::output($im, $type);
354    }
355
356    // ?????
357    static function GBVerify($length=4, $type='png', $width=180, $height=50, $fontface='simhei.ttf', $verifyName='verify') {
358        import('ORG.Util.String');
359        $code = String::randString($length, 4);
360        $width = ($length * 45) > $width ? $length * 45 : $width;
361        $_SESSION[$verifyName] = md5($code);
362        $im = imagecreatetruecolor($width, $height);
363        $borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
364        $bkcolor = imagecolorallocate($im, 250, 250, 250);
365        imagefill($im, 0, 0, $bkcolor);
366        @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
367        // ??
368        for ($i = 0; $i < 15; $i++) {
369            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
370            imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
371        }
372        for ($i = 0; $i < 255; $i++) {
373            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
374            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $fontcolor);
375        }
376        if (!is_file($fontface)) {
377            $fontface = dirname(__FILE__) . "/" . $fontface;
378        }
379        for ($i = 0; $i < $length; $i++) {
380            $fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); //??????????????
381            $codex = String::msubstr($code, $i, 1);
382            imagettftext($im, mt_rand(16, 20), mt_rand(-60, 60), 40 * $i + 20, mt_rand(30, 35), $fontcolor, $fontface, $codex);
383        }
384        Image::output($im, $type);
385    }
386
387    /**
388      +----------------------------------------------------------
389     * ??????????
390      +----------------------------------------------------------
391     * @static
392     * @access public
393      +----------------------------------------------------------
394     * @param string $image  ??????
395     * @param string $type  ???????????
396      +----------------------------------------------------------
397     * @return string
398      +----------------------------------------------------------
399     */
400    static function showASCIIImg($image, $string='', $type='') {
401        $info = Image::getImageInfo($image);
402        if ($info !== false) {
403            $type = empty($type) ? $info['type'] : $type;
404            unset($info);
405            // ????
406            $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
407            $im = $createFun($image);
408            $dx = imagesx($im);
409            $dy = imagesy($im);
410            $i = 0;
411            $out = '<span style="padding:0px;margin:0;line-height:100%;font-size:1px;">';
412            set_time_limit(0);
413            for ($y = 0; $y < $dy; $y++) {
414                for ($x = 0; $x < $dx; $x++) {
415                    $col = imagecolorat($im, $x, $y);
416                    $rgb = imagecolorsforindex($im, $col);
417                    $str = empty($string) ? '*' : $string[$i++];
418                    $out .= sprintf('<span style="margin:0px;color:#%02x%02x%02x">' . $str . '</span>', $rgb['red'], $rgb['green'], $rgb['blue']);
419                }
420                $out .= "<br>\n";
421            }
422            $out .= '</span>';
423            imagedestroy($im);
424            return $out;
425        }
426        return false;
427    }
428
429    /**
430      +----------------------------------------------------------
431     * ?????????
432      +----------------------------------------------------------
433     * @static
434     * @access public
435      +----------------------------------------------------------
436     * @param string $type ????
437     * @param string $width  ??
438     * @param string $height  ??
439      +----------------------------------------------------------
440     * @return string
441      +----------------------------------------------------------
442     */
443    static function showAdvVerify($type='png', $width=180, $height=40, $verifyName='verifyCode') {
444        $rand = range('a', 'z');
445        shuffle($rand);
446        $verifyCode = array_slice($rand, 0, 10);
447        $letter = implode(" ", $verifyCode);
448        $_SESSION[$verifyName] = $verifyCode;
449        $im = imagecreate($width, $height);
450        $r = array(225, 255, 255, 223);
451        $g = array(225, 236, 237, 255);
452        $b = array(225, 236, 166, 125);
453        $key = mt_rand(0, 3);
454        $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);
455        $borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
456        imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
457        imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
458        $numberColor = imagecolorallocate($im, 255, rand(0, 100), rand(0, 100));
459        $stringColor = imagecolorallocate($im, rand(0, 100), rand(0, 100), 255);
460        // ????
461        /*
462          for($i=0;$i<10;$i++){
463          $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
464          imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
465          }
466          for($i=0;$i<255;$i++){
467          $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
468          imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
469          } */
470        imagestring($im, 5, 5, 1, "0 1 2 3 4 5 6 7 8 9", $numberColor);
471        imagestring($im, 5, 5, 20, $letter, $stringColor);
472        Image::output($im, $type);
473    }
474
475    /**
476      +----------------------------------------------------------
477     * ??UPC-A???
478      +----------------------------------------------------------
479     * @static
480      +----------------------------------------------------------
481     * @param string $type ????
482     * @param string $type ????
483     * @param string $lw  ????
484     * @param string $hi   ????
485      +----------------------------------------------------------
486     * @return string
487      +----------------------------------------------------------
488     */
489    static function UPCA($code, $type='png', $lw=2, $hi=100) {
490        static $Lencode = array('0001101', '0011001', '0010011', '0111101', '0100011',
491    '0110001', '0101111', '0111011', '0110111', '0001011');
492        static $Rencode = array('1110010', '1100110', '1101100', '1000010', '1011100',
493    '1001110', '1010000', '1000100', '1001000', '1110100');
494        $ends = '101';
495        $center = '01010';
496        /* UPC-A Must be 11 digits, we compute the checksum. */
497        if (strlen($code) != 11) {
498            die("UPC-A Must be 11 digits.");
499        }
500        /* Compute the EAN-13 Checksum digit */
501        $ncode = '0' . $code;
502        $even = 0;
503        $odd = 0;
504        for ($x = 0; $x < 12; $x++) {
505            if ($x % 2) {
506                $odd += $ncode[$x];
507            } else {
508                $even += $ncode[$x];
509            }
510        }
511        $code.= ( 10 - (($odd * 3 + $even) % 10)) % 10;
512        /* Create the bar encoding using a binary string */
513        $bars = $ends;
514        $bars.=$Lencode[$code[0]];
515        for ($x = 1; $x < 6; $x++) {
516            $bars.=$Lencode[$code[$x]];
517        }
518        $bars.=$center;
519        for ($x = 6; $x < 12; $x++) {
520            $bars.=$Rencode[$code[$x]];
521        }
522        $bars.=$ends;
523        /* Generate the Barcode Image */
524        if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
525            $im = imagecreatetruecolor($lw * 95 + 30, $hi + 30);
526        } else {
527            $im = imagecreate($lw * 95 + 30, $hi + 30);
528        }
529        $fg = ImageColorAllocate($im, 0, 0, 0);
530        $bg = ImageColorAllocate($im, 255, 255, 255);
531        ImageFilledRectangle($im, 0, 0, $lw * 95 + 30, $hi + 30, $bg);
532        $shift = 10;
533        for ($x = 0; $x < strlen($bars); $x++) {
534            if (($x < 10) || ($x >= 45 && $x < 50) || ($x >= 85)) {
535                $sh = 10;
536            } else {
537                $sh = 0;
538            }
539            if ($bars[$x] == '1') {
540                $color = $fg;
541            } else {
542                $color = $bg;
543            }
544            ImageFilledRectangle($im, ($x * $lw) + 15, 5, ($x + 1) * $lw + 14, $hi + 5 + $sh, $color);
545        }
546        /* Add the Human Readable Label */
547        ImageString($im, 4, 5, $hi - 5, $code[0], $fg);
548        for ($x = 0; $x < 5; $x++) {
549            ImageString($im, 5, $lw * (13 + $x * 6) + 15, $hi + 5, $code[$x + 1], $fg);
550            ImageString($im, 5, $lw * (53 + $x * 6) + 15, $hi + 5, $code[$x + 6], $fg);
551        }
552        ImageString($im, 4, $lw * 95 + 17, $hi - 5, $code[11], $fg);
553        /* Output the Header and Content. */
554        Image::output($im, $type);
555    }
556
557    static function output($im, $type='png', $filename='') {
558        ob_clean(); //????'????????????'???
559        header("Content-type: image/" . $type);
560        $ImageFun = 'image' . $type;
561        if (empty($filename)) {
562            $ImageFun($im);
563        } else {
564            $ImageFun($im, $filename);
565        }
566        imagedestroy($im);
567    }
568
569}