Jump to content

[SOLVED] PHP GD Error: imagesx(): supplied argument is not a valid Image resource in


Recommended Posts

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>");
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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... :D

 

Thanks,

 

Wayne

Link to comment
Share on other sites

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

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.