CodeIgniter Forums
Upload From URL - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Upload From URL (/showthread.php?tid=3815)



Upload From URL - El Forum - 10-23-2007

[eluser]Unknown[/eluser]
hi all

I am new to code igniter framework.
In admin side while adding products. I want to upload the image by passing the image URL.
I don't know where to write the exact code of copy function to copy the Url image.

Any body have idea on this, Will provide the suggestion and giuidelines to upload the image from URL.

Thanks in Advance..


Upload From URL - El Forum - 10-23-2007

[eluser]xwero[/eluser]
You could put it in a custom library and load it when necessary.

I adjusted and tested a working function from php.net code snippets.
Code:
class Image_util
{
   function img_from_url($imgurl,$downloaddir='',$quality=85)
   {
    $file = substr(strrchr($imgurl,'/'),1);
    $filename = substr($file,0,strrpos($file,'.'));
    $ext = strtolower(substr(strrchr($file,'.'),1));
    if ($ext == 'jpg' || $ext == 'jpeg')
    {
      $img = @imagecreatefromjpeg($imgurl);
    }
    else if ($ext == 'png')
    {
      $img = @imagecreatefrompng($imgurl);
    }
    else if ($ext == 'gif')
    {
      $img = @imagecreatefromgif($imgurl);
    }
    // If an image was successfully loaded, test the image for size
    if ($img)
    {
       imagejpeg($img,$downloaddir.$filename.'.jpg',$quality);
    }
  }
}



Upload From URL - El Forum - 10-23-2007

[eluser]llbbl[/eluser]
Read these:
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

http://www.php.net/manual/en/function.fopen.php


Upload From URL - El Forum - 10-23-2007

[eluser]Unknown[/eluser]
Hi Xwero..

I understand you code, But where I have to add this code and How I will call this function.
Did you please explain in detail.


Upload From URL - El Forum - 10-24-2007

[eluser]Michael Wales[/eluser]
I would use CURL to snatch the image down.


Upload From URL - El Forum - 10-24-2007

[eluser]xwero[/eluser]
[quote author="johnvgross" date="1193219544"]Hi Xwero..

I understand you code, But where I have to add this code and How I will call this function.
Did you please explain in detail.[/quote]

You have to put it in a file called Image_util.php and put it in the application/libraries folder.

in the config/autoload.php file you can add it to the library array if you use it througout your site or if it's only on a few pages you can load the library in the controller where you call the function too
Code:
class Albums extends Controller
{
    function Albums()
    {
       $this->load->library('image_util');
    }

    function add-image()
    {
       if(isset($_POST['url']))
       {
          $this->image_util->img_from_url($_POST['url']);
       }

    }
}

This is example code, the input should be validated.


Upload From URL - El Forum - 10-24-2007

[eluser]xwero[/eluser]
[quote author="walesmd" date="1193224119"]I would use CURL to snatch the image down.[/quote]
curl is more robust that is true but i think his question is about how to call functions from custom libraries.