Welcome Guest, Not a member yet? Register   Sign In
Upload From URL
#1

[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..
#2

[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);
    }
  }
}
#4

[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.
#5

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

[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.
#7

[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.




Theme © iAndrew 2016 - Forum software by © MyBB