Следующий код работает, однако в нем есть абсурд. этап сохранения данных на диск и последующего немедленного их повторного получения. Как я могу обойти этот абсурдный шаг и опубликовать изображение непосредственно в API?
Одна вещь, которая не сработала: замена Realpath на image_data.
Код: Выделить всё
//Start with a png image $image that is 1024x1024
//Shrink the image using GD
$temp = imagecreatetruecolor(512, 512);
imagecopyresampled($temp, $image, 0, 0, 0, 0, 512, 512, 1024, 1024);
//Output the GD image to a string named $image_data
ob_start();
imagepng($temp);
$image_data = ob_get_contents();
ob_end_clean();
//Save the image string to disk
//HERE IS THE PART I WANT TO GET RID OF - Saving to and retrieving from disk
$filenamestr = "00tempfile.png";
$file = fopen($filenamestr, "wb");
fwrite($file, $image_data);
fclose($file);
//Get a path to the newly created file on disk
$imagePath = "./".$filenamestr;
$realpath = realpath($imagePath);
//END PART I WANT TO GET RID OF
//Use cURL to post the file to an API
$url = 'https://anapi.com';
$fields = array(
//HERE IS THE KEY LINE WHERE YOU POST THE DATA. BEHIND THE SCENES CURL SEEMS TO BE PULLING THE DATA FROM THE REALPATH AND
//PUTTING IT IN A STRING BUT THERE MUST BE SOME WAY TO DO THE SAME THING WITHOUT HAVING TO CREATE THIS PATH TO DISK FIRST
'image' => '@' . $realpath,
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
//other curl options
$response = curl_exec($ch);
Подробнее здесь: https://stackoverflow.com/questions/793 ... an-on-disk