image display problem!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

image display problem!!

Post by kanchan »

i have a link : http://www.test.com/thumb.php?img=wallp ... =800&h=600

when a user click on that link, that link should open to a new window and display the image but instead of displaying the image, it automatically downloads the image..

So how to display the image in new window??
could you please help me out??

below is the code for thumb.php

Code: Select all

 
<?
function thumb_on_fly($img,$h=null,$w=null,$max=80)
{
    if(file_exists($img))
    {
        if( isset($h) && isset($w) )
        {
            $s = getimagesize($img);
            $sx = $s[0];
            $sy = $s[1];
            $source = imagecreatefromjpeg($img);
            $target = imagecreatetruecolor($w, $h);
            $copy = imagecopyresampled($target, $source, 0, 0, 0, 0, $w, $h, $sx, $sy);
            $image = imagejpeg($target);
            imagedestroy($target);
            return $image;
        }
        else // scale automatically until < $max while keeping aspect ratios
        {
            $s = getimagesize($img);
            $sx = $s[0];
            $sy = $s[1];
 
            if($sx > $sy/2)
            {
                $nw=$max;
                $nh=($sy/$sx)*$nw;
            }
            else
            {
                $nh=$max*2;
                $nw=($sx/$sy)*$nh;
            }
 
            $source = imagecreatefromjpeg($img);
            $target = imagecreatetruecolor($nw, $nh);
            $copy = imagecopyresampled($target, $source, 0, 0, 0, 0, $nw, $nh, $sx, $sy);
            $image = imagejpeg($target);
            imagedestroy($target);
            return $image;
        }
    }
}
 
if ( isset($_GET['img']) && !isset($_GET['h']) && !isset($_GET['w']) && !isset($_GET['max']) )
{
    header("content-type: image/jpg");
    thumb_on_fly($_GET['img']);
}
elseif ( isset($_GET['img']) && isset($_GET['h']) && isset($_GET['w']) && !isset($_GET['max']) )
{
    header("content-type: image/jpg");
    thumb_on_fly($_GET['img'],$_GET['h'],$_GET['w']);
}
elseif ( isset($_GET['img']) && !isset($_GET['h']) && !isset($_GET['w']) && isset($_GET['max']) )
{
    header("content-type: image/jpg");
    thumb_on_fly($_GET['img'],null,null,$_GET['max']);
}
?>
 
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: image display problem!!

Post by starram »

Hey, IN your code I don't see any link, where you create hyperlink for the image.

what you can do is, Create a hyperlink for the image & set the target="_blank"

thanks
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: image display problem!!

Post by kanchan »

http://www.test.com/thumb.php?img=wallp ... =800&h=600

this is the hyperlink, opens new window but the problem is, it doesnt diplay the image, it directly download the image as soon as the new window opens....
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: image display problem!!

Post by andyhoneycutt »

does this problem occur across all browsers?
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: image display problem!!

Post by kanchan »

yeah i guess....

i want the opening window should display the resized image instead of direct downloading.....


:roll: :roll: :roll:
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: image display problem!!

Post by starram »

Please create a proper hyperlink for the image. I wnt on the same link and couldn't find the image you want to show.

You can type something like this.

styles/subsilver2/imageset/site_logo.gif
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: image display problem!!

Post by kanchan »

http://www.merobike.com/test/thumb.php? ... =800&h=600

try this out.. this link directly downloads the image instead of showing in the browser

do you know why??
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: image display problem!!

Post by starram »

It is behaving like that due to following code.

if ( isset($_GET['img']) && !isset($_GET['h']) && !isset($_GET['w']) && !isset($_GET['max']) )
{
header("content-type: image/jpg");
thumb_on_fly($_GET['img']);
}
elseif ( isset($_GET['img']) && isset($_GET['h']) && isset($_GET['w']) && !isset($_GET['max']) )
{
header("content-type: image/jpg");
thumb_on_fly($_GET['img'],$_GET['h'],$_GET['w']);
}
elseif ( isset($_GET['img']) && !isset($_GET['h']) && !isset($_GET['w']) && isset($_GET['max']) )
{
header("content-type: image/jpg");
thumb_on_fly($_GET['img'],null,null,$_GET['max']);
}


Try this

if ( isset($_GET['img']) && !isset($_GET['h']) && !isset($_GET['w']) && !isset($_GET['max']) )
{
echo "<a href=url/$_GET[img]>title</a>";
}
elseif ( isset($_GET['img']) && isset($_GET['h']) && isset($_GET['w']) && !isset($_GET['max']) )
{
echo "<a href=url/$_GET[img]>title</a>";
}
elseif ( isset($_GET['img']) && !isset($_GET['h']) && !isset($_GET['w']) && isset($_GET['max']) )
{
echo "<a href=url/$_GET[img]>title</a>";
}
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: image display problem!!

Post by kanchan »

if ( isset($_GET['img']) && !isset($_GET['h']) && !isset($_GET['w']) && !isset($_GET['max']) )
{
echo "<a href=url/$_GET[img]>title</a>";
}
elseif ( isset($_GET['img']) && isset($_GET['h']) && isset($_GET['w']) && !isset($_GET['max']) )
{
echo "<a href=url/$_GET[img]>title</a>";
}
elseif ( isset($_GET['img']) && !isset($_GET['h']) && !isset($_GET['w']) && isset($_GET['max']) )
{
echo "<a href=url/$_GET[img]>title</a>";
}
will that code generate the image to defined height and width i.e : 800x600 ??
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: image display problem!!

Post by andyhoneycutt »

I'm running firefox 3.0.1 and the page displays an image for me, does not direct download. There are issues with IE 7 and direct downloads when they should be displayed in the browser. Have you tested this across multiple browsers?
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: image display problem!!

Post by starram »

No it will display the original image. U can try lot of other things for that. Like passing the image id or name and setting width and height their with img tag.

Another thing u can re-size the image.
Post Reply