Howdy all.

Trying to create an image identifier ($src_img) using imagecreatefromjpeg(), and PHP keeps telling me that when I attempt to use $src_img, it is not a valid resource. Here's my code:

The $id passed to the function comes from [FONT=century gothic]$id = mysql_insert_id($link_id)[/FONT] My images are stored in a folder with the format $id_[name of uploaded image].xxx.

function thumbnail($id) {

global $upload_adFullimg_dir, $thumb_w, $thumb_h, $src_img;

$full_img = $_FILES['adFullimg']['type'];

if($full_img == "image/pjpeg"){
$path = "$upload_adFullimg_dir/$id" . "_" .  basename($_FILES['adFullimg']['name']);
echo $path;
$src_image = imagecreatefromjpeg($path);
echo "JPG ran.";
echo $src_img; }
else {
$src_image = imagecreatefrompng("$upload_adFullimg_dir$id" . "_" . basename($_FILES['adFullimg']['name']));
echo "PNG ran.";
echo $src_img; }

//Preserve aspect ratio.
//Code courtesy of Christian Heilmann @ http://www.onlinetools.org/articles/creating_thumbnails_all.php
$old_x = imagesx($src_img);
$old_y = imagesy($src_img);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x
($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}

The echo's are my debugging statements. Here's what the output looks like:

ebunny.jpg has been successfully uploaded.
Filesize: 12,385
Filetype: image/pjpeg
path/27_ebunny.jpgmy echo statementJPG ran.
Warning: imagesx(): supplied argument is not a valid Image resource in path/success.php on line 134

Warning: imagesy(): supplied argument is not a valid Image resource in path/success.php on line 135

Warning: imagecopyresized(): supplied argument is not a valid Image resource in path/success.php on line 150

So, I've narrowed it down to the $src_img as being an empty string. I've already tried echoing it, and it is blank.

phpinfo() reports my gd version as 1.6.2 and higher.

Any help would be greatly appreciated.

    The function imagecreatefromjpeg returns an image identifier, not a string.

    Not sure if it will work, but the function print_r($var) will display arrays, objects, and maybe (?) resources like this.

    Try:

    echo "<pre>";
    print_r($src_image);
    echo "</pre>";

    HTH

      I don't think that's going to help. The problem is that imagecreatejpeg() is supposed to return a resource identifier. I'm storing it in a variable and supplying the resource to imagesx() which specifically calls for a resource variable. So, wazzup with that?

        I found this at http://us4.php.net/manual/en/function.imagecreatefromjpeg.php

        function LoadJpeg ($imgname) {
            $im = @imagecreatefromjpeg ($imgname); /* Attempt to open */
            if (!$im) { /* See if it failed */
                $im  = imagecreate (150, 30); /* Create a blank image */
                $bgc = imagecolorallocate ($im, 255, 255, 255);
                $tc  = imagecolorallocate ($im, 0, 0, 0);
                imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
                /* Output an errmsg */
                imagestring ($im, 1, 5, 5, "Error loading $imgname", $tc);
            }
            return $im;
        }

        You were right, print_r doesn't help... sorry.

        I would check file permissions on the file, and double check the path make sure it's absolute, or that it can be found if you are using a relative one.

          I've checked the path, and it's good. I know it's finding the file. The permissions thing is interesting, though I would expect PHP to spit out some kind of error message about not having permissions. Anyway, I checke them, and they're rwxr-xr-x. Not sure if that's a problem or not. (I know very little about Unix). I'm going to investigate changing the file permissions between my file upload and my call to make the thumbnail. Perhaps that will be the answer. Thanks for the help.

          I found that code you posted, as well. That thing strikes me as just an error checker. And even then, you still don't have a copy of your original image file. Heavy sigh

          Thanks

          E

            Looking at your code again, it looks like your first if statement is exluding any JPG files from getting to the thumbnail resizing code.

            Another thing to do that helps debugging is bumping the error reporting level all the way up with error_reporting (E_ALL);

            Also, are you sure you want to echo anything before you echo the actual image? The browser needs to receive only the binary image data. Maybe you just stuck that in there for debugging, I understand, just FYI. And yeah, that other code was only for trying to display the errors a little better.

            We'll get it... 🆒

              My code is a bit contrived for debugging purposes at this point. Specifically, the files I'm uploading to the server are pjpeg, so that's all I'm testing for at the moment. I'll go back and add some kind of ereg() matching to cover my bases when I get the code working properly.

              At this point, I'm not attempting to display anything on the browser. My goal here is to create a thumbnail image and save it to file. I'll store the path to the full and thumb in my database and just fetch the paths to the images when the page is called. I don't know if that is an efficient manner or not, but I was befuzzled by trying to set up mySQL to store actual images. :p

              I'm surprised that there are virtually no references to this kind of problem with this function on the Web. Maybe mine is unique!

              E

                OK, this is become curiouser and curiouser, as they say...

                I tested the $src_image variable using [FONT=courier new]is_resource()[/FONT] and sure enough, it is! What's more, I echo'd its type, and it's a gd type. So the resource being returned is just fine.

                There must be an issue with my gd implementation. I'll have to speak to my guru about that.

                E

                  Yeah, sounds like a plan... good luck with it!

                  I didn't know about the is_resource function, that's a handy one to keep around.

                  I would lean away from storing the images in MySQL, mainly because it is alot harder/impossible to do a proper dump to a text file (for backup purposes) with large BLOB fields.

                  Later...

                    Write a Reply...