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
https://forums.phpfreaks.com/topic/108438-calling-an-image/
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
https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-555954
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
https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-555962
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
https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556054
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
https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556590
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
https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556618
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
https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556773
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
https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556810
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.