Skip to content

Instantly share code, notes, and snippets.

@Oceas
Created June 5, 2020 19:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Oceas/29db8dfec1b290154c653acd707e6a64 to your computer and use it in GitHub Desktop.
/**
* Upload photo to site.
*
* @author Scott Anderson <scott.anderson@webdevstudios.com>
* @since NEXT
* @param Request $request Request form date to format for api.
* @return int Id of image uploaded.
*/
public function upload_photo( Request $request ) : int {
$this->new_construct();
$file = file_get_contents( $request->file( 'featured_image_upload' ) );
$response = json_decode( $this->client->post( "{$this->site_url}/wp-json/wp/v2/media", [ 'headers' => $this->image_header( $request ), 'body' => $file ] )->getBody() );
return $response->id;
}
/**
* Specialized image header.
*
* @author Scott Anderson <scott.anderson@webdevstudios.com>
* @since NEXT
* @param Request $request Request form date to format for api.
* @return array
*/
private function image_header( Request $request ) : array {
return array_merge( $this->headers, [
'Content-Disposition' => 'attachment; filename=testing.jpeg',
'Content-Type' => 'image/png',
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment