/** * 生成缩略图的方法 * @param strint 原图地址 * @return array 返回成功或错误信息 * 返回值为数组各字段含义,error:是否出错,1出错,0正常,errorcode:报错代码,path:缩略图片路径 */ public function createthumb($thumb_src_image) { global $_M; $thumb_src_image = path_absolute($thumb_src_image); if ($this->thumb_save_type == 1) { $thumb_savepath = dirname($thumb_src_image) . '/' . $this->thumb_savepath; } if ($this->thumb_save_type == 2) { $thumb_savepath = dirname($thumb_src_image) . '/'; } if ($this->thumb_save_type == 3) { $thumb_savepath = $this->thumb_savepath; } if (stristr(PHP_OS, "WIN")) { $thumb_src_image = @iconv("utf-8", "GBK", $thumb_src_image); } if (!file_exists($thumb_src_image) || is_dir($thumb_src_image)) { return $this->error($_M['word']['batchtips30']); } $this->thumb_width = $this->thumb_width ? $this->thumb_width : 100; $this->thumb_height = $this->thumb_height ? $this->thumb_height : 100; $gd = $this->gd_version(); //检查原始是否文件存在并且得到原图信息 $org_info = @getimagesize($thumb_src_image); //返回图片大小 if ($org_info[mime] == 'image/bmp') { //bmp图无法压缩 return $this->error($_M['word']['upfileFail5']); } if (!$this->check_img_function($org_info[2])) { return $this->error($_M['word']['upfileFail6']); } $img_org = $this->img_resource($thumb_src_image, $org_info[2]); //原始图像和缩略图尺寸对比 $scale_org = $org_info[0] / $org_info[1]; $scale_tumnb = $this->thumb_width / $this->thumb_height; //处理缩略图宽度和高度为0的情况,背景和缩略图一样大 if ($this->thumb_width == 0) { $this->thumb_width = $this->thumb_height * $scale_org; } if ($this->thumb_height == 0) { $this->thumb_height = $this->thumb_width / $scale_org; } //创建缩略图 if ($gd == 2) { //创建一张缩略图(黑色) $img_thumb = imagecreatetruecolor($this->thumb_width, $this->thumb_height); } else { $img_thumb = imagecreate($this->thumb_width, $this->thumb_height); } //缩略图背景颜色 if (empty($this->thumb_bgcolor)) { $this->thumb_bgcolor = "#FFFFFF"; } $this->thumb_bgcolor = trim($this->thumb_bgcolor, "#"); sscanf($this->thumb_bgcolor, "%2x%2x%2x", $red, $green, $blue); $clr = imagecolorallocate($img_thumb, $red, $green, $blue); imagefilledrectangle($img_thumb, 0, 0, $this->thumb_width, $this->thumb_height, $clr); //创建背景色,默认为白色 switch ($this->thumb_kind) { case 1: $dst_x = 0; $dst_y = 0; $lessen_width = $this->thumb_width; $lessen_height = $this->thumb_height; $scr_x = 0; $scr_y = 0; $scr_w = $org_info[0]; $scr_h = $org_info[1]; break; case 2: if ($org_info[0] / $this->thumb_width > $org_info[1] / $this->thumb_height) { //上下留白 $lessen_width = $this->thumb_width; $lessen_height = $this->thumb_width / $scale_org; } else { //左右留白 $lessen_width = $this->thumb_height * $scale_org; $lessen_height = $this->thumb_height; } $dst_x = ($this->thumb_width - $lessen_width) / 2; $dst_y = ($this->thumb_height - $lessen_height) / 2; $scr_x = 0; $scr_y = 0; $scr_w = $org_info[0]; $scr_h = $org_info[1]; break; case 3: $dst_x = 0; $dst_y = 0; $lessen_width = $this->thumb_width; $lessen_height = $this->thumb_height; if ($org_info[0] / $this->thumb_width > $org_info[1] / $this->thumb_height) { //上下留白,截左右 $scr_w = $org_info[1] * $scale_tumnb; $scr_h = $org_info[1]; } else { //左右留白,截上下 $scr_w = $org_info[0]; $scr_h = $org_info[0] / $scale_tumnb; } $scr_x = ($org_info[0] - $scr_w) / 2; $scr_y = ($org_info[1] - $scr_h) / 2; break; } //放大原始图片 if ($gd == 2) { imagecopyresampled($img_thumb, $img_org, $dst_x, $dst_y, $scr_x, $scr_y, $lessen_width, $lessen_height, $scr_w, $scr_h); } else { imagecopyresized($img_thumb, $img_org, $dst_x, $dst_y, $scr_x, $scr_y, $lessen_width, $lessen_height, $scr_w, $scr_h); } if (!makedir($thumb_savepath)) { return $this->error($_M['word']['upfileFail4']); } $thumbname = $thumb_savepath . basename($thumb_src_image); //Create switch ($org_info[mime]) { case 'image/gif': if (function_exists('imagegif')) { $re = imagegif($img_thumb, $thumbname); } else { return $this->error($_M['word']['upfileFail9']); } break; case 'image/pjpeg': case 'image/jpeg': if (function_exists('imagejpeg')) { $re = imagejpeg($img_thumb, $thumbname, 100); } else { return $this->error($_M['word']['upfileFail10']); } break; case 'image/x-png': case 'image/png': if (function_exists('imagejpeg')) { $re = imagepng($img_thumb, $thumbname); } else { return $this->error($_M['word']['upfileFail11']); } break; default: return $this->error($_M['word']['upfileFail7']); } if (!$re) { return $this->error($_M['word']['upfileFail8']); } if (stristr(PHP_OS, "WIN")) { $thumbname = @iconv("GBK", "utf-8", $thumbname); } $thumbname = '../' . str_replace(PATH_WEB, '', $thumbname); imagedestroy($img_thumb); imagedestroy($img_org); return $this->sucess($thumbname); }
/** * 遍历文件夹下所有文件 * @param string $jkdir 遍历文件夹,可以是绝对路径,也可以是相对网站根目录的相对路径 * @param string $suffix 遍历文件的后缀,不填写为全部文件。支持正则。 * @param string $jump 跳过不需要遍历的文件夹。要填写网站根目录路径,不要含有../,实质是"/^({$jump})/"中正则参数。 * @return string 返回提取的文件数组。文件路径都是绝对路径。 */ function traversal($jkdir, $suffix = '[A-Za-z]*', $jump = null, &$filenamearray = array()) { if ($jkdir == '.' || $jkdir == './') { $jkdir = ''; } $jkdir = path_absolute($jkdir); $hand = opendir($jkdir); while ($file = readdir($hand)) { $filename = $jkdir . $file; if (@is_dir($filename) && $file != '.' && $file != '..' && $file != './..') { if ($jump != null) { if (preg_match_all("/^({$jump})/", str_replace(PATH_WEB, '', $filename), $out)) { continue; } } traversal($filename, $suffix, $jump, $filenamearray); } else { if ($file != '.' && $file != '..' && $file != './..' && preg_match_all("/\\.({$suffix})/i", $filename, $out)) { if (stristr(PHP_OS, "WIN")) { $filename = iconv("gbk", "utf-8", $filename); } $filenamearray[] = str_replace(PATH_WEB, '', $filename); } } } return $filenamearray; }
/** * 打水印的方法 * @param string $water_scr_image 原图路径 * @return array 返回成功信息 * 返回值为数组各字段含义,error:是否出错,1出错,0正常,errorcode:报错代码,path:水印图片路径 */ public function create($water_scr_image) { global $_M; $water_scr_image = path_absolute($water_scr_image); if (!file_exists($water_scr_image) || is_dir($water_scr_image)) { return $this->error($_M['word']['batchtips30']); } if ($this->is_watermark != 1) { return $this->sucess(path_relative($water_scr_image)); } if ($this->water_save_type == 1) { $save_path_water = dirname($water_scr_image) . '/' . $this->water_savepath; } if ($this->water_save_type == 2) { $save_path_water = dirname($water_scr_image) . '/'; } if ($this->water_save_type == 3) { $save_path_water = $this->water_savepath; } if (stristr(PHP_OS, "WIN")) { $water_scr_image = @iconv("utf-8", "GBK", $water_scr_image); $this->water_image_name = @iconv("utf-8", "GBK", $this->water_image_name); } if (!file_exists($water_scr_image) || is_dir($water_scr_image)) { return $this->error($_M['word']['batchtips30']); } $src_image_type = $this->get_type($water_scr_image); $src_image = $this->createImage($src_image_type, $water_scr_image); if (!$src_image) { return; } $src_image_w = ImageSX($src_image); $src_image_h = ImageSY($src_image); if ($this->water_mark_type == 'img') { $this->water_image_name = strtolower(trim($this->water_image_name)); $met_image_type = $this->get_type($this->water_image_name); $met_image = $this->createImage($met_image_type, $this->water_image_name); $met_image_w = ImageSX($met_image); $met_image_h = ImageSY($met_image); $temp_met_image = $this->getPos($src_image_w, $src_image_h, $this->water_pos, $met_image); $met_image_x = $temp_met_image["dest_x"]; $met_image_y = $temp_met_image["dest_y"]; if ($this->get_type($this->water_image_name) == 'png') { imagecopy($src_image, $met_image, $met_image_x, $met_image_y, 0, 0, $met_image_w, $met_image_h); } else { imagecopymerge($src_image, $met_image, $met_image_x, $met_image_y, 0, 0, $met_image_w, $met_image_h, $this->met_image_transition); } } if ($this->water_mark_type == 'text') { $temp_water_text = $this->getPos($src_image_w, $src_image_h, $this->water_pos); $water_text_x = $temp_water_text["dest_x"]; $water_text_y = $temp_water_text["dest_y"]; if (preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->water_text_color, $color)) { $red = hexdec($color[1]); $green = hexdec($color[2]); $blue = hexdec($color[3]); $water_text_color = imagecolorallocate($src_image, $red, $green, $blue); } else { $water_text_color = imagecolorallocate($src_image, 255, 255, 255); } imagettftext($src_image, $this->water_text_size, $this->water_text_angle, $water_text_x, $water_text_y, $water_text_color, $this->water_text_font, $this->water_text); } makedir($save_path_water); $save_file = $save_path_water . basename($water_scr_image); if ($save_path_water) { switch ($this->get_type($save_path_water)) { case 'gif': $src_img = ImagePNG($src_image, $save_file); break; case 'jpeg': $src_img = ImageJPEG($src_image, $save_file, $this->jpeg_quality); break; case 'png': $src_img = ImagePNG($src_image, $save_file); break; default: $src_img = ImageJPEG($src_image, $save_file, $this->jpeg_quality); break; } } else { if ($src_image_type = "jpg") { $src_image_type = "jpeg"; } header("Content-type: image/{$src_image_type}"); switch ($src_image_type) { case 'gif': $src_img = ImagePNG($src_image); break; case 'jpg': $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; case 'png': $src_img = ImagePNG($src_image); break; default: $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; } } imagedestroy($src_image); if (stristr(PHP_OS, "WIN")) { $water_scr_image = @iconv("GBK", "utf-8", $water_scr_image); $this->water_image_name = @iconv("GBK", "utf-8", $this->water_image_name); $save_file = @iconv("GBK", "utf-8", $save_file); } return $this->sucess(path_relative($save_file)); }
/** * Get the absolute path to a file in the project using the current ref * * @param null|string $path * * @return string */ public function path($path = null) { $root = $this->getDiskConfig()->get('root'); $path = isset($path) ? $root : path_join($root, $path); return path_absolute($root, $path); #return is_null($path) ? '' : path_absolute($path, $this->rootPath()); #return is_null($path) ? $root : path_join($root, $path); }