public function createFile($water_id, $app_bundle, $filepath, $filename, $bs = "") { $imgdir = hg_getimg_dir($bs); $filepath = $imgdir . app_to_dir($app_bundle) . $filepath; $exttmp = explode('.', $filename); $json_file = $filepath . $exttmp[0] . '.json'; $json = array(); if (file_exists($json_file)) { $json = json_decode(file_get_contents($json_file), true); } $sql = "SELECT * FROM " . DB_PREFIX . "water_config WHERE id=" . $water_id; $ret = $this->db->query_first($sql); $json['water'] = array('type' => $ret['type'], 'filename' => $ret['filename'], 'opacity' => $ret['opacity'], 'position' => $ret['position'], 'water_text' => $ret['water_text'], 'water_font' => $ret['water_font'], 'font_size' => $ret['font_size'], 'water_color' => $ret['water_color'], 'water_angle' => $ret['water_angle'], 'margin_x' => $ret['margin_x'], 'margin_y' => $ret['margin_y'], 'condition_x' => $ret['condition_x'], 'condition_y' => $ret['condition_y']); hg_file_write($json_file, json_encode($json)); copy($filepath . $filename, $filepath . 'nowater_' . $filename); include_once ROOT_PATH . 'lib/class/gdimage.php'; $img = new GDImage(); if ($json['water']['type'] == 1) { $json['water']['water_file_path'] = $json['water']['filename'] ? hg_getimg_default_dir() . WATER_PATH . $json['water']['filename'] : ''; //根据图片大小和图片水印比例调整水印图片大小 if ($this->settings['image_water_ratio'] && $json['water']['water_file_path']) { $img_info = getimagesize($filepath . $filename); $waterimg_info = getimagesize($json['water']['water_file_path']); if ($img_info[0] / $waterimg_info[0] < $this->settings['image_water_ratio']) { $new_width = abs(intval($img_info[0] / $this->settings['image_water_ratio'])); hg_mk_images($json['water']['water_file_path'], $json['water']['filename'], hg_getimg_default_dir() . WATER_PATH . $new_width . '/', array('width' => $new_width, 'height' => ''), array()); $json['water']['water_file_path'] = hg_getimg_default_dir() . WATER_PATH . $new_width . '/' . $json['water']['filename']; } } $img->waterimg($filepath . $filename, $json['water']); } else { $json['water']['water_font'] = $json['water']['water_font'] ? CUR_CONF_PATH . 'font/' . $json['water']['water_font'] : CUR_CONF_PATH . 'font/arial.ttf'; $json['water']['font_size'] = $json['water']['font_size'] ? $json['water']['font_size'] : 14; $img->waterstr($filepath . $filename, $json['water']); } return true; }
/** * 创建图片 * @param $uploadedfile 需生成的图片路径 * @param $name 生成后的返回的图片名称 * @param $path 需要存放的图片上级目录 * @param $size 缩略图的尺寸(array) * @param $max_pixel 尺寸的最大值(M) * @param $force 当文件存在时是否强制重新生成 * return 生成后的图片名 */ function hg_mk_images($uploadedfile, $name, $path, $size, $water, $max_pixel = 100, $force = 0) { $special = ''; if (!file_exists($uploadedfile) || !$name || !$path || !is_array($size)) { return false; } include_once ROOT_PATH . 'lib/class/gdimage.php'; //源文件 if (filesize($uploadedfile) / 1024 / 1024 >= $max_pixel) { return false; } $image = getimagesize($uploadedfile); $width = $image[0]; $height = $image[1]; //从 JPEG 或 TIFF 文件中读取 EXIF 头信息 相机照片缩略图旋转问题 if ($image[2] == 2) { @($exif = exif_read_data($uploadedfile)); if (!empty($exif['Orientation'])) { switch ($exif['Orientation']) { case 8: $tmp = $width; $width = $height; $height = $tmp; break; case 3: break; case 6: $tmp = $width; $width = $height; $height = $tmp; break; } } } //从 JPEG 或 TIFF 文件中读取 EXIF 头信息 相机照片缩略图旋转问题 $file_name = $name; //目录 $file_dir = $path; //文件路径 $file_path = $uploadedfile; $img = new GDImage(); if ($size['label']) { $new_name = $file_name; $other_dir = ""; if (empty($size['other_dir'])) { $other_dir = date('Y') . '/' . date('m') . '/'; } else { $other_dir = $size['other_dir']; } hg_mkdir($file_dir . $size['label'] . '/' . $other_dir); $save_file_path = $file_dir . $size['label'] . '/' . $other_dir . $new_name; } else { hg_mkdir($file_dir); $save_file_path = $file_dir . $file_name; } if (!file_exists($save_file_path) || $force) { $img->init_setting($file_path, $save_file_path); if ($size['height'] == 1) { if ($width > $height) { if ($size['width']) { $img->maxWidth = $width > $size['width'] ? $size['width'] : $width; $img->maxHeight = $height * ($img->maxWidth / $width); } } else { if ($size['width']) { $img->maxHeight = $height > $size['width'] ? $size['width'] : $height; $img->maxWidth = $width * ($img->maxHeight / $height); } } } else { if (empty($size['width'])) { $img->maxHeight = $height > $size['height'] ? $size['height'] : $height; $img->maxWidth = $width * ($img->maxHeight / $height); } else { if ($width > $height) { if ($size['width']) { $img->maxWidth = $width > $size['width'] ? $size['width'] : $width; if ($size['height']) { $img->maxHeight = $height > $size['height'] ? $size['height'] : $height; } else { $img->maxHeight = $height * ($img->maxWidth / $width); } } } else { if ($size['height']) { if ($height / $width > 1.2 && $size['width'] / $size['height'] > 1.2) { $special = 1; //$img->maxHeight = $height > $size['height'] ? $size['height'] : $height; //$img->maxWidth = $width * ($img->maxHeight/$height); $img->maxHeight = $height > $size['height'] ? $size['height'] : $height; $img->maxWidth = $width > $size['width'] ? $size['width'] : $width; } else { $img->maxHeight = $height > $size['height'] ? $size['height'] : $height; $img->maxWidth = $width > $size['width'] ? $size['width'] : $width; } } else { $img->maxWidth = $width > $size['width'] ? $size['width'] : $width; $img->maxHeight = $height * ($img->maxWidth / $width); } } } } $isSucc = $img->makeThumb(3, FALSE); if (!$isSucc) { return false; } if ($special) { //hg_add_backgroud($save_file_path, $size); } } if (!empty($water)) { if ($water['type'] == 1) { $water['water_file_path'] = $water['filename'] ? hg_getimg_default_dir() . WATER_PATH . $water['filename'] : ''; $img->waterimg($save_file_path, $water); } else { $water['water_font'] = $water['water_font'] ? CUR_CONF_PATH . 'font/' . $water['water_font'] : CUR_CONF_PATH . 'font/arial.ttf'; $water['font_size'] = $water['font_size'] ? $water['font_size'] : 14; $img->waterstr($save_file_path, $water); } } return $file_name; }