: This article mainly introduces a method for php to obtain remote images. For more information about PHP tutorials, see. Requirement: you need to use the profile picture of an individual, Weibo account, or QQ account as the profile picture of the application. these portraits may expire or become unavailable. Therefore, you need to download these images to your local device first, then upload the image to the Qiniu image server and reference the image address of Qiniu.
The following code is used in codeIgniter:
/**
* Information page
*/
Class Test extends Wap_base
{
/**
* Constructor
*/
Public function _ construct ()
{
Parent: :__ construct ();
}
/**
* Remove the third-party login profile and place it on the Qiniu server. then, a new url is returned.
* @ Param string $ url
* @ Return array
*/
Private function uploadOneImage ($ url)
{
$ This-> load-> library ('qiniu/upfile', '', 'upfile ');
$ PathInfo = pathinfo ($ url );
$ ImageInfo =! Empty ($ url )? Getimagesize ($ url): array (
'Mime '=> 'application/unkown'
);
$ UploadError = '';
If (! In_array ($ imageInfo ['Mime '], explode (', ', FILE_TYPES ))){
$ UploadError = "The format of the uploaded file {$ pathInfo ['basename']} is incorrect! ";
}
If (strpos ($ imageInfo ['Mime '], 'jpeg ')! = FALSE ){
If (strpos ($ pathInfo ['basename'], 'jpg ') ===false ){
$ Filename = '/tmp/'.w.pathinfo='basename'{.'.jpg ';
}
} Elseif (strpos ($ imageInfo ['Mime '], 'GIF ')! = FALSE ){
If (strpos ($ pathInfo ['basename'], 'GIF') === FALSE ){
$ Filename = '/tmp/'.w.pathinfo='basename'{.'.gif ';
}
} Elseif (strpos ($ imageInfo ['Mime '], 'PNG ')! = FALSE ){
If (strpos ($ pathInfo ['basename'], 'PNG ') === FALSE ){
$ Filename = '/tmp/'.w.pathinfo='basename'{.'.png ';
}
} Elseif (strpos ($ imageInfo ['Mime '], 'bmp ')! = FALSE ){
If (strpos ($ pathInfo ['basename'], 'bmp ') === FALSE ){
$ Filename = '/tmp/'.w.pathinfo='basename'{.'.bmp ';
}
}
// Curl retrieves the image and writes it to a local file
$ Fp = fopen ($ filename, 'WB ');
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_FILE, $ fp );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 5 );
Curl_exec ($ ch );
Fclose ($ fp );
Curl_close ($ ch );
$ Fileurl = '';
If (empty ($ filename )){
$ UploadError = "an error occurred while uploading the file. the temporary file name is blank! ";
}
If (filesize ($ filename)> MAX_FILE_SIZE * 1024*1024 ){
$ UploadError = "the size of the uploaded file [$ filename] exceeds the limit! ";
} Else {
$ Basename = basename ($ filename );
$ UploadResult = $ this-> upfile-> upload (QINIU_UPLOAD_BUCKET, $ basename, $ filename, 0 );
// Var_dump ($ uploadResult );
$ Fileurl = isset ($ uploadResult ['smallimageurl']) & $ uploadResult ['smallimageurl']! = ''? $ UploadResult ['smallimageurl']: '';
}
If (! Empty ($ uploadError )){
Return ['result' => FALSE, 'fileurl' => $ fileurl, 'error' => $ uploadError];
} Else {
Return ['result' => TRUE, 'fileurl' => $ fileurl, 'error' => $ uploadError];
}
}
Public function index ()
{
$ Url = 'http: // response ';
// The link address of the image in is not provided in the form of an image. therefore, ensure that these file names are valid when uploading to the Qiniu server. 0 is definitely not acceptable.
$ Begin = microtime (TRUE );
$ Result = $ this-> uploadOneImage ($ url );
$ During = (microtime (TRUE)-$ begin );
Echo 'the time used to execute this method is: '. $ .'
';
}
}
}
Call address: http: // localhost/wap/test/index
The above describes a php method for obtaining remote images, including some content. I hope my friends who are interested in PHP tutorials will be helpful.