• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to link image with anchor function

#1
[eluser]umefarooq[/eluser]
Hi
i want to put link on an image but anchor() function is working for text what about if i want to put link to an image any helper function for this.

#2
[eluser]rogierb[/eluser]
I use a customs function for images:
Code:
function anchor_image($url, $image, $attributes = '', $image_attributes = '') {
  $url ? $link_url = site_url($url) : $link_url ="[removed]void(0);";
  $attributes = _parse_attributes($attributes);
  $image_attributes = _parse_attributes($image_attributes);
  return '<a href="'.$link_url.'"><img src="'.base_url().$image.'" /></a>';
}

Edit: Code is f*cked:
Use: ja-va-script: where is says [removed]

Just remove the -.

#3
[eluser]Yash[/eluser]
simple just use anchor_url and in second parameter a pass a string as <img src=" '.base_url().'source/image.jpg' '" />

#4
[eluser]umefarooq[/eluser]
hi
here is my code for the link an image using CI anchor and img functions.
Code:
function anchor_img($uri = '',$img = '', $title = '', $attributes = '')
    {
        $title = (string) $title;
        
        if ( ! is_array($uri))
        {
            $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
        }
        else
        {
            $site_url = site_url($uri);
        }

        if ($title == '')
        {
            $title = $site_url;
        }

        if ($attributes != '')
        {
            $attributes = _parse_attributes($attributes);
        }

        return '<a href="'.$site_url.'">'.img($img).'</a>';
    }

and here how we will pass arguments in two different ways
Code:
anchor_img('test.htm',array('src'=>'admin/images/unpublish.png','border'=>'0'),'testing image')

second way
Code:
anchor_img('test.htm','admin/images/unpublish.png','testing image')

#5
[eluser]umefarooq[/eluser]
little more change in function

Code:
function anchor_img($uri = '',$img = '',$attributes = '')
    {
        
        if ( ! is_array($uri))
        {
            $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
        }
        else
        {
            $site_url = site_url($uri);
        }

        if ($attributes != '')
        {
            $attributes = _parse_attributes($attributes);
        }

        return '<a href="'.$site_url.'">'.img($img).'</a>';
    }

#6
[eluser]xwero[/eluser]
The problem with the custom functions is that they are limiting the possibilities to generate the a tag as you want. The anchor function doesn't check if the second argument contains no tags so you can do
Code:
anchor('test',img(array('src'=>'admin/images/unpublish.png','border'=>'0','alt'=>'testing image')),array('class'=>'imglink'));

#7
[eluser]umefarooq[/eluser]
wow really nice an simple one line of code.. thanks for help

#8
[eluser]ayukawaa[/eluser]
The answer of xwero does not work, I have found that the simplest form of doing it without bother with custom functions is using %s as the anchor() title and then replace the %s with the img() using printf

I have put it in many lines to better read:

Code:
printf
(
    anchor
    (
        'welcome/index',
        '%s',
        array
        (
            'title'=>'Logo Inc.'
        )
    ),
    img
    (
        array
        (
            'src'=>base_url().'images/logo.gif',
            'border'=>'0',
            'width'=>'20',
            'height'=>'10',
            'alt'=>'Logo'
        )
    )
);

This way is simply and clear because anchor() and img() are CI internal functions.

Note:
- you must use the title="something" in the anchor (if not, CI will add title="%s" )
- the img() is in the html helper

^_^

#9
[eluser]Eason[/eluser]
Here is my version of anchor_img() function:

Code:
/**
* @param    string   identical to the 1st param of anchor()
* @param    mixed    identical to the 3rd param of anchor()
* @param    string   the path to the image; it can be either an external one
*                    starting by "http://", or internal to your application
* @param    mixed    image attributes that have similar structure as the 3rd param of anchor()
* @return   string
*
* Example 1: anchor_img('controller/method', 'title="My title"', 'path/to/the/image.jpg', 'alt="My image"')
* Example 2: anchor_img('http://example.com', array('title' => 'My title'), 'http://example.com/image.jpg', array('alt' => 'My image'))
*/

function anchor_img($uri = '', $anchor_attributes = '', $img_src = '', $img_attributes = '')
{
    if ( ! is_array($uri))
    {
        $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
    }
    else
    {
        $site_url = site_url($uri);
    }

    if ($anchor_attributes != '')
    {
        $anchor_attributes = _parse_attributes($anchor_attributes);
    }
    
    if (strpos($img_src, '://') === FALSE)
    {
        $CI =& get_instance();
        $img_src = $CI->config->slash_item('base_url').$img_src;
    }
    
    if ($img_attributes != '')
    {
        $img_attributes = _parse_attributes($img_attributes);
    }

    return '<a href="'.$site_url.'">'.'<img src="'.$img_src.'" />'.'</a>';
}

To make it works, this function should be placed in system/helpers/url_helper.php or system/application/helpers/MY_url_helper.php

#10
[eluser]elaniobro[/eluser]
[quote author="Eason" date="1253874144"]Here is my version of anchor_img() function:

Code:
/**
* @param    string   identical to the 1st param of anchor()
* @param    mixed    identical to the 3rd param of anchor()
* @param    string   the path to the image; it can be either an external one
*                    starting by "http://", or internal to your application
* @param    mixed    image attributes that have similar structure as the 3rd param of anchor()
* @return   string
*
* Example 1: anchor_img('controller/method', 'title="My title"', 'path/to/the/image.jpg', 'alt="My image"')
* Example 2: anchor_img('http://example.com', array('title' => 'My title'), 'http://example.com/image.jpg', array('alt' => 'My image'))
*/

function anchor_img($uri = '', $anchor_attributes = '', $img_src = '', $img_attributes = '')
{
    if ( ! is_array($uri))
    {
        $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
    }
    else
    {
        $site_url = site_url($uri);
    }

    if ($anchor_attributes != '')
    {
        $anchor_attributes = _parse_attributes($anchor_attributes);
    }
    
    if (strpos($img_src, '://') === FALSE)
    {
        $CI =& get_instance();
        $img_src = $CI->config->slash_item('base_url').$img_src;
    }
    
    if ($img_attributes != '')
    {
        $img_attributes = _parse_attributes($img_attributes);
    }

    return '<a href="'.$site_url.'">'.'<img src="'.$img_src.'" />'.'</a>';
}

To make it works, this function should be placed in system/helpers/url_helper.php or system/application/helpers/MY_url_helper.php[/quote]


So I put this code into the system/helpers/url_helper.php on line 220 right after the anchor popup function.

I tried using this line of code but nothing is showing up on my site:
Code:
&lt;?= anchor_img('/resume/download',array('src'=>'img/resume/resume_dl.gif','border'=>'0'),'testing image') ?&gt;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.