Author Topic: Save local copy of images from a URL  (Read 22232 times)

0 Members and 1 Guest are viewing this topic.

Offline brandonc

  • Pre-Newbie
  • Posts: 2
    • View Profile
Save local copy of images from a URL
« on: October 27, 2005, 10:17:21 PM »
This mod allows you to save a local copy of an image when loading the image from a URL path.  It downloads the file to the server and saves a copy of it on the server, instead of just referencing the URL.  I've found this incredibly useful and hope that you will too.  This also auto-creates thumbnails for me (using ImageMagick), but some other features may not work correctly as I didn't pay much attention to other functionality requirements.

Use the attached files to replace these files:
member.txt->4images_root/member.php
upload.txt->4images_root/includes/upload.php
member_uploadform.txt->4images_root/templates/4blue_orange1/member_uploadform.html

Of course the changes in the template file would need to be applied to which ever template you are using.  Just add this line in your template file where you want the option to appear:
Code: [Select]
<input type="checkbox" name="remote_media_file_download">Save a copy locally  <br />
Enjoy!
Brandon Checketts
www.webpipe.net System Administrator
Webpipe.net hosting accounts are fully compatible with 4images

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Save local copy of images from a URL
« Reply #1 on: October 28, 2005, 12:19:40 AM »
Intersting, but please post the changes requred for the mod and not already fixed files, that way people who installed other mods wont need reinstall them again.
Thank you.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline Nasser

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: Save local copy of images from a URL
« Reply #2 on: October 28, 2005, 01:39:02 AM »
wonderful!!
but I am also asking you to paste the changes here please as V@no said coz I have lots of MOD installed and I don't know how to do it without your help

thank You

Offline brandonc

  • Pre-Newbie
  • Posts: 2
    • View Profile
Re: Save local copy of images from a URL
« Reply #3 on: October 28, 2005, 07:02:20 AM »
Instructions for modifying your customized installation
This requires several changes to 2 PHP files and 1 small change to a template file.  There are several changes in each of the PHP files.   Line numbers here were taken from the latest download and will be different once you modify your files.


Changes to member.php

(starting at about line 549):
change this:
Code: [Select]
    // Upload Media file
    if (!empty($HTTP_POST_FILES['media_file']['tmp_name']) && $HTTP_POST_FILES['media_file']['tmp_name'] != "none") {
      $new_name = $site_upload->upload_file("media_file", "media", $upload_cat);
      if (!$new_name) {
        $msg .= (($msg != "") ? "<br />" : "")."<b>".$lang['file_upload_error'].": ".$new_name."</b><br />".$site_upload->get_upload_errors();
        $uploaderror = 1;
      }
    }
    else {
      $new_name = $remote_media_file;
    }

Into this:
Code: [Select]
    // Upload Media file
    if (!empty($HTTP_POST_FILES['media_file']['tmp_name']) && $HTTP_POST_FILES['media_file']['tmp_name'] != "none") {
      $new_name = $site_upload->upload_file("media_file", "media", $upload_cat);
      if (!$new_name) {
        $msg .= (($msg != "") ? "<br />" : "")."<b>".$lang['file_upload_error'].": ".$new_name."</b><br />".$site_upload->get_upload_errors();
        $uploaderror = 1;
      }
    }
    //  BEGIN MODIFICATION by Brandon Checketts from www.webpipe.net bchecketts at hotmail dot com
    elseif ($HTTP_POST_VARS['remote_media_file_download']) {
        $new_name = $site_upload->download_file("remote_media_file", "media", $upload_cat);
    }
    // END BC MODIFICATION
    else {
      $new_name = $remote_media_file;
    }



Change this (about line 573)
Code: [Select]
elseif ($config['auto_thumbnail'] == 1 && !empty($HTTP_POST_FILES['media_file']['tmp_name']) && $HTTP_POST_FILES['media_file']['tmp_name'] != "none" && !$uploaderror) {

Into this:
Code: [Select]
    // BEGIN MODIFICATION by Brandon Checketts from www.webpipe.net  bchecketts at hotmail dot com
    elseif ($config['auto_thumbnail'] == 1 && !empty($_POST['remote_media_file']) && !$uploaderror) {
      if ($direct_upload) {
        $src = MEDIA_PATH."/".$cat_id."/".$new_name;
        $dest = THUMB_PATH."/".$cat_id."/".$new_name;
      }
      else {
        $src = MEDIA_TEMP_PATH."/".$new_name;
        $dest = THUMB_TEMP_PATH."/".$new_name;
      }
      $do_create = 0;
      if ($image_info = @getimagesize($src)) {
        if ($image_info[2] == 1 || $image_info[2] == 2 || $image_info[2] == 3) {
          $do_create = 1;
        }
      }
      if ($do_create) {
        require(ROOT_PATH.'includes/image_utils.php');
        $convert_options = init_convert_options();
        if (!$convert_options['convert_error']) {
          $dimension = (intval($config['auto_thumbnail_dimension'])) ? intval($config['auto_thumbnail_dimension']) : 100;
          $resize_type = (intval($config['auto_thumbnail_resize_type'])) ? intval($config['auto_thumbnail_resize_type']) : 1;
          $quality = (intval($config['auto_thumbnail_quality']) && intval($config['auto_thumbnail_quality']) <= 100) ? intval($config['auto_thumbnail_quality']) : 100;

          if (create_thumbnail($src, $dest, $quality, $dimension, $resize_type)) {
            $new_thumb_name = $new_name;
          }
        }
      }
    }
    // END BC MODIFICATION


    elseif (($config['auto_thumbnail'] == 1 && !empty($HTTP_POST_FILES['media_file']['tmp_name']) && $HTTP_POST_FILES['media_file']['tmp_name'] != "none" && !$uploaderror) ||
        ($config['auto_thumbnail'] == 1 && !$empty($_POST['remote_media_file']) && !empty($_POST['remote_media_file_download']) && !$uploaderror) ) {



Changes to includes/upload.php

About line 44, Add this function before the Upload class is defined:
Code: [Select]
// BEGIN MODIFICATION by Brandon Checketts from www.webpipe.net  bchecketts at hotmail dot com
function move_downloaded_file($file_name, $destination) {
    return copy($file_name, $destination) ? 1 : 0 ;
}
// END BC MODIFICATION

class Upload {


Change this (about line 103)
Code: [Select]
$ok = move_uploaded_file($this->upload_file, $this->upload_path[$this->image_type]."/".$this->file_name);to this
Code: [Select]
        // BEGIN MODIFICATION by Brandon Checketts from www.webpipe.net  bchecketts at hotmail dot com
        if(isset($_POST['remote_media_file_download']) && $_POST['remote_media_file_download']) {
            $ok = move_downloaded_file($this->upload_file, $this->upload_path[$this->image_type]."/".$this->file_name);
        } else {
            $ok = move_uploaded_file($this->upload_file, $this->upload_path[$this->image_type]."/".$this->file_name);
        }
        // END BC MODIFICATION

Change this (line 122)
Code: [Select]
$ok = move_uploaded_file($this->upload_file, $this->upload_path[$this->image_type]."/".$this->file_name);
to this:
Code: [Select]
        // BEGIN MODIFICATION by Brandon Checketts from www.webpipe.net  bchecketts at hotmail dot com
        if(isset($_POST['remote_media_file_download']) && $_POST['remote_media_file_download']) {
            $ok = move_downloaded_file($this->upload_file, $this->upload_path[$this->image_type]."/".$this->file_name);
        } else {
            $ok = move_uploaded_file($this->upload_file, $this->upload_path[$this->image_type]."/".$this->file_name);
        }
        // END BC MODIFICATION


Add this function on line 226 before the "function check_file_extension($extension = "") { line
Code: [Select]
// START MODIFICATIONS by Brandon Checketts from www.webpipe.net  bchecketts at hotmail dot com
  function download_file($field_name, $image_type, $cat_id = 0, $file_name = "")
  {
    $this->field_name = $field_name;
    $this->image_type = $image_type;

    if ($cat_id) {
      $this->upload_path['thumb'] = THUMB_PATH."/".$cat_id;
      $this->upload_path['media'] = MEDIA_PATH."/".$cat_id;
    }
    else {
      $this->upload_path['thumb'] = THUMB_TEMP_PATH;
      $this->upload_path['media'] = MEDIA_TEMP_PATH;
    }

    // Figure out an appropriate file name
    if ($file_name != "") {
      ereg("(.+)\.(.+)", $file_name, $regs);
      $this->name = $regs[1];
      ereg("(.+)\.(.+)", $this->HTTP_POST_FILES[$this->field_name]['name'], $regs);
      $this->extension = $regs[2];
      $this->file_name = $this->name.".".$this->extension ;
    }
    else {
      $file_only = $_POST[$field_name];
      $file_only = preg_replace("/.*\//","",$file_only);
      $this->file_name = $file_only;
      $this->file_name = ereg_replace(" ", "_", $this->file_name);
      $this->file_name = ereg_replace("%20", "_", $this->file_name);
      $this->file_name = preg_replace("/[^-\._a-zA-Z0-9]/", "", $this->file_name);

      ereg("(.+)\.(.+)", $this->file_name, $regs);
      $this->name = $regs[1];
      $this->extension = $regs[2];
    }


    $url = $_POST[$field_name];
    $ch = curl_init($url);
    $save_file = "/tmp/" . $this->file_name;

    $fp = fopen($save_file,"w");

    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_exec($ch);

    $this->mime_type = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);

    curl_close($ch);
    fclose($fp);
    if ($this->save_downloaded_file($save_file)) {
      return $this->file_name;
    }
    else {
      return false;
    }

  }
  // END BC MODIFICATIONS




Changes to your template file (member_uploadform.html)
Add this line in the section where it prompts for the URL (line 27 for for the 4blue_orange1 template)
Code: [Select]
<!-- BEGIN MODIFICATION by Brandon Checketts from www.webpipe.net  bchecketts at hotmail dot com -->
<input type="checkbox" name="remote_media_file_download">Save a copy locally  <br />
<!-- END BC MODIFICATION -->



Thanks,
Brandon Checketts
bchecketts at hotmail dot com
www.webpipe.net System Administrator
Hosting accounts at Webpipe.net are fully compatible with 4images!

Offline newlooks

  • Pre-Newbie
  • Posts: 3
    • View Profile
Re: Save local copy of images from a URL
« Reply #4 on: April 12, 2006, 08:57:06 PM »
is there any way to do multi upload  for URL

Offline maineyak

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Save local copy of images from a URL
« Reply #5 on: February 23, 2008, 03:04:19 PM »
I know this is an old post, but does anyone know why this is uploading the image twice?

Offline maineyak

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Save local copy of images from a URL
« Reply #6 on: February 26, 2008, 12:03:47 AM »
This is an awesome mod I installed with no problems. THanks!

Offline XFuzon

  • Pre-Newbie
  • Posts: 9
    • View Profile
Re: Save local copy of images from a URL
« Reply #7 on: February 29, 2008, 09:08:53 AM »
I'm not getting any checkbox  8O

Offline maineyak

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Save local copy of images from a URL
« Reply #8 on: February 29, 2008, 06:54:37 PM »
I made mine a hidden field so it's automatically checked.

Quote
<input type="hidden" name="remote_media_file_download" value=checked>

Offline NetRebel

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Save local copy of images from a URL
« Reply #9 on: January 03, 2009, 10:42:49 PM »
Sounds like a great mod. Unfortunately it results in a

Fatal error: Call to undefined method Upload::save_downloaded_file() in /home/netrebel/public_html/files.netrebel.net/includes/upload.php on line 306

I can't find the save_downloaded_file() anywhere else in the code, so I tried changing it to move_downloaded_file but that didn't work either. How come maineyak writes it is working okay. Did I miss something?

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Save local copy of images from a URL
« Reply #10 on: January 04, 2009, 12:03:24 AM »
add
  function save_downloaded_file($tmp_file) {
    global 
$user_info;

    
$this->upload_file $tmp_file;
    
$ok 1;
    if (empty(
$this->upload_file) || $this->upload_file == "none") {
      
$this->set_error($this->lang['no_image_file']);
      
$ok 0;
    }


    if (
$user_info['user_level'] != ADMIN) {
      if (!
$this->check_max_filesize()) {
        
$this->set_error($this->lang['invalid_file_size']);
        
$ok 0;
      }
      if (
eregi("image"$this->HTTP_POST_FILES[$this->field_name]['type'])) {
        if (!
$this->check_image_size()) {
          
$ok 0;
        }
      }
    }

    if (!
$this->check_file_extension() || !$this->check_mime_type()) {
      
$this->set_error($this->lang['invalid_file_type']. " (".$this->extension.", ".$this->mime_type.")");
      
$ok 0;
    }
    if (
$ok) {
      if (!
$this->copy_file()) {
        if (isset(
$this->lang['file_copy_error'])) {
          
$this->set_error($this->lang['file_copy_error']);
        }
        
$ok 0;
      }
    }
    return 
$ok;
  }


above
  function download_file($field_name$image_type$cat_id 0$file_name ""
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline NetRebel

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Save local copy of images from a URL
« Reply #11 on: January 04, 2009, 02:49:53 PM »
Yes, it's working great now! :D
Thanks a lot for your help V@no, and thanks brandonc for the original mod of course!