Jump to content

Calling an image


j4v1

Recommended Posts

A few quick questions that would truly help me complete a project i'm working on.

 

I have the following script that generates an image. The script works great, but heres where I'm stuck:

 

1. When calling the image it doesn't pull the latest (most recent), because the script is slow in creating the image.

2. What is the best way to call the image?

3. Can I execute this script in an html?

 

 

<?php
resize("./", "http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif", "538", "./");
function resize($cur_dir, $cur_file, $newwidth, $output_dir)
{
    $dir_name = $cur_dir;
    $olddir = getcwd();
    $dir = opendir($dir_name);
    $filename = $cur_file;
    $format='image/gif';
    if(preg_match("/.jpg/i", "$filename"))
    {
        $format = 'image/jpeg';
    }
    if (preg_match("/.gif/i", "$filename"))
    {
        $format = 'image/gif';
    }
    if(preg_match("/.png/i", "$filename"))
    {
        $format = 'image/png';
    }
    if($format!='')
    {
        list($width, $height) = getimagesize($filename);
        $newheight=$height*$newwidth/$width;
        switch($format)
        {
            case 'image/jpeg':
            $source = imagecreatefromjpeg($filename);
            break;
            case 'image/gif';
            $source = imagecreatefromgif($filename);
            break;
            case 'image/png':
            $source = imagecreatefrompng($filename);
            break;
        }
        $dimg = imagecreatetruecolor(640,538);
        imagealphablending($dimg, false);
        $source = @imagecreatefromgif("$filename");
        imagecopyresized($dimg, $source, 0,0,0,438, $newwidth, $newheight, $width, $height);
        $filename="$output_dir/test1.gif";
        @imagegif($dimg,$filename);
    }
}
?>

 

Any assistance would truly be appreciated

 

THANK YOU!

J4v1

Link to comment
Share on other sites

1) What do you mean "doesn't pull the most recent"?

 

2) By call the image, do you mean in an <img> tag, or in a PHP script.

 

3) PHP code MUST be run in a .php file, or you can make Apache treat html files as php, but you might not be able to do that.

Link to comment
Share on other sites

Thanks for the fast reply. I'm truly in a bind and have not found any real help.

Correct,

Here what I have and the bottom of the script.  Forgot to add the text:

<html>
<body> <img src="test1.gif"></body>
</html>

Link to comment
Share on other sites

Ok. I'm trying that right now.

 

So you're sayin' to make look like this, new snippet code at the bottom

<?php
[code=php:0]header('refresh:120; url=gold.php');

resize("./", "http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif", "538", "./");

function resize($cur_dir, $cur_file, $newwidth, $output_dir)

{

    $dir_name = $cur_dir;

    $olddir = getcwd();

    $dir = opendir($dir_name);

    $filename = $cur_file;

    $format='image/gif';

    if(preg_match("/.jpg/i", "$filename"))

    {

        $format = 'image/jpeg';

    }

    if (preg_match("/.gif/i", "$filename"))

    {

        $format = 'image/gif';

    }

    if(preg_match("/.png/i", "$filename"))

    {

        $format = 'image/png';

    }

    if($format!='')

    {

        list($width, $height) = getimagesize($filename);

        $newheight=$height*$newwidth/$width;

        switch($format)

        {

            case 'image/jpeg':

            $source = imagecreatefromjpeg($filename);

            break;

            case 'image/gif';

            $source = imagecreatefromgif($filename);

            break;

            case 'image/png':

            $source = imagecreatefrompng($filename);

            break;

        }

        $dimg = imagecreatetruecolor(640,538);

        imagealphablending($dimg, false);

        $source = @imagecreatefromgif("$filename");

        imagecopyresized($dimg, $source, 0,0,0,438, $newwidth, $newheight, $width, $height);

        $filename="$output_dir/test1.gif";

        @imagegif($dimg,$filename);

    }

}

?>

<html>

<body> <img src="imagegif($dimg)"></body>

</html>

[/code]

Link to comment
Share on other sites

No.  Do this:

 

<?php
header('refresh:120; url=gold.php');
resize("./", "http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif", "538", "./");
function resize($cur_dir, $cur_file, $newwidth, $output_dir)
{
    $dir_name = $cur_dir;
    $olddir = getcwd();
    $dir = opendir($dir_name);
    $filename = $cur_file;
    $format='image/gif';
    if(preg_match("/.jpg/i", "$filename"))
    {
        $format = 'image/jpeg';
    }
    if (preg_match("/.gif/i", "$filename"))
    {
        $format = 'image/gif';
    }
    if(preg_match("/.png/i", "$filename"))
    {
        $format = 'image/png';
    }
    if($format!='')
    {
        list($width, $height) = getimagesize($filename);
        $newheight=$height*$newwidth/$width;
        switch($format)
        {
            case 'image/jpeg':
            $source = imagecreatefromjpeg($filename);
            break;
            case 'image/gif';
            $source = imagecreatefromgif($filename);
            break;
            case 'image/png':
            $source = imagecreatefrompng($filename);
            break;
        }
        $dimg = imagecreatetruecolor(640,538);
        imagealphablending($dimg, false);
        $source = @imagecreatefromgif("$filename");
        imagecopyresized($dimg, $source, 0,0,0,438, $newwidth, $newheight, $width, $height);
        $filename="$output_dir/test1.gif";
        header('Content-type: image/gif');
        header('Pragma: no-cache');
        @imagegif($dimg);
    }
}
?>

 

Then, in ANOTHER file (an HTML file even), do:

<img src="getimage.php" />

 

As a completely separate file.  Name it getimage.php.

Link to comment
Share on other sites

Oh well duh.  You never call the function.  Here's what you have to do now though.  Remove the function declaration (and the { }), and then make the function parameters into GET variables, so that way you can call getimage.php?size=15 in the image tag.

Link to comment
Share on other sites

Thank you for helpin' me out.  I've been stuck on this problem for too long and its getting down to my deadline.

 

The script does what I want.  I generates the correct image that I need.  All that I'm looking to do is have the script execute, everytime I call the image to display on the browser.

 

Am I going about this totally, wrong.

Link to comment
Share on other sites

Oh well duh.  You never call the function. 

 

Whats this line then?

 

resize("./", "http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif", "538", "./");

 

 


 

As for the problem:

 

1.) Remove the error surpressors (@) -- how do you hope to find errors if you're surpressing them?

2.) You're creating from a gif (imagecreatefromgif) yet appear to want support for other image types.

3.) Your regex is going to be problematic. Taking your regex for png, for example:

 

"/.jpg/i"

 

This will match any string with the text jpg appearing after any character. That is, both of the following would match: image.jpg, ajpg.png. The . character is a special character, meaning any character. You should escape is and make the regex check only the end of the string:

 

if(preg_match("/\.jpg$/i", $filename))

 

And finally, you should get in the habit of posting your updated code. Its much easier for people to follow.

Link to comment
Share on other sites

Thanks!!

I'll answer the first question:

 

Take the image from its current directory and sizing down and place it in the local current folder, when I call resize.

<?php
resize("./", "http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif", "538", "./");

 

 

I've updated the script w/ your comments.

<?php
resize("./", "http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif", "538", "./");
function resize($cur_dir, $cur_file, $newwidth, $output_dir)
{
    $dir_name = $cur_dir;
    $olddir = getcwd();
    $dir = opendir($dir_name);
    $filename = $cur_file;
    $format='image/gif';
    if (preg_match("http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif"/i, "$filename"))
    {
        $format = 'image/gif';
    }
    if($format!='')
    {
        list($width, $height) = getimagesize($filename);
        $newheight=$height*$newwidth/$width;
        switch($format)
        {
            case 'image/gif';
            $source = imagecreatefromgif($filename);
            break;
        }
        $dimg = imagecreatetruecolor(640,538);
        imagealphablending($dimg, false);
        $source = imagecreatefromgif("$filename");
        imagecopyresized($dimg, $source, 0,0,0,438, $newwidth, $newheight, $width, $height);
        $filename="$output_dir/test1.gif";
        imagegif($dimg,$filename);
    }
}
?>

Link to comment
Share on other sites

No, i was actually just suggesting (as i posted) this:

 

if (preg_match("|\.gif$|i", $filename)){

 

I notice you would have had a parse error in the above. I wonder if you have the display of errors turned off. Add this:

 

error_reporting(E_ALL);
ini_set('display_errors','On');

 

To the top of your code.  I also notice you're not setting the correct header. You'll need to tell the browser what it's expecting. Thus the code is:

 

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
resize("./", "http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif", "538", "./");
function resize($cur_dir, $cur_file, $newwidth, $output_dir)
{
    $dir_name = $cur_dir;
    $olddir = getcwd();
    $dir = opendir($dir_name);
    $filename = $cur_file;
    if (preg_match("|\.gif$|i", $filename))
    {
        $format = 'image/gif';
    }
    if($format!='')
    {
        list($width, $height) = getimagesize($filename);
        $newheight=$height*$newwidth/$width;
        switch($format)
        {
            case 'image/gif';
            $source = imagecreatefromgif($filename);
            break;
        }
        $dimg = imagecreatetruecolor(640,538);
        imagealphablending($dimg, false);
        $source = imagecreatefromgif($filename);
        imagecopyresized($dimg, $source, 0,0,0,438, $newwidth, $newheight, $width, $height);
        header("Content-type: image/gif");
        imagegif($dimg);
    }
}
?>

Link to comment
Share on other sites

Ok.  I'll give that a shot, but how should I go about executing the script and then calling the generated image, all from an html file?

 

Also, I just restarted and now everytime I try to preview I get the following error:

HTTP Error 500-  The required operations requires elevation
.  Not sure what Vista is doing, but its blocking me from see any of the code. 
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.