Testing the generateThumbnail call of the Azure Computer Vision API from PHP. I have been able to get it to operate, but the images being saved locally are very, very poor quality. Highly pixelated, very blurry, etc. They look nothing like the examples presented at https://www.microsoft.com/cognitive-services/en-us/computer-vision-api/documentation#Thumbnails
Is this an issue with the image processing on the server side, or possibly a degradation issue occurring locally during the file save process? I'm having trouble determining where to start on this one.
This seems to be the same follow-up question asked here:
Generate thumbnail in php, posting to Azure Computer Vision API
Source image dimensions are 542x1714. Trying to create 115x115 thumbnail.
Code at the moment. Have tried it with smartCropping set to both True and False.
$posturl = 'https://westus.api.cognitive.microsoft.com/vision/v1.0/generateThumbnail';
$posturl = add_query_arg( array( 'width' => $max_w, 'height' => $max_h, 'smartCropping' => true), $posturl);
$request = wp_remote_post( $posturl, array( 'headers' => array( 'Content-Type' => 'application/octet-stream', 'Ocp-Apim-Subscription-Key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' ), 'body' => file_get_contents( $this->file ) ) );
if ( is_wp_error( $request ) ) {
return null;
} else {
$resized = #imagecreatefromstring( $request['body'] );
}
The following code works fine, however if I change the files to use myFile.pdf or myFile.xlsx the file gets created with 0 bytes. only thing that changes for the working version and the non working version is the file type. Why am I getting 0 bytes with excel files and pdfs?
$file = file_get_contents('myFile.xml');
$url = 'destination.php';
$post_data = array(
"file" => $file,
"fileName" => "myFile.xml"
);
$stream_options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($post_data),
),
);
$context = stream_context_create($stream_options);
$response = file_get_contents($url, null, $context);
echo $response;
then my destination file saves the file like this:
file_put_contents($_POST["fileName"], $_POST["file"]);
?>
In My instance the problem was actually with file size restrictions on the server and the file types were just coincidental. check post_max_size and upload_max_filesize.
I'm using the library tmhOAuth to post to Twitter in an app and I've already implemented uploading pictures but am having trouble implementing video upload.
This is the call I use to upload pictures and works perfectly with images.
$temp = '#upload/'.$name.';type='.$_FILES['img']['type'].';filename='.$name;
$media = $tmhOAuth->request('POST', 'https://upload.twitter.com/1.1/media/upload.json', array('media' => $temp), true, true);
So I thought it might be the same for videos but I got the error
stdClass Object ( [request] => /1.1/media/upload.json [error] => media type unrecognized. )
I believe I have to make 3 separate calls, as per the Twitter API, so I tried this
$media = $tmhOAuth->request('POST', 'https://upload.twitter.com/1.1/media/upload.json?command=INIT&media_type=video/mp4&total_bytes='.$_FILES['img']['size'], array('media' => $temp), true, true);
$media_id = json_decode($tmhOAuth->response['response'])->media_id_string;
$media = $tmhOAuth->request('POST', 'https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id='.$media_id.'&segment_index=0', array('media' => $temp), true, true);
$media = $tmhOAuth->request('POST', 'https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id='.$media_id, array('media' => $temp), true, true);
but I kept getting the same error for all 3 calls
stdClass Object ( [request] => /1.1/media/upload.json [error] => media type unrecognized. )
Can anyone provide an example as to how to upload videos to twitter? I could find no examples online and it might just not be possible.
I had the same problem. Here's how I managed to solve it.
First you set up a var containing the filesystem full path to the media you want to upload.
$media_path = '/PATH/TO/THE/file.mp4';
Then instantiate $tmhOAuth and do the 3 steps :
$tmhOAuthUpload = new tmhOAuth();
INIT:
$code = $tmhOAuthUpload->request(
'POST',
$tmhOAuthUpload->url('/1.1/media/upload.json'),
array(
"command" => "INIT",
"total_bytes" => (int)filesize($media_path),
'media_type' => 'video/mp4',
)
);
Retrieve media id returned by Twitter
$results = json_decode($tmhOAuthUpload->response['response']);
$media_id = $results->media_id_string;
APPEND: Handle video/media upload with the Append loop
$fp = fopen($media_path, 'r');
$segment_id = 0;
while (! feof($fp)) {
$chunk = fread($fp, 1048576); // 1MB per chunk for this sample
$tmhOAuthUpload->request(
'POST',
$tmhOAuthUpload->url('/1.1/media/upload.json'),
array(
"command" => "APPEND",
"media_id" => $media_id,
'media_data' => base64_encode($chunk),
"segment_index" => $segment_id
)
);
$segment_id++;
}
FINALIZE:
$tmhOAuthUpload->request(
'POST',
$tmhOAuthUpload->url('/1.1/media/upload.json'),
array(
"command" => "FINALIZE",
"media_id" => $media_id,
)
);
By then I was able to to send my tweet:
$code = $tmhOAuth->request(
'POST',
$tmhOAuthUpload->url('1.1/statuses/update'),
array(
'media_ids' => $media_id,
'status' => $text,
),
true // use auth
);
Hope that helps
I've only been able to get video uploading working with CodeBird - a different PHP library.
The Twitter API calls for video are quite different from uploading images, as you've discovered.
Uploading videos to Twitter (≤ 15MB, MP4) requires you to send them in chunks. You need to perform at least 3 calls to obtain your media_id for the video:
Send an INIT event to get a media_id draft.
Upload your chunks with APPEND events, each one up to 5MB in size.
Send a FINALIZE event to convert the draft to a ready-to-tweet media_id.
Post your tweet with video attached.
Remember, each APPEND must be 5MB or under.
If you are consistently getting "Media Type Unrecognised" errors, it might be that the video you are using is incompatible with Twitter. Can you test uploading the video via another service?
Thank you very much for that answer Pierre! I was however getting a “Not valid video” error if I tried to create the tweet too soon. The video wasn't done being processed by Twitter. In addition to Pierre's code, I needed something like this to check STATUS, after FINALIZE:
$videoCount = 0;
do
{
$tmhOAuth->request(
'GET',
$tmhOAuth->url('/1.1/media/upload.json'),
array(
"command" => "STATUS",
"media_id" => $mediaID,
)
);
$twitterResult = json_decode($tmhOAuth->response['response']);
if ($twitterResult->processing_info->state != 'succeeded')
{ sleep(5); }
$videoCount++;
}
while ($twitterResult->processing_info->state != 'succeeded' && $videoCount < 5);
Note: my variable names are different
i try to get website speed of some website, but i can read only ''domain.com'' i can't read website files like css , js ... why ? i use proxies for this job.
here is the php code:
$options = array(
'useragent' => "Firefox (+http://www.firefox.org)", // who am i
'connecttimeout' => 120, // timeout on connect
'timeout' => 120, // timeout on response
'redirect' => 10, // stop after 10 redirects
'referer' => "http://www.google.com",
'proxyhost' =>'85.25.8.14:80'
);
$response = http_get("http://solve-ict.com/wp-content/themes/ict%20theme/js/jquery-1.7.1.min.js", $options , $info);
but it works fine with http://solve-ict.com/
but with files css or js it gives 404
using some free proxy servers available
How can I find the file size of an image on a remote server? On the local machine, I can use filesize(), but I want to find the size without downloading the image.
// Similar solution seen in another stackoverflow post:
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
$headers = get_headers($source_file,1);
$source_file_size = null;
if (is_array($headers) && array_key_exists('Content-Length',$headers)) {
$source_file_size = intval($headers['Content-Length']);
}
if ($source_file_size === null){ //we didn't get a Content-Length header
/** Grab file to local disk and use filesize() to set $size **/
}
stream_context_set_default(
array(
'http' => array(
'method' => 'GET'
)
)
);