$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 缩略图高度 * @param string $savepath * @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 $source_path * @param $upFile * @param int $target_width * @param int $target_height * @return array * @author 倪宗锋 */ public static function cutPic($source_path, $upFile, $target_width = 300, $target_height = 300) { $source_info = getimagesize($source_path); $source_width = $source_info[0]; $source_height = $source_info[1]; $source_mime = $source_info['mime']; $source_ratio = $source_height / $source_width; $target_ratio = $target_height / $target_width; // 源图过高 if ($source_ratio > $target_ratio) { $cropped_width = $source_width; $cropped_height = $source_width * $target_ratio; $source_x = 0; $source_y = ($source_height - $cropped_height) / 2; } elseif ($source_ratio < $target_ratio) { // 源图过宽 $cropped_width = $source_height / $target_ratio; $cropped_height = $source_height; $source_x = ($source_width - $cropped_width) / 2; $source_y = 0; } else { // 源图适中 $cropped_width = $source_width; $cropped_height = $source_height; $source_x = 0; $source_y = 0; } switch ($source_mime) { case 'image/gif': $source_image = imagecreatefromgif($source_path); break; case 'image/jpeg': $source_image = imagecreatefromjpeg($source_path); break; case 'image/png': $source_image = imagecreatefrompng($source_path); break; default: return Util::returnArrEr(); break; } $target_image = imagecreatetruecolor($target_width, $target_height); $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height); // 裁剪 imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height); // 缩放 imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height); imagejpeg($target_image, $upFile, 100); imagedestroy($source_image); imagedestroy($target_image); imagedestroy($cropped_image); return Util::returnArrSu('', array('url' => $upFile)); } }