mcmuney Posted January 26, 2018 Share Posted January 26, 2018 (edited) 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 January 26, 2018 by mcmuney Quote Link to comment https://forums.phpfreaks.com/topic/306332-help-with-gd-library/ Share on other sites More sharing options...
requinix Posted January 26, 2018 Share Posted January 26, 2018 -All images that are uploaded are renamed with a .jpg extension.Don't do that. Quote Link to comment https://forums.phpfreaks.com/topic/306332-help-with-gd-library/#findComment-1555768 Share on other sites More sharing options...
gizmola Posted January 26, 2018 Share Posted January 26, 2018 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)); Quote Link to comment https://forums.phpfreaks.com/topic/306332-help-with-gd-library/#findComment-1555779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.