zoukankan      html  css  js  c++  java
  • [Yii Framework] A component for creating and getting the thumb image path

    There may be another good way to create and get the thumb image in Yii, here is my method to do it.

    1. You have to install this extension, "phpthumb", please search this extension in this website. :P

    2. Create a file named "ImageThumb.php", and locate it in protected/components , and insert the code as below:

    代码
    <?php
    /**
    */
    class ImageThumb extends CApplicationComponent
    {
    private $originalPath = "/upload/products/";
    private $thumbPath = "/upload/thumbs";

    public function createThumb($modelImage, $site="", $resizeW=100, $resizeH=100)
    {
    $oldImage = $modelImage;
    $modelImage = $this->_thumbName($modelImage, $site);
    $oldImageResite = $modelImage;

    if(!file_exists(Yii::getPathOfAlias("webroot").$this->originalPath.$oldImage))
    {
    $modelImage = "thumb.jpg";//You shall have a thumb.jpg image exists in path of $this->originalPath
    $oldImage = $modelImage;
    $modelImage = $this->_thumbName($modelImage, $site);
    $oldImageResite = $modelImage;
    }




    Yii
    ::app()->thumb->setThumbsDirectory($this->thumbPath);
    if(!file_exists(Yii::getPathOfAlias("webroot").$this->thumbPath.$oldImageResite))
    {
    Yii
    ::app()->thumb
    ->load(Yii::getPathOfAlias("webroot").$this->originalPath.$oldImage)
    ->resize($resizeW, $resizeH)
    ->save($modelImage, "JPG", "JPG");
    }
    return $modelImage;
    }




    private function _thumbName($modelImage, $site)
    {
    if(!empty($site))
    {
    $tem = explode(".", $modelImage);
    if(count($tem) > 1)
    $modelImage = $tem[0]."_".$site.".".$tem[1];//the file name starts with "m_", "s_", or null;
    }
    return $modelImage;
    }
    }

    3. Demo

    代码
    Yii::import("application.components.ImageThumb");
    $imageThumb = new ImageThumb();



    $imageOne
    = $imageThumb->createThumb("example.jpg", "", 580, 280);
    $imageTwo = $imageThumb->createThumb("example2.jpg", "s", 100, 70);


    Have fun with Yii. :)

  • 相关阅读:
    OLTP与OLAP
    Entity Framework(07):TPH、TPT、TPC与继承类
    Entity Framework(05):主从表的增删改
    Entity Framework(06):配置关系
    Entity Framework(04):主从表数据加载方式
    Entity Framework(03):Code First基础
    Entity Framework(02):模型优先 ,ObjectContext类
    Entity Framework(01):模型优先,Linq to Entities
    简介LINUX远程联机软件:PUTTY、SecureCRT、X-Manager
    php函数分割
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/1806818.html
Copyright © 2011-2022 走看看