Home  >  Article  >  Backend Development  >  How to localize remote images in php

How to localize remote images in php

墨辰丷
墨辰丷Original
2018-06-07 17:31:413596browse

This article mainly introduces how to localize remote images in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The code is as follows:

]*)\.(gif|jpg|png))/isU",$body,$img_array); 
   
  $img_array = array_unique($img_array[1]); 
  $imgUrl = $cfg_uploaddir.'/'.date("ymd", time()); 
  $imgPath = $cfg_basedir.$imgUrl; 
  if(!is_dir($imgPath.'/')) 
  { 
    MkdirAll($imgPath, $GLOBALS['cfg_dir_purview']); 
    CloseFtp(); 
  } 
  $milliSecond = date('His',time()); 
  foreach($img_array as $key=>$value) 
  {  
    if(preg_match("#".$basehost."#i", $value)) 
    { 
      continue; 
    } 
    if(preg_match("#".$basehost."#i", $value)) 
    { 
      continue; 
    } 
    if(!preg_match("#^http:\/\/#i", $value)) 
    { 
      continue; 
    } 
    
    $http=pget($value,'$value',true); 
    $itype=($http['head']['content_type']); 
    if(!preg_match("#\.(jpg|gif|png)#i", $itype)) 
    { 
      if($itype=='image/gif') 
      { 
        $itype = ".gif"; 
      } 
      else if($itype=='image/png') 
      { 
        $itype = ".png"; 
      } 
      else 
      { 
        $itype = '.jpg'; 
      } 
    } 
    $milliSecondN = rand(1000,9999).rand(1000,9999); 
    $value = trim($value); 
    $rndFileName = $imgPath.'/'.$milliSecondN.'-'.$key.$itype; 
    $fileurl = $imgUrl.'/'.$milliSecondN.'-'.$key.$itype; 
 
    $tp = fopen($rndFileName, 'wb'); 
    fwrite($tp, $http['data']); 
    fclose($tp); 
     
    if(file_exists($cfg_basedir.$fileurl)) 
    { 
      $info = ''; 
      $imginfos = GetImageSize($rndFileName, $info); 
      $fsize = filesize($rndFileName); 
      $body = str_replace($value, $fileurl, $body); 
  
    } 
  } 
  return $body; 
} 
 
//调用方式 
echo GetCurContent($body);

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

How to use the PHP error handling function register_shutdown_function

PHP uses preg_split and explode to achieve splitting Methods for storing content in textarea

PHP implementation of file locking and unlocking methods

The above is the detailed content of How to localize remote images in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn