Jump to content

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;
        }
Edited by mcmuney
Link to comment
https://forums.phpfreaks.com/topic/306332-help-with-gd-library/
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));
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.