Last active
December 15, 2015 12:59
-
-
Save picasso250/5263988 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// from file | |
function make_image2($imagefile, $opt = array()) | |
{ | |
// deault option | |
$opt = array_merge(array( | |
'crop' => 0, | |
'resize' => 0, | |
'width' => 50, | |
'height' => 50, | |
'list' => null, | |
), $opt); | |
$extention = $image_type = end(explode('.', $imagefile)); | |
$tmp_img = $imagefile; | |
return _make_image($tmp_img, $image_type, $extention, $opt); | |
} | |
function _make_image($tmp_img, $image_type, $extention, $opt) | |
{ | |
$resize = $opt['resize']; | |
$opt_list = $opt['list']; | |
if (!$opt_list) { | |
$opt_list = array($opt); | |
} | |
$ret = array(); | |
foreach ($opt_list as $opt_) { | |
if ($resize) { | |
$content = image_file_resize($tmp_img, $image_type, $opt_['crop'], $opt_['width'], $opt_['height']); | |
} else { | |
$content = file_get_contents($tmp_img); | |
} | |
$file_name = uniqid() . '.' . $extention; | |
$ret[] = write_upload($content, $file_name); | |
} | |
return count($ret) === 1 ? reset($ret) : $ret; | |
} | |
/** | |
* main function | |
* @param type $image is xx in $_FILES['xx'] | |
* @param type $opt resize crop width height | |
* @return string url of the final img | |
* @throws Exception | |
*/ | |
function make_image($image, $opt=array()) { | |
// default option | |
$opt = array_merge(array( | |
'crop' => 0, | |
'resize' => 0, | |
'width' => 50, | |
'height' => 50, | |
'list' => null, | |
), $opt); | |
$image = $_FILES[$image]; | |
$arr = explode('/', $image['type']); | |
$file_type = reset($arr); | |
$image_type = end($arr); | |
if ($file_type == 'image') { | |
$extention = file_ext($image['name']); | |
$tmp_img = $image['tmp_name']; | |
return _make_image($tmp_img, $image_type, $extention, $opt); | |
} else { // maybe throw?? | |
return ''; | |
} | |
} | |
// write file content to dst | |
function write_upload($content, $file_name) { | |
if (ON_SAE) { | |
$up_domain = UP_DOMAIN; | |
$s = new SaeStorage(); | |
$s->write($up_domain , $file_name , $content); | |
return $s->getUrl($up_domain ,$file_name); | |
} else { | |
$root = 'data/'; | |
if (!file_exists($root)) { | |
mkdir($root); | |
} | |
$dst_root = $root .'upload/'; | |
if (!file_exists($dst_root)) { | |
mkdir($dst_root); | |
} | |
$year_month_folder = date('Ym'); | |
$path = $year_month_folder; | |
if (!file_exists($dst_root.$path)) { | |
mkdir($dst_root.$path); | |
} | |
$date_folder = date('d'); | |
$path .= '/'.$date_folder; | |
if (!file_exists($dst_root.$path)) { | |
mkdir($dst_root.$path); | |
} | |
$path .= '/'.$file_name; | |
file_put_contents($dst_root.$path, $content); | |
return ROOT . 'data/upload/' . $path; | |
} | |
} | |
function file_ext($file_name) { | |
$arr = explode('.', $file_name); | |
if (count($arr) < 2) { | |
throw new Exception('bad file name: ' . $image['name']); | |
} | |
return end($arr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment