- <?php
-
/**
-
* CImage +_+ PHP Version
-
*
-
* @author aboys72
-
* @date 2011/7/20
-
*/
-
define('PIC_DIR', '/tmp/');
-
class CImage
-
{
-
const ERR_NO_FILE = 1;
-
-
protected $module_cfg = array(
-
'upload' => 'images/',
-
);
-
-
protected $uid;
-
protected $module;
-
protected $name_dst;
-
protected $file_type;
-
protected $hash_folder;
-
protected $w_src;
-
protected $h_src;
-
protected $prefix;
-
protected $save_path;
-
protected $msg_id = 0;
-
-
-
public function __construct($module, $uid = 0)
-
{
-
if (!isset($this->module_cfg[$module])) {
-
die('No find module');
-
} else {
-
$this->module = $module;
-
$this->uid = $uid;
-
$this->save_path = PIC_DIR . $this->module_cfg[$module];
-
}
-
}
-
-
public function copyImage($src, $prefix = '')
-
{
-
if (!$this->checkFile($src)) {
-
return false;
-
}
-
$this->prefix = $prefix;
-
return copy($src, $this->getFullPath());
-
}
-
-
public function thumbImage($src, $w_dst = 0, $h_dst = 0, $prefix = 'thumb_', $w_max = 0, $h_max = 0, $quality = 80)
-
{
-
if ($this->name_dst == '' && $this->checkFile($src) == false) {
-
return false;
-
}
-
$this->prefix = $prefix;
-
$image = new Imagick($src);
-
if (empty($this->w_src) || empty($this->h_src)) {
-
$this->w_src = $image->getImageWidth();
-
$this->h_src = $image->getImageHeight();
-
}
-
list($width, $height) = $this->getThumbXY($w_dst, $h_dst);
-
$image->setcompressionquality($quality);
-
if ($image->getnumberimages() == 1) {
-
$image->scaleImage($width, $height);
-
$image->writeimage($this->getFullPath());
-
} else {
-
foreach ($image as $im) {
-
$im->scaleImage($width, $height);
-
}
-
$image->writeimages($this->getFullPath(), true);
-
}
-
$image->clear();
-
$image->destroy();
-
-
if (!empty($h_max) && $h_max < $height) {
-
return $this->cropImage($this->getFullPath(), $width, $h_max, 0, 0, $prefix);
-
}
-
return true;
-
}
-
-
public function cropImage($src, $w_dst, $h_dst, $x = 0, $y = 0, $prefix = 'crop_')
-
{
-
if ($this->name_dst == '' && $this->checkFile($src) == false) {
-
return false;
-
}
-
$this->prefix = $prefix;
-
$image = new Imagick($src);
-
if ($image->getnumberimages() == 1) {
-
$image->cropimage($w_dst, $h_dst, $x, $y);
-
$image->writeimage($this->getFullPath());
-
} else {
-
foreach ($image as $im) {
-
$im->cropimage($w_dst, $h_dst, $x, $y);
-
}
-
$image->writeimages($this->getFullPath(), true);
-
}
-
$image->clear();
-
$image->destroy();
-
return true;
-
}
-
-
protected function getFullPath()
-
{
-
return $this->save_path . $this->hash_folder . $this->prefix .$this->name_dst;
-
}
-
-
protected function checkFile($filename)
-
{
-
if (!file_exists($filename)) {
-
$this->msg_id = self::ERR_NO_FILE;
-
return false;
-
}
-
$image = new Imagick($filename);
-
$this->file_type = strtolower($image->getimageformat());
-
if ($this->file_type == 'jpeg') {
-
$this->file_type = 'jpg';
-
}
-
$this->w_src = $image->getimagewidth();
-
$this->h_src = $image->getimageheight();
-
$this->name_dst = $this->randName();
-
$this->hash_folder = $this->createHashDir();
-
$image->clear();
-
$image->destroy();
-
return true;
-
}
-
-
protected function randName()
-
{
-
return date('YmdHis') . rand(1000, 9999) . '.' . $this->file_type;
-
}
-
-
protected function createHashDir($number = 2)
-
{
-
$sha1_name = sha1($this->name_dst);
-
$sha1_dir = '';
-
for ($i = 0; $i < $number; $i++) {
-
$sha1_dir.= substr($sha1_name, $i * 2, 2) . '/';
-
}
-
$this->mkdirs($this->save_path . $sha1_dir);
-
return $sha1_dir;
-
}
-
-
protected function mkdirs($path, $mode = 0755)
-
{
-
if (is_dir($path)) {
-
return true;
-
}
-
$dirpath = dirname($path);
-
if (!file_exists($dirpath)) {
-
$this->mkdirs($dirpath, $mode);
-
}
-
@mkdir($path, $mode);
-
return is_dir($path);
-
}
-
-
protected function getThumbXY($w_dst, $h_dst)
-
{
-
$x = 0;
-
$y = 0;
-
if ($w_dst >= $this->w_src && $h_dst >= $this->h_src) {
-
$x = $this->w_src;
-
$y = $this->h_src;
-
} else if ($w_dst <= $this->w_src && empty($h_dst)) {
-
$x = $w_dst;
-
$y = round($w_dst / $this->w_src * $this->h_src);
-
} else if (empty($w_dst) && $h_dst <= $this->h_src) {
-
$x = round($h_dst / $this->h_src * $this->w_src);
-
$y = $h_dst;
-
} else if ($w_dst > 0 && $h_dst > 0) {
-
if ($w_dst / $this->w_src < $h_dst / $this->h_src) {
-
$x = $w_dst;
-
$y = $y = round($w_dst / $this->w_src * $this->h_src);
-
} else {
-
$x = round($h_dst / $this->h_src * $this->w_src);
-
$y = $h_dst;
-
}
-
} else {
-
$x = $this->w_src;
-
$y = $this->h_src;
-
}
-
return array($x, $y);
-
}
-
-
public function getHttpPicUrl()
-
{
-
return $this->getUrl();
-
}
-
-
public function getUrl()
-
{
-
return PIC_HOST_HTTP . '/' . $this->getPath();
-
}
-
-
public function getPath()
-
{
-
return substr($this->getFullPath(), strlen(PIC_DIR));
-
}
-
-
-
public function getMessage()
-
{
-
$errors = array(
-
self::ERR_NO_FILE => '上传文件不存在!',
-
);
-
return (isset($errors[$this->msg_id])) ? $errors[$this->msg_id] : null;
-
}
-
-
public function getRemoteFile($url)
-
{
-
$fileinfo = pathinfo($url);
-
$file = @file_get_contents($url);
-
if ($file == false) {
-
$this->msg_id = self::ERR_NO_FILE;
-
return false;
-
}
-
$this->file_type = $fileinfo['extension'];
-
$this->name_dst = $this->randName();
-
$this->hash_folder = $this->createHashDir();
-
file_put_contents($this->getFullPath(), $file);
-
return true;
-
}
-
}
$ci = new CImage('upload');
$ci->copyImage($_FILES['name']['tmp_name']);
$url = $ci->getUrl();