Jump to content

Help with GD Library


mcmuney

Recommended Posts

I'm using a gd library script to resize images that are uploaded. 

 

-All images that are uploaded are renamed with a .jpg extension. 

-When the original images is .jpg or .jpeg, I can see the image and resize it without any issues.

-But when the original image is a .gif or a .png, I cannot view or resize the image

-I'll get an error, "JPEG library reports unrecoverable error.. $filename is not a valid JPEG file"

-This is the line that tells it to look for jpg/jpeg file:

$func = 'imagecreatefrom' . ($ext == 'jpg' ? 'jpeg' : $ext);

-If the original is a .png, if I replace the 'jpeg' above with 'png', then it works, but obviously, it no longer works with .jpg files

 

My question, how can I modify the line above to specify for jpg, jpeg, png and gif? 

 

I tried this, but it wouldn't work: $func = 'imagecreatefrom' . ($ext == 'jpg' ? 'jpeg' ? 'png' ? 'gif' : $ext);

 

Here's the full portion of the code:

    function loadImage($filename)
    {
        $ext  = strtolower($this->_getExtension($filename));
        $func = 'imagecreatefrom' . ($ext == 'jpg' ? 'jpeg' : $ext);
        if (!$this->_isSupported($filename, $ext, $func, false)) {
            return false;
        }

        $this->_imgOrig = $func($filename);
        if ($this->_imgOrig == null) {
            $this->_debug("The image could not be created from the '$filename' file using the '$func' function.");
            return false;
        }
Link to comment
Share on other sites

You need to start with explaining why you tried to force everything to be a .jpg in the first place? Rather than trying to create a function name from a string, just use a simple switch statement:

 

$ext  = strtolower($this->_getExtension($filename));
switch ($ext) {
   case 'jpeg':
   case 'jpg' :
       $this->_imgOrig = imagecreatefromjpeg($filename);
       break;
   case 'png' :
       $this->_imgOrig = imagecreatefrompng($filename);
       break;
   case 'gif' :
       $this->_imgOrig = imagecreatefromgif($filename);
       break; 
   default: 
       // If it gets here, there was no extension, so it's an error.
    
}
An alternative is to use imagecreatefromstring.

 

$this->_imgOrig = imagecreatefromstring(file_get_contents($filename));
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.