/** * 配置好图片的预览 * @param nulll * @return string html预览页面 */ function img_preview() { $size = $_GET['size'] ? $_GET['size'] : 'L'; $setting = $_POST['pic'][$size]; $w = $setting['width']; $h = $setting['height']; $storager = new base_storager(); $mdl_img = $this->app->model('image'); $img_row = $mdl_img->dump($setting['default_image']); $tmp_image_id = $mdl_img->gen_id(); if ($setting['wm_type'] == 'text' && $setting['wm_text']) { $url = 'http://chart.apis.google.com/chart?chst=d_text_outline&chld=000000|20|h|ffffff|_|' . urlencode($setting['wm_text']); if (constant("ECAE_MODE")) { $tmp_water_file = tempnam(sys_get_temp_dir(), 'img'); } else { $tmp_water_file = tempnam(DATA_DIR, 'img'); } file_put_contents($tmp_water_file, file_get_contents($url)); $setting['wm_text_preview'] = true; $setting['wm_text_image'] = $tmp_water_file; } if ($img_row['storage'] == 'network') { if (constant("ECAE_MODE")) { $tmp_file = tempnam(sys_get_temp_dir(), 'img'); } else { $tmp_file = tempnam(DATA_DIR, 'img'); } file_put_contents($tmp_file, file_get_contents($img_row['url'])); @unlink($tmpfile); } else { $tmp_file = $storager->worker->getFile($img_row['ident'], 'image'); } if (constant("ECAE_MODE")) { $tmp_target = tempnam(sys_get_temp_dir(), 'img'); } else { $tmp_target = tempnam(DATA_DIR, 'img'); } image_clip::image_resize($mdl_img, $tmp_file, $tmp_target, $w, $h); if ($setting['wm_type'] != 'none' && ($setting['wm_text'] || $setting['wm_image'])) { image_clip::image_watermark($mdl_img, $tmp_target, $setting); } $type = getimagesize($tmp_target); if (file_exists($tmp_water_file)) { unlink($tmp_water_file); } header("Content-Type: {$type[mime]}"); readfile($tmp_target); @unlink($tmp_target); }
/** * 给图片打水印的接口 * @param string image_id唯一标识 * @param string size规格类型 * @param boolean 是否打水印 * @return null */ function rebuild($image_id, $sizes, $watermark = true) { $storager = new base_storager(); if ($sizes) { $cur_image_set = $this->app->getConf('image.set'); $allsize = $this->app->getConf('image.default.set'); $this->watermark_define = array(); $this->watermark_default = ''; if (constant("ECAE_MODE")) { $tmp_target = tempnam(sys_get_temp_dir(), 'img'); } else { $tmp_target = tempnam(DATA_DIR, 'img'); } $img = $this->dump($image_id); if (is_array($img)) { $org_file = $img['url']; } if (substr($org_file, 0, 4) == 'http') { if ($img['storage'] == 'network') { $response = kernel::single('base_httpclient')->get($org_file); if ($response === false) { $data = array('image_id' => $image_id, 'last_modified' => time()); parent::save($data); return true; } $image_content = $response; } else { $image_file = $storager->worker->getFile($img['ident'], 'image'); if (!$image_file) { return false; } $image_content = file_get_contents($image_file); } if (constant("ECAE_MODE")) { $org_file = tempnam(sys_get_temp_dir(), 'imgorg'); } else { $org_file = tempnam(DATA_DIR, 'imgorg'); } file_put_contents($org_file, $image_content); } if (!file_exists($org_file)) { $data = array('image_id' => $image_id, 'last_modified' => time()); // parent::save($data); return true; } foreach ($sizes as $s) { if (isset($allsize[$s])) { $w = $cur_image_set[$s]['width']; $h = $cur_image_set[$s]['height']; $wh = $allsize[$s]['height']; $wd = $allsize[$s]['width']; $w = $w ? $w : $wd; $h = $h ? $h : $wh; image_clip::image_resize($this, $org_file, $tmp_target, $w, $h); if ($watermark && $cur_image_set[$s]['wm_type'] != 'none' && ($cur_image_set[$s]['wm_text'] || $cur_image_set[$s]['wm_image'])) { $watermark = true; image_clip::image_watermark($this, $tmp_target, $cur_image_set[$s]); } $this->store($tmp_target, $image_id, $s, null, $watermark); /** 删除指定规格图片 **/ @unlink(ROOT_DIR . '/' . $img[strtolower($s) . '_url']); } } @unlink($tmp_target); if (strpos('imgorg', $org_file) !== false) { @unlink($org_file); } } }
/** * 商品图片相册图片生成 * * @param $ident 需要生成相册图片唯一值 * @param $sizes 生成图片大小 * * @return bool */ public function rebuild($ident, $sizes) { if (!$sizes) { $imageSetParams = app::get('image')->getConf('image.set'); $allsize = app::get('image')->getConf('image.default.set'); foreach ($allsize as $s => $value) { if (!isset($allsize[$s])) { break; } $w = $imageSetParams[$s]['width']; $h = $imageSetParams[$s]['height']; $wh = $allsize[$s]['height']; $wd = $allsize[$s]['width']; $sizes[$s]['width'] = $w ? $w : $wd; $sizes[$s]['height'] = $h ? $h : $wh; } } $storager = kernel::single('base_storager'); $orgFile = $storager->getFile($ident); if (!file_exists($orgFile) || !$sizes) { return true; } foreach ($sizes as $s => $value) { $tmpTarget = tempnam(TMP_DIR, 'img'); $w = $value['width']; $h = $value['height']; $orgFileSize = getimagesize($orgFile); if ($orgFileSize['0'] < $w) { $w = $orgFileSize['0']; } if ($orgFileSize['1'] < $h) { $h = $orgFileSize['1']; } image_clip::image_resize($orgFile, $tmpTarget, $w, $h); $imageParams = getimagesize($tmpTarget); $size = filesize($tmpTarget); $fileObject = new UploadedFile($tmpTarget, $images['image_name'], $imageParams['mime'], $size, 0, true); $storager->rebuild($fileObject, strtolower($s), $ident); unlink($tmpTarget); } return true; }
function rebuild($image_id, $sizes, $watermark = true) { if ($sizes) { $cur_image_set = $this->app->getConf('image.set'); $allsize = $this->app->getConf('image.default.set'); $this->watermark_define = array(); $this->watermark_default = ''; $tmp_target = tempnam('/tmp', 'img'); $img = $this->dump($image_id); if (is_array($img)) { $org_file = $img['url']; } if (substr($org_file, 0, 4) == 'http') { $response = kernel::single('base_http')->action('get', $org_file); if ($response === false) { $data = array('image_id' => $image_id, 'last_modified' => time()); parent::save($data); return true; } $org_file = tempnam('/tmp', 'imgorg'); file_put_contents($org_file, file_get_contents($img['url'])); } if (!file_exists($org_file)) { $data = array('image_id' => $image_id, 'last_modified' => time()); parent::save($data); return true; } foreach ($sizes as $s) { if (isset($allsize[$s])) { $w = $cur_image_set[$s]['width']; $h = $cur_image_set[$s]['height']; $wh = $allsize[$s]['height']; $wd = $allsize[$s]['width']; $w = $w ? $w : $wd; $h = $h ? $h : $wh; image_clip::image_resize($this, $org_file, $tmp_target, $w, $h); if ($watermark && $cur_image_set[$s]['wm_type'] != 'none' && ($cur_image_set[$s]['wm_text'] || $cur_image_set[$s]['wm_image'])) { image_clip::image_watermark($this, $tmp_target, $cur_image_set[$s]); } $this->store($tmp_target, $image_id, $s); // error_log($tmp_target,3,'d:\text.txt'); } } unlink($tmp_target); } }
/** * 缩略图、水印. * * @param string image_id唯一标识 * @param string size规格类型 * @param bool 是否打水印 */ public function rebuild($image_id, $sizes, $watermark = false) { $watermark = false; //暂时禁用水印先 $storager = new base_storager(); if ($sizes) { $size_conf = $this->app->getConf('size'); $img = $this->dump($image_id); if (is_array($img)) { $org_file = $img['url']; } if ($org_file && method_exists($storager->worker, 'image_rebuild')) { foreach ($sizes as $s) { $re_url = $storager->worker->image_rebuild($org_file, $s); if (!$re_url) { return false; } $u_data = array('image_id' => $image_id, strtolower($s) . '_url' => $re_url); parent::save($u_data); } return true; } $tmp_target = tempnam(TMP_DIR, 'img'); if (substr($org_file, 0, 4) == 'http') { if ($img['storage'] == 'network') { $response = vmc::singleton('base_httpclient')->get($org_file); if ($response === false) { $data = array('image_id' => $image_id, 'last_modified' => time()); parent::save($data); return true; } $image_content = $response; } else { $image_file = $storager->worker->getFile($img['ident'], 'image'); if (!$image_file) { return false; } $image_content = file_get_contents($image_file); } $org_file = tempnam(TMP_DIR, 'imgorg'); file_put_contents($org_file, $image_content); } if (!file_exists($org_file)) { $data = array('image_id' => $image_id, 'last_modified' => time()); @unlink($tmp_target); // parent::save($data); return true; } foreach ($sizes as $s) { if (isset($size_conf[$s])) { $w = $size_conf[$s]['width']; $h = $size_conf[$s]['height']; if (!$w || !$h) { continue; } image_clip::image_resize($this, $org_file, $tmp_target, $w, $h); $this->store($tmp_target, $image_id, $s, null, $watermark); /* 删除指定规格图片 **/ @unlink(ROOT_DIR . '/' . $img[strtolower($s) . '_url']); } } @unlink($tmp_target); if (strpos($org_file, 'imgorg') !== false) { @unlink($org_file); } } }
/** * 配置好图片的预览 * @param nulll * @return string html预览页面 */ function img_preview() { $size = $_GET['size'] ? $_GET['size'] : 'L'; $setting = $_POST['pic'][$size]; $w = $setting['width']; $h = $setting['height']; $storager = new base_storager(); $mdl_img = $this->app->model('image'); $img_row = $mdl_img->dump($setting['default_image']); $tmp_image_id = $mdl_img->gen_id(); if ($setting['wm_type'] == 'text' && $setting['wm_text']) { //$url = 'http://chart.apis.google.com/chart?chst=d_text_outline&chld=000000|20|h|ffffff|_|'.urlencode($setting['wm_text']); //file_put_contents($tmp_water_file,file_get_contents($url)); if (!function_exists('imagettftext')) { trigger_error('gd函数库的版本过低,请配置高于2.0.28的版本', E_USER_NOTICE); echo "Notice:gd函数库的版本过低,请配置高于2.0.28的版本"; exit; } #生成文字图片 $tmp_water_file = TMP_DIR . "/img" . time() . ".png"; $fontfile = PUBLIC_DIR . "/app/" . $this->app->app_id . "/statics/msyh.ttf"; $img = imagecreatetruecolor(120, 100); $color = imagecolorallocate($img, 255, 255, 255); imagecolortransparent($img, $color); imagefill($img, 0, 0, $color); $textcolor = imagecolorallocate($img, 0, 0, 0); imagettftext($img, 16, 0, 0, 50, $textcolor, $fontfile, $setting['wm_text']); imagesavealpha($img, true); imagepng($img, $tmp_water_file); imagedestroy($img); $setting['wm_text_preview'] = true; $setting['wm_text_image'] = $tmp_water_file; } if ($img_row['storage'] == 'network') { $tmp_file = tempnam(TMP_DIR, 'img'); file_put_contents($tmp_file, file_get_contents($img_row['url'])); } else { $tmp_file = $storager->worker->getFile($img_row['ident'], 'image'); } $tmp_target = tempnam(TMP_DIR, 'img'); image_clip::image_resize($mdl_img, $tmp_file, $tmp_target, $w, $h); if ($setting['wm_type'] != 'none' && ($setting['wm_text'] || $setting['wm_image'])) { image_clip::image_watermark($mdl_img, $tmp_target, $setting); } $type = getimagesize($tmp_target); header("Content-Type: {$type[mime]}"); readfile($tmp_target); }
/** * 配置好图片的预览 * @param nulll * @return string html预览页面 */ function img_preview() { $size = $_GET['size'] ? $_GET['size'] : 'L'; $setting = $_POST['pic'][$size]; $w = $setting['width']; $h = $setting['height']; $storager = new base_storager(); $mdl_img = $this->app->model('image'); $img_row = $mdl_img->dump($setting['default_image']); $tmp_image_id = $mdl_img->gen_id(); if ($setting['wm_type'] == 'text' && $setting['wm_text']) { if (!function_exists('imagettftext')) { trigger_error('gd函数库的版本过低,请配置高于2.0.28的版本', E_USER_NOTICE); echo "Notice:gd函数库的版本过低,请配置高于2.0.28的版本"; exit; } #生成文字图片 //$tmp_water_file = TMP_DIR."/img".time().".png"; $tmp_water_file = tempnam(TMP_DIR, 'img') . time() . ".png"; $fontfile = PUBLIC_DIR . "/app/" . app::get('image')->app_id . "/statics/msyh.ttf"; $img = imagecreatetruecolor(120, 100); $color = imagecolorallocate($img, 255, 255, 255); imagecolortransparent($img, $color); imagefill($img, 0, 0, $color); $textcolor = imagecolorallocate($img, 0, 0, 0); imagettftext($img, 16, 0, 0, 50, $textcolor, $fontfile, $setting['wm_text']); imagesavealpha($img, true); imagepng($img, $tmp_water_file); imagedestroy($img); $setting['wm_text_preview'] = true; $setting['wm_text_image'] = $tmp_water_file; } $objLibImage = kernel::single('image_data_image'); $tmp_file = $objLibImage->fetch($img_row['image_id']); $tmp_target = tempnam(TMP_DIR, 'img'); image_clip::image_resize($tmp_file, $tmp_target, $w, $h); if ($setting['wm_type'] != 'none' && ($setting['wm_text'] || $setting['wm_image'])) { image_clip::image_watermark($objLibImage, $tmp_target, $setting); } unlink($tmp_water_file); $type = getimagesize($tmp_target); header("Content-Type: {$type[mime]}"); readfile($tmp_target); }