$maxsize) { $pin = round($maxsize / $size, 2) * 100; } try{ list($width, $height) = getimagesize($upfile['tmp_name']); move_uploaded_file($upfile['tmp_name'], $updir); $new_image = imagecreatetruecolor($width, $height); $old_image = imagecreatefromjpeg($updir); imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $width, $height, $width, $height); imagejpeg($new_image, $updir, $pin); return Util::returnArrSu('', ['url' => $updir]); }catch (Exception $e){ return Util::returnArrEr('上传图片失败! '); } } /** * Des:获取比例压缩后的图片大小 * Name: show_pic_scal * @param $width * @param $height * @param $picpath * @return mixed * @author 倪宗锋 */ public static function show_pic_scal($width, $height, $picpath) { $imginfo = GetImageSize($picpath); $imgw = $imginfo [0]; $imgh = $imginfo [1]; $ra = number_format(($imgw / $imgh), 1); //宽高比 $ra2 = number_format(($imgh / $imgw), 1); //高宽比 if ($imgw > $width or $imgh > $height) { if ($imgw > $imgh) { $newWidth = $width; $newHeight = round($newWidth / $ra); } elseif ($imgw < $imgh) { $newHeight = $height; $newWidth = round($newHeight / $ra2); } else { $newWidth = $width; $newHeight = round($newWidth / $ra); } } else { $newHeight = $imgh; $newWidth = $imgw; } $newsize [0] = $newWidth; $newsize [1] = $newHeight; return $newsize; } public static function getImageInfo($src) { return getimagesize($src); } /** * 创建图片,返回资源类型 * @param string $src 图片路径 * @return resource $im 返回资源类型 * **/ public static function create($src) { $info = static::getImageInfo($src); switch ($info[2]) { case 1: $im = imagecreatefromgif($src); break; case 2: $im = imagecreatefromjpeg($src); break; default: $im = imagecreatefrompng($src); break; } return $im; } /** * 缩略图主函数 * @param string $src 图片路径 * @param int $w 缩略图宽度 * @param int $h 缩略图高度 * @return mixed 返回缩略图路径 * **/ public static function resize($src, $w, $h, $savepath) { //获取图片的基本信息 $info = static::getImageInfo($src); $width = $info[0];//获取图片宽度 $height = $info[1];//获取图片高度 $per1 = round($width / $height, 2);//计算原图长宽比 $per2 = round($w / $h, 2);//计算缩略图长宽比 //计算缩放比例 if ($per1 > $per2 || $per1 == $per2) { //原图长宽比大于或者等于缩略图长宽比,则按照宽度优先 $per = $w / $width; } else { //原图长宽比小于缩略图长宽比,则按照高度优先 $per = $h / $height; } $temp_w = intval($width * $per);//计算原图缩放后的宽度 $temp_h = intval($height * $per);//计算原图缩放后的高度 $temp_img = imagecreatetruecolor($temp_w, $temp_h);//创建画布 $im = static::create($src); imagecopyresampled($temp_img, $im, 0, 0, 0, 0, $temp_w, $temp_h, $width, $height); if ($per1 > $per2) { imagejpeg($temp_img, $savepath, 100); imagedestroy($im); return static::addBg($savepath, $w, $h, "w"); //宽度优先,在缩放之后高度不足的情况下补上背景 } if ($per1 == $per2) { imagejpeg($temp_img, $savepath, 100); imagedestroy($im); return $savepath; //等比缩放 } if ($per1 < $per2) { imagejpeg($temp_img, $savepath, 100); imagedestroy($im); return static::addBg($savepath, $w, $h, "h"); //高度优先,在缩放之后宽度不足的情况下补上背景 } return ''; } /** * Des:添加背景 返回加上背景的图片 * Name: addBg * @param string $src 图片路径 * @param int $w 背景图像宽度 * @param int $h 背景图像高度 * @param String $first 决定图像最终位置的,w 宽度优先 h 高度优先 wh:等比 * @return mixed * @author 倪宗锋 */ public static function addBg($src, $w, $h, $first = "w") { $bg = imagecreatetruecolor($w, $h); $white = imagecolorallocate($bg, 255, 255, 255); imagefill($bg, 0, 0, $white);//填充背景 //获取目标图片信息 $info = static::getImageInfo($src); $width = $info[0];//目标图片宽度 $height = $info[1];//目标图片高度 $img = static::create($src); if ($first == "wh") { //等比缩放 return $src; } else { if ($first == "w") { $x = 0; $y = ($h - $height) / 2;//垂直居中 } else { $x = ($w - $width) / 2;//水平居中 $y = 0; } imagecopymerge($bg, $img, $x, $y, 0, 0, $width, $height, 100); imagejpeg($bg, $src, 100); imagedestroy($bg); imagedestroy($img); return $src; } } /** * Des:剪切图片 * Name: cutPic * @param $image * @param $upFile * @param int $xx * @param int $yy * @return array * @author 倪宗锋 */ public static function cutPic($image, $upFile, $xx = 300, $yy = 300) { $imgstream = file_get_contents($image); $im = imagecreatefromstring($imgstream); $x = imagesx($im);//获取图片的宽 $y = imagesy($im);//获取图片的高 if ($x > $y) { //图片宽大于高 $sx = abs(($y - $x) / 2); $sy = 0; $thumbw = $y; $thumbh = $y; } else { //图片高大于等于宽 $sy = abs(($x - $y) / 2.5); $sx = 0; $thumbw = $x; $thumbh = $x; } if (function_exists("imagecreatetruecolor")) { $dim = imagecreatetruecolor($yy, $xx); // 创建目标图gd2 } else { $dim = imagecreate($yy, $xx); // 创建目标图gd1 } imageCopyreSampled($dim, $im, 0, 0, $sx, $sy, $yy, $xx, $thumbw, $thumbh); imagejpeg($dim, $upFile); return Util::returnArrSu('', array('url' => $upFile)); } }