Proportional scaling of PHP background images

//Image upload to achieve proportional scaling

// Upload multiple images of products
 public function index (){
 $typeArr = array ( "jpg" , "png" , "gif" ) ; // Allow upload file formats
 $path = "./uploads/img/" ; // upload path
 if ( isset ( $_POST )) {
 $name = $_FILES [ 'file' ][ 'name' ] ;
 $size = $_FILES [ 'file' ][ 'size' ] ;
$name_tmp                                     = $_FILES [ 'file' ][ 'tmp_name' ] ;
 if ( empty ( $name )) {
 echo json_encode( array ( "error" => " You have not selected a picture " )) ; exit ;
 }
 $type = strtolower (substr(strrchr( $name , '.' ) , 1 )) ; // Get the file type
 if (!in_array( $type , $typeArr )) {
 echo json_encode( array                                                        ( "error" => " Please upload a jpg, png or gif image! " )) ; exit ;
 }
 if ( $size > ( 2 * 1024 * 1024 )) {
 echo json_encode( array ( "error" => " Image size is over 2MB ! " )) ; exit ;
 }
 $image = \think\Image:: open (request()-> file ( 'file' )) ;
 $type                                                    = $image -> type () ;
 // Get the height and width of the
 image $width = $image -> width () ;
 $height = $image -> height () ;
 $file = date( "Ymd" , time( )) ;
 $fileName = './uploads/thums/' . $file ;
 if (!is_dir( $fileName )) mkdir( $fileName , 0777 ) ;
 $imgName = md5(uniqid(rand())). '. ' .                                                        $type ;
 $pathurl = $fileName . '/' . $imgName ; // path + image name
 if (( $width / $height ) == 1 ){   // when the ratio of the current image is larger than the ratio of the target image Compress the width to achieve proportional scaling of height
 $result = $image -> thumb ( 560 , 560 , \think\Image:: THUMB_SCALING )-> save ( $pathurl ) ;
 } else {
 $result = $image ->                                                thumb(560,560,\think\Image::THUMB_FILLED)->save($pathurl);
}
if($result){
echo json_encode(array("error"=>"0","pic"=>substr($pathurl,1),"name"=>$imgName));
}else{
echo json_encode(array("error"=>"The upload is wrong, please check the server configuration!                                                "));
        }

    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325508658&siteId=291194637