/** * 对本地图片做缩略图处理 * @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; }
<form class="form-horizontal" action="<?php echo get_url("UserControlApi", "thumbnail"); ?> " method="post" style="max-width: 800px"> <fieldset> <legend>缩略图大小设置</legend> <div class="form-group"> <label class="control-label col-sm-2" for="InputThumbnailWidth">默认缩略图宽</label> <div class="col-sm-10"> <input type="number" name="image_thumbnail_width" placeholder="默认宽度400" id="InputThumbnailWidth" value="<?php echo image_thumbnail_width(); ?> " class="form-control"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="InputThumbnailHeight">默认缩略高度</label> <div class="col-sm-10"> <input type="number" name="image_thumbnail_height" placeholder="默认宽度300" id="InputThumbnailHeight" value="<?php echo image_thumbnail_height(); ?> " class="form-control"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="InputHdWidth">默认高清图宽</label> <div class="col-sm-10"> <input type="number" name="image_hd_width" placeholder="默认宽度1600" id="InputHdWidth" value="<?php echo image_hd_width(); ?> " class="form-control">