j4v1 Posted June 2, 2008 Share Posted June 2, 2008 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 More sharing options...
DarkWater Posted June 2, 2008 Share Posted June 2, 2008 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 https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-555923 Share on other sites More sharing options...
j4v1 Posted June 2, 2008 Author Share Posted June 2, 2008 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 https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-555943 Share on other sites More sharing options...
DarkWater Posted June 2, 2008 Share Posted June 2, 2008 Where are you getting test1.gif from? The script? Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-555945 Share on other sites More sharing options...
j4v1 Posted June 2, 2008 Author Share Posted June 2, 2008 Correct. Once the script generates test1.gif. I go out and call it. Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-555950 Share on other sites More sharing options...
BlueSkyIS Posted June 2, 2008 Share Posted June 2, 2008 leave out the filename to have imagegif($dimg) print straight to browser. Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-555951 Share on other sites More sharing options...
j4v1 Posted June 2, 2008 Author Share Posted June 2, 2008 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 More sharing options...
j4v1 Posted June 2, 2008 Author Share Posted June 2, 2008 :-( Nada. It shows the a box w/ the x in it. No image displaying. Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-555957 Share on other sites More sharing options...
DarkWater Posted June 2, 2008 Share Posted June 2, 2008 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 More sharing options...
j4v1 Posted June 2, 2008 Author Share Posted June 2, 2008 Sorry for the delay No dice. Still the same thing. I ran the html file calling the getimage.php, and nothing. Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556004 Share on other sites More sharing options...
DarkWater Posted June 2, 2008 Share Posted June 2, 2008 Did you make the changes to getimage.php? I changed some lines. Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556008 Share on other sites More sharing options...
j4v1 Posted June 2, 2008 Author Share Posted June 2, 2008 Correct. In the HTML file, should I have the full path in the <img src...> to the php file? Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556010 Share on other sites More sharing options...
DarkWater Posted June 2, 2008 Share Posted June 2, 2008 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 https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556014 Share on other sites More sharing options...
j4v1 Posted June 2, 2008 Author Share Posted June 2, 2008 i'm lost. Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556022 Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 Learn to walk before you run? Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556043 Share on other sites More sharing options...
j4v1 Posted June 2, 2008 Author Share Posted June 2, 2008 Thank you, Mr. Miyagi. Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556045 Share on other sites More sharing options...
j4v1 Posted June 2, 2008 Author Share Posted June 2, 2008 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 More sharing options...
j4v1 Posted June 3, 2008 Author Share Posted June 3, 2008 Can anyone please assist? Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556493 Share on other sites More sharing options...
GingerRobot Posted June 3, 2008 Share Posted June 3, 2008 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 More sharing options...
j4v1 Posted June 3, 2008 Author Share Posted June 3, 2008 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 More sharing options...
j4v1 Posted June 3, 2008 Author Share Posted June 3, 2008 I researched regex again and here what I think you were asking me to do: if (preg_match("/http:\/\/images\.wsdot\.wa\.gov\/\nwflow\/\flowmaps\/\sysvert.gif/i", "$filename")) Link to comment https://forums.phpfreaks.com/topic/108438-calling-an-image/#findComment-556685 Share on other sites More sharing options...
GingerRobot Posted June 3, 2008 Share Posted June 3, 2008 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 More sharing options...
j4v1 Posted June 3, 2008 Author Share Posted June 3, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.