Topic: Image in the Item

Hi,
I wish to change the way the image is handled in the Item creation. I wish to insert a URL rather than upload an image, but I could not locate where in the database the reference to the uploaded image is made.

Any clue on where the image is referenced?

Thanks in advance
carmelo

Re: Image in the Item

The uploaded image has the same filename as the item id with the extension .jpg . It is not possible to change this without a major change in the source.

/Joe

Re: Image in the Item

Thanks Joe,

Actually this may require less intervention than it seems. Instead of saving a .jpg file I manage to save a url written in a text file saved as .txt. Then when the image is called the file is opened and the url used as part of a <img src=URL />.

What do you think?

Thanks, Carmelo

Re: Image in the Item

Well, if it works, this might be a candidate to the wiki. Will you update the wiki?

https://frontaccounting.com/fawiki

Joe

Re: Image in the Item

I will try to get it to work as explained. If I am successful I would be pleased to send the code changes.

Carmelo

Re: Image in the Item

Ah, if we need to change the core, we might think of a global flag in the config.php file.

Joe

Re: Image in the Item

Hi Joe,
Having a flag in the config file would be nice.
I did the necessary changes to have the image link to a url. The following are the changes I did. Two files were affected.

I hope this is considered useful and implemented in the core for version 2.4 (or before)

-----------------------------------------------------------------------------------------------
    Change Item Image to URL
-----------------------------------------------------------------------------------------------
fa/inventory/manage/items.php

Insert in Line 54 right after: $Ajax->activate('details');

if (isset($_POST['url']) && $_POST['url'] != '')
{
    $url = trim($_POST['url']);
    if (strpos($url, 'http') !== false)
    {
        $stock_id = $_POST['NewStockID'];
        $upload_file = 'Yes'; //Assume all is well to start off with
        $filename = company_path().'/images';
        if (!file_exists($filename))
        {
            mkdir($filename);
        }   
        $filename .= "/".item_img_name($stock_id).".url";
        $handle = fopen($filename, 'w');
        fwrite($handle, $url);
        $result=fclose($handle);
        if (!$result)
        {
            display_error(_('The link could not be saved'));
            $upload_file ='No';
        }
    }
}

--------------------------------------------------
~ Line 199 : REPLACE
            $filename = company_path().'/images/'.item_img_name($_POST['NewStockID']).".jpg";
            if (file_exists($filename))
                unlink($filename);

WITH:
            $filename = company_path().'/images/'.item_img_name($_POST['NewStockID']).".jpg";
            if (file_exists($filename))
                unlink($filename);
            $filename = company_path().'/images/'.item_img_name($_POST['NewStockID']).".url";
            if (file_exists($filename))
                unlink($filename);
               
--------------------------------------------------
~ Line 276 : REPLACE
        $filename = company_path().'/images/'.item_img_name($stock_id).".jpg";
        if (file_exists($filename))
            unlink($filename);

WITH:
        $filename = company_path().'/images/'.item_img_name($stock_id).".jpg";
        if (file_exists($filename))
            unlink($filename);
        $filename = company_path().'/images/'.item_img_name($stock_id).".url";
        if (file_exists($filename))
            unlink($filename);

-------------------------------------------------   
around line 418 REPLACE:
        // Add image upload for New Item  - by Joe
        file_row(_("Image File (.jpg)") . ":", 'pic', 'pic');
        // Add Image upload for New Item  - by Joe
        $stock_img_link = "";
        $check_remove_image = false;
        if (isset($_POST['NewStockID']) && file_exists(company_path().'/images/'
            .item_img_name($_POST['NewStockID']).".jpg"))
        {
         // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
            $stock_img_link .= "<img id='item_img' alt = '[".$_POST['NewStockID'].".jpg".
                "]' src='".company_path().'/images/'.item_img_name($_POST['NewStockID']).
                ".jpg?nocache=".rand()."'"." height='$pic_height' border='0'>";
            $check_remove_image = true;
        }
        else
        {
            $stock_img_link .= _("No image");
        }

WITH:
       
    // URL link for images added by Carmelo Romano on 25/07/2013
    $stock_img_link = "";
    $check_remove_image = false;
    url_row(_("Image Link") . ":", 'url', 'url');
    if (isset($_POST['NewStockID']) && file_exists(company_path().'/images/'
        .item_img_name($_POST['NewStockID']).".url"))
    {
        $url="";
        $filename=company_path().'/images/'.item_img_name($_POST['NewStockID']).".url";
        $handle = @fopen($filename, r);
       
        if ($handle) {
            $url = fgets($handle, 1024);
        }
       
        if (!empty($url)) {
            $stock_img_link .= "<a href='".$url."' target='_766266'>
                                <img id='item_img' alt = '[".$_POST["NewStockID"].".jpg"."]'
                                    src='".$url."'"." height='$pic_height' border='0'><br>
                                        Open image in New Window</a>";
           
            $check_remove_image = true;
        }
        fclose($handle);
    }
    else
    {   
        // Add image upload for New Item  - by Joe
        file_row(_("Image File (.jpg)") . ":", 'pic', 'pic');
        // Add Image upload for New Item  - by Joe
        $stock_img_link = "";
        $check_remove_image = false;
        if (isset($_POST['NewStockID']) && file_exists(company_path().'/images/'
            .item_img_name($_POST['NewStockID']).".jpg"))
        {
         // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
            $stock_img_link .= "<img id='item_img' alt = '[".$_POST['NewStockID'].".jpg".
                "]' src='".company_path().'/images/'.item_img_name($_POST['NewStockID']).
                ".jpg?nocache=".rand()."'"." height='$pic_height' border='0'>";
            $check_remove_image = true;
        }
        else
        {
            $stock_img_link .= _("No image");
        }
    }

------------------------------------------------   
fa/includes/ui/ui_input.inc

ADD:
//-----------------------------------------------------------------------------------
function url_cells($label, $name, $id="")
{
    if ($id != "")
        $id = "id='$id'";
    label_cells($label, "<input type='text' name='$name' size='35' />");
}       
function url_row($label, $name, $id = "")
{
    echo "<tr><td class='label'>$label</td>";
    url_cells(null, $name, $id);
    echo "</tr>\n";
}   


Regards, carmelo

Re: Image in the Item

I'm not the developer here, but if I may make a humble suggestion...

There are perfectly good reasons to store binary data in the filesystem, and not as a blob in the database. But there is no reason whatsoever to store a string in a file, when you have a database.

Currently the item id seems to do double duty as a filename field, which is fine if all files are local. And if no item needs to have more than one image associated with it.

Since a URL can point to a local or a remote file, it is clearly a lot more flexible. But then it should be stored in the database. Either as a new field in the item table, or in a new table. The latter allows an item to have more than one image, which would be an improvement in itself. (That part could be added later, if there was already support at the database level for it. And if someone actually needed it.)

If anything is to be merged into the trunk, it should not only be an improvement. It should also be as general and flexible as possible. Duplicating functionality and selecting the appropriate execution paths based on a config file flag may not be the best way forward here.

But fortunately, reading/writing the URL from/to the database should be no more complicated than what you're doing now. And it should be easily handled by an update script. (Just create the new field/table and populate it with the paths to each file in whatever subdirectory the images are currently stored.) The image upload page would need to handle files as well as URL's, but not necessarily at the same time. Initially, you could just overwrite the current entry with the first non-empty field supplied. And later, it could be extended to support multiple images.

Re: Image in the Item

@tm totally agree.
The reason I took the route I did was so as not to change the database mid-way through a version.
There will be database changes in v2.4 but not within v2.3 although I stand corrected.

So what I did was that I took the current functionality and saved the url in the file rather than the image.
This may slow down the system if there are 1000s of items, so having the url's stored in the database would be a much better solution.

Cheers, Carmelo

Re: Image in the Item

@Carmelo

Maybe we should postpone this change until release 2.4 . And make it a multi choice of images.
Carmelo, you can use your changes in the meantime, but please have a backup of your new files, so you can restore them during eventually minor releases in 2.3.

/Joe