public function process_avatar($file, $width) { c_lib()->load('image'); $image = new Image(Image::IMAGE_GD); $image->open(_BasePath_ . "/avatar/" . $file); $img_w = $image->width(); $img_h = $image->height(); $x = $img_h / $img_w; $x_h = $img_h < $img_w ? $width / $x : $width * $x; $image->thumb($x_h, $x_h, Image::IMAGE_THUMB_SCALE); $image->thumb($width, $width, Image::IMAGE_THUMB_CENTER); $image->save(_BasePath_ . "/avatar/" . $file, 'jpeg'); $this->clearOtherAvatar($file); }
/** * 对本地图片做缩略图处理 * @param string $path * @param array $server_meta * @return array */ private function makeThumbnail($path, $server_meta) { $rt = []; if (isset($server_meta['server_root_path'])) { $root = $server_meta['server_root_path']; $img_path = $root . "/" . $path; if (is_file($img_path) && is_readable($img_path)) { try { c_lib()->load('image'); $img = new Image(Image::IMAGE_GD, $img_path); $img_w = $img->width(); $img_h = $img->height(); $x = $img_w / $img_h; //创建高清图 if ($img_w > image_hd_width()) { $img->thumb(image_hd_width(), image_hd_width() / $x, Image::IMAGE_THUMB_SCALE); $t_path = "hd/" . dirname($path); $this->createPath($root . "/" . $t_path); $img->save($root . "/hd/" . $path); $rt['pic_hd_path'] = "hd/" . $path; $rt['pic_hd_width'] = $img->width(); $rt['pic_hd_height'] = $img->height(); } else { $rt['pic_hd_path'] = $path; } //创建显示图 if ($img_w > image_display_width()) { $img->thumb(image_display_width(), image_display_width() / $x, Image::IMAGE_THUMB_SCALE); $t_path = "display/" . dirname($path); $this->createPath($root . "/" . $t_path); $img->save($root . "/display/" . $path); $rt['pic_display_path'] = "display/" . $path; $rt['pic_display_width'] = $img->width(); $rt['pic_display_height'] = $img->height(); } else { $rt['pic_display_path'] = $path; } //创建缩略图 $img_w = $img->width(); $c_w = image_thumbnail_width(); $c_h = image_thumbnail_height(); $img_h = $img->height(); if ($img_w < $c_w) { //目标图片宽度小于缩略图 $img_w = $c_w; $img_h = $img_h * $c_w / $img_w; } if ($img_h < $c_h) { //目标图片高度小于缩略图 $img_h = $c_h; $img_w = $img_w * $c_h / $img_h; } //if($img_w / $img_h < $c_w / $c_h){ if ($img_w * $c_h < $c_w * $img_h) { //化简表达 $x_w = $img_w; $x_h = ceil($x_w * $c_h / $c_w); } else { $x_h = $img_h; $x_w = ceil($x_h * $c_w / $c_h); } Log::write(print_r(get_defined_vars(), true)); $img->thumb($x_w, $x_h, Image::IMAGE_THUMB_CENTER); Log::write(print_r(get_defined_vars(), true)); //$img->thumb($c_w, $c_h, Image::IMAGE_THUMB_SCALE); $t_path = "thumbnail/" . dirname($path); $this->createPath($root . "/" . $t_path); $img->save($root . "/thumbnail/" . $path); $rt['pic_thumbnails_path'] = "thumbnail/" . $path; $rt['pic_thumbnails_width'] = $c_w; $rt['pic_thumbnails_height'] = $c_h; } catch (\Exception $ex) { return []; } } } return $rt; }