SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    imagecopyresampled() not working, "supplied argument is not a valid Image resource"

    Okay, so I am trying to write a function for making thumbnails, here's the function (you can ignore all the string functions, that all works properly):
    PHP Code:
    function makeThumbnail($originalImage$username)
    {
        
    $sidemax 250;
        
        
    $removestr $_SERVER['DOCUMENT_ROOT'] . 'users/' $username '/images/';
        
    $removelen strlen($removestr);
        
    $originalImageName substr_replace($originalImage''0$removelen);
        
    $thumbnailName 'thm-' $originalImageName;
        
    $thumbnailName $removestr $thumbnailName;
        
        
    $dimensions getimagesize($originalImage);
        
    $ow $dimensions[0];
        
    $oh $dimensions[1];
        
    $largerval max($ow$oh);
        
    $x round(($oh $sidemax) / $ow);
        if (
    $largerval == $ow)
        {
            
    $nw $x;
            
    $nh $sidemax;
        } else {
            
    $nw $sidemax;
            
    $nh $x;
        }
        
        
    $thumbie imagecreatetruecolor($nw$nh);
        if (!
    imagejpeg($thumbie$thumbnailName))
        {
            
    $error 'Error creating jpeg.';
            include 
    $_SERVER['DOCUMENT_ROOT'] . 'error.html.php';
            exit();
        }
        
    $thumbnail[0] = imagecopyresampled($thumbnailName$originalImage0000$nw$nh$ow$oh);
        
    $thumbnail[1] = $thumbnailName;
        
        return 
    $thumbnail;


    The $originalImage is the url to an image that was uploaded and moved to it's appropriate directory. The first two image functions I used work how they're suppose to (I checked the folder and found the black rectangle jpeg).

    The error message I'm getting is this:
    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in [.......]\filewriting.inc.php on line 63
    Line 63 is the one with the imagecopyresampled function on it.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Code:
    imagecopyresampled($thumbnailName, $originalImage, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
    What are the values of $thumbnailName and $originalImage when you get to the imagecopyresampled() statement?

    Can you var_dump them and confirm that they are correct?

    I see
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Yes, it's seems you're passing a path for $originalImage and $thumbnailName, while you shoud really load it (using one of the imagecreatefrom* functions).

    For example, if it's a jpg you're working with, you need to load using imagecreatefromjpeg.

  4. #4
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $originalImage is the url to the original image file that I'm trying to make a thumbnail of. $thumbnailName is kinda defined in the function... I just took the file name from $originalImage and added "thm-" to the beginning of the file name.

    And Immerse - thank you, I will try that.

  5. #5
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, so I tried the suggestions, and I now have this (note the switch statement):
    PHP Code:
    function makeThumbnail($originalImage$username)
    {
        
    $sidemax 250;
        
        
    $removestr $_SERVER['DOCUMENT_ROOT'] . 'users/' $username '/images/';
        
    $removelen strlen($removestr);
        
    $originalImageName substr_replace($originalImage''0$removelen);
        
    $thumbnailName 'thm-' $originalImageName;
        
    $thumbnailName $removestr $thumbnailName;
        
        
    $dimensions getimagesize($originalImage);
        
    $ow $dimensions[0];
        
    $oh $dimensions[1];
        
    $largerval max($ow$oh);
        
    $x round(($oh $sidemax) / $ow);
        if (
    $largerval == $ow)
        {
            
    $nw $x;
            
    $nh $sidemax;
        } else {
            
    $nw $sidemax;
            
    $nh $x;
        }
        
        
    $thumbie imagecreatetruecolor($nw$nh);
        if (!
    imagejpeg($thumbie$thumbnailName))
        {
            
    $error 'Error creating jpeg.';
            include 
    $_SERVER['DOCUMENT_ROOT'] . 'error.html.php';
            exit();
        }
        
        
    $mimetype exif_imagetype($originalImage);
        switch (
    $mimetype
        {
            case 
    1:
                
    $original imagecreatefromgif($originalImage);
                break;
            case 
    2:
                
    $original imagecreatefromjpeg($originalImage);
                break;
            case 
    3:
                
    $original imagecreatefrompng($originalImage);
                break;
            case 
    6:
                
    $original imagecreatefromwbmp($originalImage);
                break;
        }
        
        
    $thumbie imagecreatefromjpeg($thumbnailName);
        
    $thumbnail[0] = imagecopyresampled($thumbie$original0000$nw$nh$ow$oh);
        
    $thumbnail[1] = $thumbnailName;
        
        return 
    $thumbnail;


    But I get this error:
    Fatal error: Call to undefined function exif_imagetype()
    There was a suggestion on how to fix it, here: http://us3.php.net/manual/en/functio...-imagetype.php
    under the notes, but I tried that and it did not work x_x

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ShinoKage View Post
    There was a suggestion on how to fix it, here: http://us3.php.net/manual/en/functio...-imagetype.php
    under the notes, but I tried that and it did not work x_x
    Can you find on the phpinfo() page, exif?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No... no I cannot Aaaaaaaargggggggg.... I have to reinstall it all, don't I?

    It was in the php.ini file tho! Doesn't that count for something?

  8. #8
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Or you could use the mimetype returned by getimagetype instead:

    PHP Code:
    $dimensions getimagesize($originalImage);
    $ow $dimensions[0];
    $oh $dimensions[1];
    $mime $dimensions['mime']; 

  9. #9
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    Or you could use the mimetype returned by getimagetype instead
    Yeah... I was thinking about doing that, but I'm a little confused about what it returns, exactly.... It says
    Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.
    So does that mean it gives the int or does it give it in the imagetype_xxx form? x.X

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    getimagesize's second index returns an int which you can compare against the IMAGETYPE_XXX constants.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oooooh, okay. Thank you sooooo much!

    Once I get my computer working properly again, I will let y'all know how it goes, but I imagine that should do the trick.

  12. #12
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, well, it took me to the next page without displaying any errors, but it's still not working.... All it gives me for the thumbnail is a black box...
    PHP Code:
    function makeThumbnail($originalImage$username)
    {
        
    $sidemax 250;
        
        
    $removestr $_SERVER['DOCUMENT_ROOT'] . '/users/' $username '/images/';
        
    $removelen strlen($removestr);
        
    $originalImageName substr_replace($originalImage''0$removelen);
        
    $thumbnailName 'thm-' $originalImageName;
        
    $thumbnailName $removestr $thumbnailName;
        
        
    $dimensions getimagesize($originalImage);
        
    $ow $dimensions[0];
        
    $oh $dimensions[1];
        
    $largerval max($ow$oh);
        
    $x round(($oh $sidemax) / $ow);
        if (
    $largerval == $ow)
        {
            
    $nw $x;
            
    $nh $sidemax;
        } else {
            
    $nw $sidemax;
            
    $nh $x;
        }
        
        
    $thumbie imagecreatetruecolor($nw$nh);
        if (!
    imagejpeg($thumbie$thumbnailName))
        {
            
    $error 'Error creating jpeg.';
            include 
    $_SERVER['DOCUMENT_ROOT'] . '/error.html.php';
            exit();
        }
        
        
    $mimetype exif_imagetype($originalImage);
        switch (
    $mimetype) {
            case 
    1:
                
    $original imagecreatefromgif($originalImage);
                break;
            case 
    2:
                
    $original imagecreatefromjpeg($originalImage);
                break;
            case 
    3:
                
    $original imagecreatefrompng($originalImage);
                break;
            case 
    6:
                
    $original imagecreatefromwbmp($originalImage);
                break;
        }
        
        
    $thumbie imagecreatefromjpeg($thumbnailName);
        
    $thumbnail[0] = imagecopyresampled($thumbie$original0000$nw$nh$ow$oh);
        
    $thumbnail[1] = $thumbnailName;
        
        return 
    $thumbnail;


    I uploaded it onto my host's server so I could figure out how to fix it while still using that function >.< Anyways, like I said, not giving any errors, but it's still not working. I'm not sure that it's even updating the table in the database either because it says the ID number is 0....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •