Yii¿ò¼ÜʵÏÖͼƬÉÏ´«µÄ·½·¨Ïê½â

5年以前  |  阅读数:1093 次  |  编程语言:PHP 

±¾ÎÄʵÀý½²ÊoÁËYii¿o¼ÜʵÏÖͼƬÉÏ´«µÄ½¨¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º

½ñÌiÔÚÍøÉÏ¿´ÁËÏÂÓйØͼƬÉÏ´«µÄ½Ì³Ì£¬Àu¾­´iÕ۲ŵ÷ÊԺã¬ÏÖÔÚ°ÑÏa¹Ø´uÂe¼°Æa˵Ã÷Ìu³oÀ´£¬ÒÔ¹(C)³o´ÎʹÓõÄÅoÓÑÃDzο¼¡£

Model£º


    <?php
    class Upload extends CActiveRecord {
      public $image;
      public static function model($className = __CLASS__) {
        return $className;
      }
      public function tableName() {
        return '{{resource}}';
      }
      public function rules() {
        return array(
          array('image', 'file', 'types'=>'jpg, gif, png')
        );
      }
    }

×¢£ºresourceΪÊý¾Ý±i£¬±iǰ׺¿ÉÔÚmain.phpÄÚÉeÖã¬ÏaÐÅÅoÓÑÃÇÔÚ¿´µ½ÎļþÉÏ´«Ê±Ó¦¸ÃÊiϤÁËmain.phpλÖÃÔÚÄļ°ÔË×÷»uÖÆ¡£

Controller£º


    <?php
    class UploadController extends Controller {
      public function actionIndex() {
        $model=new Upload;
        if(isset($_POST['Upload'])) {
          $model->image=CUploadedFile::getInstance($model,'image');
          $ext = $model->image->getExtensionName();
          $fileName = uniqid() . '.' . $ext;
          $model->image->saveAs('assets/' . $fileName);
        }
        $this->renderPartial('index', array('model'=>$model));
      }
    }

×¢£ºsaveAsÀiÃaeÊÇ´aeÅͼƬÉÏ´«ºoµÄµØÖ£¬××ÙÏ´uÂe¿ÉÒÔ¢ÏÖ£¬¸Ã²ÎÊýÊÇmove_uploaded_fileº¯ÊýµÄµÚ¶þ¸o²ÎÊý£¬Ò»¶¨µÃÊÇÎļþÃu¡£

View£º


    <meta charset="utf-8">
    <?php echo CHtml::form(SITE_URL . 'admin/upload/index','post',array('enctype'=>'multipart/form-data')); ?>
    <?php echo CHtml::activeFileField($model, 'image'); ?>
    <?php echo CHtml::submitButton('Ìa½»');?>
    <?php echo CHtml::endForm(); ?>

×¢£ºÉÏÃaeµÄSITE_URLΪÏiÄ¿¶¨ÒaµÄ³£Á¿£¬Ò²¾ÍÊÇÏiÄ¿µÄÍøÖ*

ÏaО­¹ýÉÏÊo²½Öe£¬ÅoÓÑÃÇÓ¦¸Ã¿ÉÒÔÉÏ´«³É¹¦Í¼Æ¬£¬¶øÇÒÔÚÏiĿϵÄassetsĿ¼ÏÂÕÒµ½ÉÏ´«µÄͼƬ¡£ÒoΪ¢ÏÖyiiûÓÐËoÂÔͼµÄ½¨£¬ÓÚÊÇ°ÑthinkphpËoÂÔͼµÄ½¨ÕuºÏÁ˽øÀ´£¬°ÑÏÂÃae´uÂe±£´aeΪImage.phpÅÔÚÏiĿϵÄprotected/extensionsĿ¼ÏÂ


    <?php
    class Image extends CController {
      /**
       +----------------------------------------------------------
       * È¡µÃͼÏñÐÅÏ¢
       *
       +----------------------------------------------------------
       * @static
       * @access public
       +----------------------------------------------------------
       * @param string $image ͼÏñÎļþÃu
       +----------------------------------------------------------
       * @return mixed
       +----------------------------------------------------------
       */
      static function getImageInfo($img) {
        $imageInfo = getimagesize($img);
        if ($imageInfo !== false) {
          $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
          $imageSize = filesize($img);
          $info = array(
            "width" => $imageInfo[0],
            "height" => $imageInfo[1],
            "type" => $imageType,
            "size" => $imageSize,
            "mime" => $imageInfo['mime']
          );
          return $info;
        } else {
          return false;
        }
      }
      /**
       +----------------------------------------------------------
       * Éu³ÉËoÂÔͼ
       +----------------------------------------------------------
       * @static
       * @access public
       +----------------------------------------------------------
       * @param string $image ԭͼ
       * @param string $type ͼÏñ¸ñʽ
       * @param string $thumbname ËoÂÔͼÎļþÃu
       * @param string $maxWidth ¿i¶È
       * @param string $maxHeight ¸ß¶È
       * @param string $position ËoÂÔͼ±£´aeĿ¼
       * @param boolean $interlace ÆoÓøoÐÐɨÃe
       +----------------------------------------------------------
       * @return void
       +----------------------------------------------------------
       */
      static function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
        // »ñȡԭͼÐÅÏ¢
        $info = Image::getImageInfo($image);
        if ($info !== false) {
          $srcWidth = $info['width'];
          $srcHeight = $info['height'];
          $type = empty($type) ? $info['type'] : $type;
          $type = strtolower($type);
          $interlace = $interlace ? 1 : 0;
          unset($info);
          $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // ¼ÆËaËo*űÈÀý
          if ($scale >= 1) {
            // ³¬¹ýԭͼ´oС²»ÔÙËoÂÔ
            $width = $srcWidth;
            $height = $srcHeight;
          } else {
            // ËoÂÔͼ³ß´ç
            $width = (int) ($srcWidth * $scale);
            $height = (int) ($srcHeight * $scale);
          }
          // ÔØÈeԭͼ
          $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
          if(!function_exists($createFun)) {
            return false;
          }
          $srcImg = $createFun($image);
          //´´½¨ËoÂÔͼ
          if ($type != 'gif' && function_exists('imagecreatetruecolor'))
            $thumbImg = imagecreatetruecolor($width, $height);
          else
            $thumbImg = imagecreate($width, $height);
           //pngºÍgifµÄ͸Ã÷´¦Ài by luofei614
          if('png'==$type){
            imagealphablending($thumbImg, false);//È¡ÏuĬÈϵĻiɫģʽ£¨Îª½a¾oÒoӰΪÂÌÉ«µÄÎÊÌa£(C)
            imagesavealpha($thumbImg,true);//Ée¶¨±£´aeÍeÕuµÄ alpha ͨµÀÐÅÏ¢£¨Îª½a¾oÒoӰΪÂÌÉ«µÄÎÊÌa£(C)
          }elseif('gif'==$type){
            $trnprt_indx = imagecolortransparent($srcImg);
             if ($trnprt_indx >= 0) {
                //its transparent
                $trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);
                $trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($thumbImg, 0, 0, $trnprt_indx);
                imagecolortransparent($thumbImg, $trnprt_indx);
           }
          }
          // ¸´ÖÆͼƬ
          if (function_exists("ImageCopyResampled"))
            imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
          else
            imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
          // ¶ÔjpegͼÐÎÉeÖøoÐÐɨÃe
          if ('jpg' == $type || 'jpeg' == $type)
            imageinterlace($thumbImg, $interlace);
          // Éu³ÉͼƬ
          $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
          $imageFun($thumbImg, $thumbname);
          imagedestroy($thumbImg);
          imagedestroy($srcImg);
          return $thumbname;
        }
        return false;
      }
    }
    ?>

ÔÙÔÚÏiĿϵÄprotected/config/main.phpÖÐimport×ֶμÓÉÏ


    // autoloading model and component classes
      'import'=>array(
        'application.models.*',
        'application.components.*',
        'application.extensions.*',  #¼ÓÉÏ´ËÐУ¬Òa˼Ϊ×Ô¶¯ÔØÈe
      ),

ÔÙÉÏÃaeµÄController¼ÓÉÏ


    public function actionIndex() {
        $model=new Upload;
        if(isset($_POST['Upload'])) {
          $model->image=CUploadedFile::getInstance($model,'image');
          $ext = $model->image->getExtensionName();
          $fileName = uniqid() . '.' . $ext;
          $model->image->saveAs('assets/' . $fileName);
          // Éu³ÉËoÂÔͼ
          Image::thumb('assets/' . $fileName, 'assets/' . uniqid() . '.' . $ext);
        }
        $this->renderPartial('index', array('model'=>$model));
    }

Õa´Î¾ÍÍeÕuÁË¡£

¸u¶a¹ØÓÚYiiÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶Yii¿o¼ÜÈeÃż°³£Óü¼ÇÉ×ܽa¡¡¢¡¶[phpÓÅÐa¿ª¢¿o¼Ü×ܽa](http://www.jb51.net/Special/155.htm)¡¡¢¡¶smartyÄ£°aÈeÃÅ»u´¡½Ì³Ì¡¡¢¡¶phpÃaeÏo¶ÔÏo³ÌÐoÉe¼ÆÈeÃŽ̡̳¡¢¡¶[php×Öu´®(string)Óè×ܽa](http://www.jb51.net/Special/47.htm)¡¡¢¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡

Ï£Íu±¾ÎÄËuÊo¶Ô´o¼Ò»uÓÚYii¿o¼ÜµÄPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例