Chinaunix首页 | 论坛 | 博客
  • 博客访问: 69687
  • 博文数量: 18
  • 博客积分: 492
  • 博客等级: 下士
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-24 20:27
文章分类
文章存档

2011年(18)

分类: 系统运维

2011-07-26 10:23:07

  1. <?php
  2. /**
  3. * CImage +_+ PHP Version
  4. *
  5. * @author    aboys72
  6. * @date 2011/7/20
  7. */
  8. define('PIC_DIR', '/tmp/');
  9. class CImage
  10. {
  11.     const ERR_NO_FILE = 1;

  12.     protected $module_cfg = array(
  13.         'upload' => 'images/',
  14.     );

  15.     protected $uid;
  16.     protected $module;
  17.     protected $name_dst;
  18.     protected $file_type;
  19.     protected $hash_folder;
  20.     protected $w_src;
  21.     protected $h_src;
  22.     protected $prefix;
  23.     protected $save_path;
  24.     protected $msg_id = 0;
  25.     

  26.     public function __construct($module, $uid = 0)
  27.     {
  28.         if (!isset($this->module_cfg[$module])) {
  29.             die('No find module');
  30.         } else {
  31.             $this->module = $module;
  32.             $this->uid = $uid;
  33.             $this->save_path = PIC_DIR . $this->module_cfg[$module];
  34.         }
  35.     }

  36.     public function copyImage($src, $prefix = '')
  37.     {
  38.         if (!$this->checkFile($src)) {
  39.             return false;
  40.         }
  41.         $this->prefix = $prefix;
  42.         return copy($src, $this->getFullPath());
  43.     }

  44.     public function thumbImage($src, $w_dst = 0, $h_dst = 0, $prefix = 'thumb_', $w_max = 0, $h_max = 0, $quality = 80)
  45.     {
  46.         if ($this->name_dst == '' && $this->checkFile($src) == false) {
  47.             return false;
  48.         }
  49.         $this->prefix = $prefix;
  50.         $image = new Imagick($src);
  51.         if (empty($this->w_src) || empty($this->h_src)) {
  52.             $this->w_src = $image->getImageWidth();
  53.             $this->h_src = $image->getImageHeight();
  54.         }
  55.         list($width, $height) = $this->getThumbXY($w_dst, $h_dst);
  56.         $image->setcompressionquality($quality);
  57.         if ($image->getnumberimages() == 1) {
  58.             $image->scaleImage($width, $height);
  59.             $image->writeimage($this->getFullPath());
  60.         } else {
  61.             foreach ($image as $im) {
  62.                 $im->scaleImage($width, $height);
  63.             }
  64.             $image->writeimages($this->getFullPath(), true);
  65.         }
  66.         $image->clear();
  67.         $image->destroy();

  68.         if (!empty($h_max) && $h_max < $height) {
  69.             return $this->cropImage($this->getFullPath(), $width, $h_max, 0, 0, $prefix);
  70.         }
  71.         return true;
  72.     }

  73.     public function cropImage($src, $w_dst, $h_dst, $x = 0, $y = 0, $prefix = 'crop_')
  74.     {
  75.         if ($this->name_dst == '' && $this->checkFile($src) == false) {
  76.             return false;
  77.         }
  78.         $this->prefix = $prefix;
  79.         $image = new Imagick($src);
  80.         if ($image->getnumberimages() == 1) {
  81.             $image->cropimage($w_dst, $h_dst, $x, $y);
  82.             $image->writeimage($this->getFullPath());
  83.         } else {
  84.             foreach ($image as $im) {
  85.                 $im->cropimage($w_dst, $h_dst, $x, $y);
  86.             }
  87.             $image->writeimages($this->getFullPath(), true);
  88.         }
  89.         $image->clear();
  90.         $image->destroy();
  91.         return true;
  92.     }

  93.     protected function getFullPath()
  94.     {
  95.         return $this->save_path . $this->hash_folder . $this->prefix .$this->name_dst;
  96.     }

  97.     protected function checkFile($filename)
  98.     {
  99.         if (!file_exists($filename)) {
  100.             $this->msg_id = self::ERR_NO_FILE;
  101.             return false;
  102.         }
  103.         $image = new Imagick($filename);
  104.         $this->file_type = strtolower($image->getimageformat());
  105.         if ($this->file_type == 'jpeg') {
  106.             $this->file_type = 'jpg';
  107.         }
  108.         $this->w_src = $image->getimagewidth();
  109.         $this->h_src = $image->getimageheight();
  110.         $this->name_dst = $this->randName();
  111.         $this->hash_folder = $this->createHashDir();
  112.         $image->clear();
  113.         $image->destroy();
  114.         return true;
  115.     }

  116.     protected function randName()
  117.     {
  118.         return date('YmdHis') . rand(1000, 9999) . '.' . $this->file_type;
  119.     }

  120.     protected function createHashDir($number = 2)
  121.     {
  122.         $sha1_name = sha1($this->name_dst);
  123.         $sha1_dir = '';
  124.         for ($i = 0; $i < $number; $i++) {
  125.             $sha1_dir.= substr($sha1_name, $i * 2, 2) . '/';
  126.         }
  127.         $this->mkdirs($this->save_path . $sha1_dir);
  128.         return $sha1_dir;
  129.     }

  130.     protected function mkdirs($path, $mode = 0755)
  131.     {
  132.         if (is_dir($path)) {
  133.             return true;
  134.         }
  135.         $dirpath = dirname($path);
  136.         if (!file_exists($dirpath)) {
  137.             $this->mkdirs($dirpath, $mode);
  138.         }
  139.         @mkdir($path, $mode);
  140.         return is_dir($path);
  141.     }

  142.     protected function getThumbXY($w_dst, $h_dst)
  143.     {
  144.         $x = 0;
  145.         $y = 0;
  146.         if ($w_dst >= $this->w_src && $h_dst >= $this->h_src) {
  147.             $x = $this->w_src;
  148.             $y = $this->h_src;
  149.         } else if ($w_dst <= $this->w_src && empty($h_dst)) {
  150.             $x = $w_dst;
  151.             $y = round($w_dst / $this->w_src * $this->h_src);
  152.         } else if (empty($w_dst) && $h_dst <= $this->h_src) {
  153.             $x = round($h_dst / $this->h_src * $this->w_src);
  154.             $y = $h_dst;
  155.         } else if ($w_dst > 0 && $h_dst > 0) {
  156.             if ($w_dst / $this->w_src < $h_dst / $this->h_src) {
  157.                 $x = $w_dst;
  158.                 $y = $y = round($w_dst / $this->w_src * $this->h_src);
  159.             } else {
  160.                 $x = round($h_dst / $this->h_src * $this->w_src);
  161.                 $y = $h_dst;
  162.             }
  163.         } else {
  164.             $x = $this->w_src;
  165.             $y = $this->h_src;
  166.         }
  167.         return array($x, $y);
  168.     }

  169.     public function getHttpPicUrl()
  170.     {
  171.         return $this->getUrl();
  172.     }

  173.     public function getUrl()
  174.     {
  175.         return PIC_HOST_HTTP . '/' . $this->getPath();
  176.     }

  177.     public function getPath()
  178.     {
  179.         return substr($this->getFullPath(), strlen(PIC_DIR));
  180.     }


  181.     public function getMessage()
  182.     {
  183.         $errors = array(
  184.             self::ERR_NO_FILE => '上传文件不存在!',
  185.         );
  186.         return (isset($errors[$this->msg_id])) ? $errors[$this->msg_id] : null;
  187.     }

  188.     public function getRemoteFile($url)
  189.     {
  190.         $fileinfo = pathinfo($url);
  191.         $file = @file_get_contents($url);
  192.         if ($file == false) {
  193.             $this->msg_id = self::ERR_NO_FILE;
  194.             return false;
  195.         }
  196.         $this->file_type = $fileinfo['extension'];
  197.         $this->name_dst = $this->randName();
  198.         $this->hash_folder = $this->createHashDir();
  199.         file_put_contents($this->getFullPath(), $file);
  200.         return true;
  201.     }
  202. }
$ci = new CImage('upload');
$ci->copyImage($_FILES['name']['tmp_name']);
$url = $ci->getUrl();
阅读(1442) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~