spectacularstuff Posted July 9, 2008 Share Posted July 9, 2008 I am having some issues with GD progamming. GD is available on our server already confirmed with our hosting company. Todays Goal: Clear up all issues with this php script below so it displays all requested images in all browsers. Ultimate Goal: Be able to resize images using GD based upon screen width detected with JavaScript. I have the JavaScript code for that as well and I will include it below if anyone needs it. Problem: All of the code is written and works to an extent. I cannot figure out where this error is coming from and why. I think it may have something to do with how my arrays are written but I am unsure. Specific Problem Area: $width = imagesx($image); //This is the problem area $height = imagesy($image); //This is the problem area Can someone please help? I am heading to work right now and will not be back until 10pm or 11pm EST. I can respond to anything than however I am leaving you with everything that I have. Thank you for any help. - Wayne Website: Computer Repair Sarasota, Bradenton, Venice OR http://67.199.62.141/ whichever you prefer. Location of my the images on my server: /images/ (The array below will show you what images I am attempting to utilize for this) PHP GD Code <?php function open_image ($file) { // Get extension $extension = strrchr($file, '.'); $extension = strtolower($extension); switch($extension) { case '.jpg': case '.jpeg': $im = @imagecreatefromjpeg($file); break; case '.gif': $im = @imagecreatefromgif($file); break; case '.png': $im = @imagecreatefrompng($file); break; // ... etc default: $im = false; break; } return $im; } //Creating an array $image = array("/images/scrfix-logo.png", "/images/shopping-cart.jpg", "/images/forums.jpg", "/images/sign-in.jpg", "/images/contactus.jpg", "/images/printer.jpg"); // Load image for ($i=0; $i<count($image); $i++) { $image = $image[$i]; open_image($image); if ($image === false) { die ('Unable to open image'); } // This passes with no problems // Get original width and height $width = imagesx($image); //This is the problem area $height = imagesy($image); //This is the problem area // Could this be because the array includes /images/ in it? //I have the below commented out because it causes massive problems with it uncommented. I am attempting to take care of 1 issue at a time. As soon as we clear up the above issue I will uncomment the rest of this and find out why those issues are happening. /* // New width and height $new_width = 150; $new_height = 100; // Resample $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Display resized image header('Content-type: image/jpeg'); imagejpeg($image_resized); die('Extreme Error'); */ echo "<img src='" . $image . "' />"; } ?> PHP and JavaScript Resolution Detection Code <?php //Set the variable to Self Page $url = $_SERVER['PHP_SELF']; //Test for the cookie on the users computer if(isset($HTTP_COOKIE_VARS["res"])) $res = $HTTP_COOKIE_VARS["res"]; //If no cookie found. Create the cookie and redirect to same page. else{ ?> <script language="javascript"> <!-- go(); function go() { var today = new Date(); var the_date = new Date("August 31, 2020"); var the_cookie_date = the_date.toGMTString(); var the_cookie = "res="+ screen.width +"x"+ screen.height; var the_cookie = the_cookie + ";expires=" + the_cookie_date; document.cookie=the_cookie location = '<?echo "$url";?>'; } //--> </script> <?php } ?> <?php //Let's "split" the resolution results into two variables list($width, $height) = split('[x]', $res); //Take the width and height minus 300 $tb_width = $width-300; // This can be changed to whatever such as decimals (ie. 0.75). $tb_height = $height-300; // This can be changed to whatever such as decimals (ie. 0.75). //Make the table //This is here only to test the code and show that it works. The GD code above will go in its place print("<table align=center border=1 width=" .$tb_width . " height=" . $tb_height . " > <tr><td align=center>Your screen resolution is " . $width . " by " . $height . ".<br> The width/height of this table is " . $tb_width . " by " . $tb_height . ".</td></tr> </table>"); ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted July 9, 2008 Share Posted July 9, 2008 You're not 'catching' the returned images resource of your open_image function. Simply change open_image($image); to $image = open_image($image); in your for loop, and you should be good Quote Link to comment Share on other sites More sharing options...
spectacularstuff Posted July 9, 2008 Author Share Posted July 9, 2008 Okay, that worked however now I get the following error. Unable to open image Any ideas Quote Link to comment Share on other sites More sharing options...
spectacularstuff Posted July 9, 2008 Author Share Posted July 9, 2008 I put some debugging notes on the page to help. http://67.199.62.141/ Wayne Quote Link to comment Share on other sites More sharing options...
thebadbad Posted July 9, 2008 Share Posted July 9, 2008 Oh, just went through the rest of your code. An image resource created by e.g. imagecreatefromjpeg() can only be output to the browser using imagejpeg() along with the right header (what you have commented out in your script). And that's why echoing the image resource gives you nothing. Another thing; your for loop is useless unless you're saving the images, 'cause you can only output one per script. Just try to uncomment the whole thing, and we can try to work us through it. Quote Link to comment Share on other sites More sharing options...
spectacularstuff Posted July 9, 2008 Author Share Posted July 9, 2008 I will uncomment it but you will see some massive issues appear... lol I will have it uncommented in the next 2 minutes. Wayne Quote Link to comment Share on other sites More sharing options...
spectacularstuff Posted July 9, 2008 Author Share Posted July 9, 2008 I have run into some issues on the computer on this end. It will not let me into FTP. I downloaded Filezilla and installed it, looked into the filezilla start menu and 30 viral infected files were inside the directory. I downloaded it from sourceforge so I am not sure what is happening. I will look into this first and then change that file. sorry for the delay. Quote Link to comment Share on other sites More sharing options...
spectacularstuff Posted July 10, 2008 Author Share Posted July 10, 2008 Okay, I have the file changed now with everything uncommented however I still have the error that as soon as the file goes to $image = open_image($image); it loses the picture 100%. any ideas? Wayne Quote Link to comment Share on other sites More sharing options...
spectacularstuff Posted July 11, 2008 Author Share Posted July 11, 2008 Okay, Side Note Strange on the infections that showed up on the computer I was using. I uninstalled FileZilla and they went away, ran two different on demand AV scanners and they returned no infections. Current Issue I have moved this over to another page and tried some different very simple code to attempt to get 1 picture to show up. I am not sure what is happening. I still cannot get a picture to show up. I have the correct header information. I have the correct pathway. New URL: http://67.199.62.141/test3.php If you view source, will see I have attempted to output to browser and failed. 1. Reads as if I do not have proper header information but I do. Here is the new code: <?php header ('Content-Type: image/png'); // What type of image are we working with? $image = open_image('SCRFix-logo.png'); // Where is the image located? //Check to see if we can find the image if ($image === false) { die ('Unable to find image'); } echo 'Opened image'; //Did we successfully open this image. Please note that for some reason when we do find the image. This disappears. If I comment out everything below. This shows up and says open image. When I attempt to display the image, this no longer outputs to the browser. function open_image($file) { // What extension are we working with? $extension = strrchr($file, '.'); $extension = strtolower($extension); switch($extension) { case '.jpg': case '.jpeg': $im = @imagecreatefromjpeg($file); break; case '.gif': $im = @imagecreatefromgif($file); break; case '.png': $im = @imagecreatefrompng($file); break; // ... etc ... etc ...etc (I did not include the rest here to keep this short) default: $im = false; break; } return $im; } imagepng($image); // Output to browser directly through GD Code however this doesn't return an image. ?> I have not programmed GD before so this is a learning process. Please help... Thanks, Wayne Quote Link to comment Share on other sites More sharing options...
thebadbad Posted July 11, 2008 Share Posted July 11, 2008 You must understand that when you're outputting an image (not as part of a page, but an image itself), you can't output anything else for obvious reasons. So remove echo 'Opened image'; and it should work. Quote Link to comment Share on other sites More sharing options...
spectacularstuff Posted July 11, 2008 Author Share Posted July 11, 2008 That was it! Thanks. No, I didn't realize that I couldn't output that within that code. I had kept it out of the GD code. I didn't realize that I couldn't have ANY other output Thank you for your help. That is one issue down and the rest to go. Now that I have that set up, I am going to work on the for statement for outputting multiple pictures from an array of images. If I run into issues with that I will post again. Thanks again. Wayne Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.