Fetch & Save / Cache an image from a remote server using PH

PHP Result Center PHP Result Center เป็นหมวด ที่ไว้รวบรวม โปรแกรม Code php Javascript CSS CMS

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41344
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Fetch & Save / Cache an image from a remote server using PH

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »


Fetch & Save / Cache an image from a remote server

โค้ด: เลือกทั้งหมด

function cache_image($image_url){
    //replace with your cache directory
    $image_path = 'path/to/cache/dir/';
    //get the name of the file
    $exploded_image_url = explode("/",$image_url);
    $image_filename = end($exploded_image_url);
    $exploded_image_filename = explode(".",$image_filename);
    $extension = end($exploded_image_filename);
    //make sure its an image
    if($extension=="gif"||$extension=="jpg"||$extension=="png"){
        //get the remote image
        $image_to_fetch = file_get_contents($image_url);
        //save it
        $local_image_file  = fopen($image_path.$image_filename, 'w+');
        chmod($image_path.$image_filename,0755);
        fwrite($local_image_file, $image_to_fetch);
        fclose($local_image_file);    
    }
}

//วิธีเรียกใช้
//cache_image("http://www.flickr.com/someimage.jpg"); 
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 61