Jump to content

IF/THEN with Image


mcmuney

Recommended Posts

Basically, I have a php page, which shows an image based on the id attached to the link. For example, page.php?image_id=123, this page will display 123.jpg on that page. Now lets say that this image is removed or the file is corrupted. Now when people visit page.php?image_id=123, it'll simply show a bad image link.

 

What I want to do is use an if/then statement to redirect the user if the image link/path is bad.

Link to comment
Share on other sites

you could create a script with php GD to load the image e.g.

 

page.php:

<?php
function LoadJpeg($imgname)
{
    $im = @imagecreatefromjpeg($imgname); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im  = imagecreatetruecolor(300, 30); /* Create a black image */
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 300, 30, $bgc);
        /* Output an errmsg */
        imagestring($im, 16, 5, 5, "Error loading $imgname", $tc);
    }
    return $im;
}
header("Content-Type: image/jpeg");
$img = LoadJpeg($_GET['image_id'].".jpg");
imagejpeg($img);
?>

 

Scott.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.