php remote images saved to the local

2010-12-31  来源:本站原创  分类:PHP  人气:110 

Display remote images:

<?php
header('Content-Type:image/jpg');
echo file_get_contents("http://www.baidu.com/img/baidu_logo.gif");
?>

The first is: Precision

<?php   

//  Variable description :
// $url  Is the full URL to the remote address of the picture , Cannot be empty .
// $filename  Is an optional variable :  If empty, the local file name is based on the time and date
//  Automatically generated .   

function get_photo($url,$filename='',$savefile='test/')
{
        $imgArr = array('gif','bmp','png','ico','jpg','jepg');

    if(!$url) return false;

    if(!$filename) {
      $ext=strtolower(end(explode('.',$url)));
      if(!in_array($ext,$imgArr)) return false;
      $filename=date("dMYHis").'.'.$ext;
    }   

        if(!is_dir($savefile)) mkdir($savefile, 0777);
        if(!is_readable($savefile)) chmod($savefile, 0777);

        $filename = $savefile.$filename;

    ob_start();
    readfile($url);
    $img = ob_get_contents();
    ob_end_clean();
    $size = strlen($img);   

    [email protected]($filename, "a");
    fwrite($fp2,$img);
    fclose($fp2);   

    return $filename;
 }   

 $img=get_photo("http://www.baidu.com/img/baidu_logo.gif");
 echo $img ? '<pre><img src="'.$img.'"></pre>' : "false";

The second: extract the image from the article and save it to local

function getImg($str){
        $str = stripslashes($str);
        $pattern = "/<img[^>]*src\=\"(([^>]*)(jpg|gif|png|bmp|jpeg))\"/i";   // Gets the all pictures tab of all information
        preg_match_all($pattern, $str, $matches);

        return $matches[1];   //$matches[1] In the match results, the result is an array
}

        $str = <<<EOT
Money has been moving into Brazilian stocks over the last couple of days, despite mostly flat trading activity in the U.S. equity markets. During Thursday's session, the iShares MSCI Brazil Index ETF (NYSE: EWZ) has risen 1.12% after outperforming the U.S. indices yesterday as well.

Must Read
SonySony Playstation Phone Rumors Heat Up
A South Korean Army soldier walks up steps of a guard post near the demilitarised zone separating the two Koreas.S. Korea waves olive branch
<img width="1" height="1" alt="" src="http://img.ibtimes.com/www/site/us/images/1px.gif" sized="yes">
Two giant resource companies make up a big chunk of the Brazilian Bovespa. They are PetroBras (NYSE: PBR), with a market cap of $163.44 billion, and miner Vale (NYSE: VALE), which has a market cap of $178.95 billion. PBR shares have risen 1.47% thus far today and VALE is trading 0.77% higher at $34.03.
<img alt="Sony" src="http://img.ibtimes.com/www/thumb/mainpage/13463-12079-sony.jpg" sized="yes">
This article was originally published on Benzinga, and is republished here with permission.
EOT;

foreach( getImg($str) as $url)
{
        get_photo($url);
}
相关文章
  • php remote images saved to the local 2010-12-31

    Display remote images: <?php header('Content-Type:image/jpg'); echo file_get_contents("http://www.baidu.com/img/baidu_logo.gif"); ?> The first is: Precision <?php // Variable description : // $url Is the full URL to the remote address o

  • Oracle Remote Copy table to the local 2011-08-13

    Problem 2 SQL*Plus Copy Command Problem Description How SQL*Plus Between different tables to achieve data replication, whether local or remote ? Analysis In SQL*Plus The copy command , You can complete the remote database by the local database or Ora

  • Batch FTP download complete remote backup files to the local 2011-10-26

    Zhuantie Address: http://topic.csdn.net/u/20090625/22/C55BBD61-8CEB-4965-BCE8-6351B96404F1.html

  • Jsp to read using a remote file to local 2010-07-14

    This article describes the use of jsp to read a remote file to the local paper topics. With jsp, read remote files, save to the local Read the network file some are not, I give you a complete bar code, saved as jsp can run directly. <% @ Page import

  • Transfer: Based on remote database table, materialized view creation of local databases 2010-05-03

    Based on remote database tables, creation of local databases materialized views December 28, 2008 Sunday 12:40 to connect the remote server is a reference to the following snippet: create database link linkfwq connect to fzept identified by neu using

  • Use PuTTYgen generated in Windows XP remote SSH key pair and copy files function - in 2010-07-07

    NOTE: The code notes that the role of # only, the content can not be entered into the command line window Used in the Windows system generated PuTTYgen key pair Locate and run the PuTTYgen.exe (PuTTY Key Generator), select SSH-2 RSA key type, then pr

  • ubuntu SSH connection remote upload and download files 2011-08-18

    Install SSH (Secure Shell) for remote management services sudo apt-get install ssh SSH remote login Ubuntu machine ssh [email protected] The files / folders from remote Ubuntu machine to a local copy (scp) scp-r [email protected].1: / home / user

  • java to read a remote file using SMB 2008-06-28

    package com.yss.test.FileReadWriter; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream

  • Mysql remote connection can not solve the problem 2010-03-29

    Solution Mysql remote connection problems can not be 1, Mysql port is correct, by netstat-ntlp view port occupancy, in general, the port is 3306. In connection with the tool is to use the port MySQl. For example My Admin \ My Query Browser \ MySQl Fr

  • linux copy file command in the remote (scp) Xiangjie 2010-03-08

    Linux systems use scp command. scp is the secure copy of the shorthand, used to remotely copy files under linux commands, and it has a similar command cp, cp, but only in the copy machine can not cross-server. Sometimes we need a file on a remote ser

  • flex examples of shared object access to local data (intermediate) 2010-06-19

    1, shareObj.mxml <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo&qu

  • Putty + Xmanager combination of remote access is very Cool 2010-06-24

    A very Cool gameplay and ultra-simple configuration, security, Ye Hao is Putty + Xmanager, run the X application that looks more like a MS Windows program, in fact, the actual use of window manager is the MS Windows , behind the map. General idea Xma

  • Putty + Xmanager combination of remote access is Cool 2010-06-24

    A very Cool gameplay and ultra-simple configuration, security, Ye Hao is Putty + Xmanager, run the X application that looks more like a MS Windows program, in fact, the actual use of window manager is the MS Windows , behind the map. General idea Xma

  • winscp read the remote server command line batch file example 2010-07-12

    option echo off option batch on option confirm off open scp: / / root: [email protected] get / opt/backup/gnome-libs-1.4.2.tar.bz2 e: \ bb \ gnome-libs-1.4.2.tar.bz2 close exit The total online article that there is an order that order, is an examp

  • spring ejb3 remote call exception 2010-08-25

    2010-08-25 14:51:46 46 [org.jboss.remoting.transport.socket.SocketClientInvoker] - [ERROR] - Got marshalling exception, exiting java.lang.ClassNotFoundException: javax.ejb.NoSuchEJBException at org.jboss.remoting.serialization.ClassLoaderUtility.load

  • SSH remote login. Management. 2010-11-19

    ssh server Openssh service configuration / etc / ssh / sshd_config port protocol listenaddress serverkeybits loglevel logingracetime permitrootlogin # control whether to allow root user login PasswordAuthentication no # control whether to enable publ

  • linux remote Detailed scp and sftp file transfer 2011-06-15

    In general, the local network for data exchange with the remote network hold, or data migration, there are three methods commonly used, first, ftp, second wget / fetch Third, rsync large-scale data migration with rysync, followed by fetch / wget, mos

  • Remote Desktop mstsc / console (/ admin) the use of 2011-07-01

    When our windows2003 server exceeded the maximum number of connections after we restart the server in addition, there is a best way is to use / console (console mode mstsc / console / v: IP: port terminal) to log on. When we added the parameters, wil

  • mysql remote connection fails solutions 2011-04-29

    Today to build LAMP (Linux, apache, mysql, php) environment, due to the mysql database and php is not the same computer, apache mysql connection error when. View user under mysql database table The original is not open remote login mysql, only to log

  • 12 The most important J2EE best practices 2009-03-04

    MVC Framework (as well as the J2EE implementation of the MVC) is one of the advantages of the component elements can (in fact, very easy) on your application for unit testing. Therefore, you can easily entity bean, session bean and the JSP an indepen