CodeIgniter Forums

Full Version: saving external image and creating thumbnail locally problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]lexusgs430[/eluser]
Hi,

I have a database filled with links to external images, Id like to save those images locally and resize them to a thumbnail size. Im having trouble doing that. ive verified that the image link being passed to my function are correct, but it doesen't want to create the image on my server it seems (made sure priveleges were at 777 for the folder.

Any help would be much appreciated.

(Note that I am loading this as a helper)

Code:
<?php

function image_thumb($image_link, $height, $width, $image_name)
{
    // Get the CodeIgniter super object
    $CI =& get_instance();

    // Path to image thumbnail
    $base = base_url();
    $image_thumb = $image_link;
    $new_src = $base."images/".$image_name.".jpg";


    if( ! file_exists($image_thumb))
    {
        // LOAD LIBRARY
        $CI->load->library('image_lib');

        // CONFIGURE IMAGE LIBRARY
        $config['image_library']    = 'gd2';
        $config['source_image']        = $image_link;
        $config['new_image']        = $new_src;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio']    = TRUE;
        $config['height']            = $height;
        $config['width']            = $width;
        $CI->image_lib->initialize($config);
        $CI->image_lib->resize();
        $CI->image_lib->clear();
    }

    return '<img src="' . $new_src . '" />';
}

El Forum

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...e_lib.html

Quote:source_image None None Sets the source image name/path. The path must be a relative or absolute server path, not a URL.

El Forum

[eluser]lexusgs430[/eluser]
Thanks, I already had read that.... Does that mean it is not possible to get the image from an external URL? It seems like this would be a pretty commonly used feature.

El Forum

[eluser]danmontgomery[/eluser]
PHP didn't support opening of remote files with fopen (or include, require, etc) until sometime during PHP4 (4.3, maybe?). It's also dependent on a php.ini setting. You can still fetch the file yourself using fopen, file_get_contents, curl or sockets, save the file locally, then proceed normally.

El Forum

[eluser]lexusgs430[/eluser]
ah. cool, thanks for the help

El Forum

[eluser]lexusgs430[/eluser]
Ok so I got the save to server working, now in a seperate function call, I am trying to resize the image. What is strange is, I am referencing the image URL using relative, and its working fine to display the current image on the controller (the same image I am trying to resize), but on when the image_thumb function is called it doesent throw any errors, but also does not resize. and I also put in a return the image address call at the bottom, which is not recognizing the path to the image correctly. So ya, was hoping you could take a look at it now.

Code:
function image_thumb($image_url, $height, $width)
{
    // Get the CodeIgniter super object
    $CI =& get_instance();

    // Path to image thumbnail




    if( ! file_exists($image_url))
    {
        
        // LOAD LIBRARY
        $CI->load->library('image_lib');

        // CONFIGURE IMAGE LIBRARY
        $config['image_library']    = 'gd2';
        $config['source_image']        = $image_url;
        $config['maintain_ratio']    = TRUE;
        $config['height']            = $height;
        $config['width']            = $width;
        $CI->image_lib->initialize($config);
        $CI->image_lib->resize();
        $CI->image_lib->clear();
    }

    return "<img src='".$image_url."' />";
}

Also, in the if file exists I put an echo "true", to see if it was getting to that part of the function or not, and it was echoing true. Which I would think would make the function work. But ya any help appreciated in advance.

El Forum

[eluser]lexusgs430[/eluser]
NM got it working, problem was somewhere with the if statement, not sure exactly what it was but taking it out fixed it.