#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Resized images are black


    I've seen this question pop up over and over again but all of the solutions I've tried doesn't fix the problem of writing a black image instead of the new resized image.

    I'm looking to resize any file larger than 800px wide down to 800px in width and the same percentage in height as well.

    below is the test code I'm using, Thanks.

    Code:
    $url = "http://webserver.com/test.jpg";
    $imgfilenew = "newfile.jpg";
    if (false !== ($rfile = file_get_contents($url))) { $rfile = file_get_contents($url); }	
    list($width, $height) = getimagesize($url);
    
    //figure percent reduction
    $reduce =  800/$width;
    
    //figure new dims
    $newwidth = $width * $reduce;
    $newheight = $height * $reduce;
    
    $rimg = imagecreatetruecolor($newwidth, $newheight);
    $oimg = imagecreatefromjpeg($rfile);
     
    // Resize
    imagecopyresampled($rimg, $oimg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    
    // Output
    imagejpeg($rimg, $imgfilenew, 100); //save image as jpg
    
    imagedestroy($rimg); 
    imagedestroy($oimg);
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    $rfile is the contents of the file? It should be just the filename. If you want to load the image from the contents (which is inefficient) you'd use imagecreatefromstring().
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Also this...

    PHP Code:
    if (false !== ($rfile file_get_contents($url))) { $rfile file_get_contents($url); } 
    ... is a whee bit redundant. If $rfile is successfully assigned the contents of $url, you're assigning it the contents again.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    Sorry guys, I figured out my error soon after I posted this question

    remove this line
    Code:
    if (false !== ($rfile = file_get_contents($url))) { $rfile = file_get_contents($url); }
    and change this line from
    Code:
    $oimg = imagecreatefromjpeg($rfile);
    to this
    Code:
    $oimg = imagecreatefromjpeg($url);
    and it works as I wanted it to.

IMN logo majestic logo threadwatch logo seochat tools logo