1

Sirs,

I am getting an error from a function I created. The purpose of the function is to save an image. I have copied my source and error below.

I'm not sure what it could be, did I do something wrong?

private function validadeFormatImage() {
    switch ($this->dadosImg['type']) {
        case 'image/jpeg':
        case 'image/pjpeg':
            $this->image = imagecreatefromjpeg($this->dadosImg['tmp_name']);
            $this->resizeImg();
            $this->validateDirectory();
            imagejpeg($this->imgResized, $this->directory . $this->nameImg);
            break;
        case 'image/png':
        case 'image/x-png':
            $this->image = imagecreatefrompng($this->dadosImg['tmp_name']);
            $this->resizeImg();
            $this->validateDirectory();
            imagepng($this->imgResized, $this->directory . $this->nameImg);
            break;
    }
}
Source line 49: $this->image = imagecreatefromjpeg($this->dadosImg['tmp_name']);

Fatal error: Uncaught Error: Call to undefined function Module\administrative\Models\helper\imagecreatefromjpeg() in C:\xampp\htdocs\DevHomologacao\adm\module\administrative\Models\helper\AdmsUploadImgRed.php:49 Stack trace: #0 C:\xampp\htdocs\DevHomologacao\adm\module\administrative\Models\helper\AdmsUploadImgRed.php(33): Module\administrative\Models\helper\AdmsUploadImgRed->validadeFormatImage() #1 C:\xampp\htdocs\DevHomologacao\adm\module\site\Models\StsEditSobCompany.php(70): Module\administrative\Models\helper\AdmsUploadImgRed->uploadImd(Array, '../site/assets/...', 'perfil2.jpg', 500, 400) #2 C:\xampp\htdocs\DevHomologacao\adm\module\site\Models\StsEditSobCompany.php(51): Module\site\Models\StsEditSobCompany->uploadNewImage() #3 C:\xampp\htdocs\DevHomologacao\adm\module\site\Controllers\EditSobCompany.php(39): Module\site\Models\StsEditSobCompany->alterSobCompany(Array) #4 C:\xampp\htdocs\DevHomologacao\adm\module\site\Controllers\EditSobCompany.php(25): Module\site\Controllers\EditSobCompany->editSobCompany() #5 C:\xampp\htdocs\DevHomologacao\adm\config\ConfigController.php(101): Module\site\Controllers\EditSobCompany->editInfoSobCompany('1') #6 C:\xampp\htdocs\DevHomologacao\adm\config\ConfigController.php(83): Config\ConfigController->checkMethod() #7 C:\xampp\htdocs\DevHomologacao\adm\index.php(10): Config\ConfigController->classLoadPage() #8 {main} thrown in C:\xampp\htdocs\DevHomologacao\adm\module\administrative\Models\helper\AdmsUploadImgRed.php on line 49

treckstar
  • 1,956
  • 5
  • 21
  • 26
Pedro
  • 11
  • 2
  • Based on the error message, do you have the function that is coming up as undefined? Can you post the code for `imagecreatefromjpeg()`. I think that would be best place to start. – treckstar Jun 11 '22 at 05:25
  • You might want to look at other questions like https://stackoverflow.com/questions/13338339/imagecreatefromjpeg-and-similar-functions-are-not-working-in-php – Progman Jun 12 '22 at 14:14

1 Answers1

1
private function validadeFormatImage()
{
    switch ($this->dadosImg['type']) {
        case 'image/jpeg':
        case 'image/pjpeg':
            $this->image = $this->imagecreatefromjpeg($this->dadosImg['tmp_name']);
            $this->resizeImg();
            $this->validateDirectory();
            imagejpeg($this->imgResized, $this->directory . $this->nameImg);
            break;
        case 'image/png':
        case 'image/x-png':
            $this->image = $this->imagecreatefrompng($this->dadosImg['tmp_name']);
            $this->resizeImg();
            $this->validateDirectory();
            imagepng($this->imgResized, $this->directory . $this->nameImg);
            break;
    }
}
  • if you want to call same class function in side of function use $this->
Uttam Nath
  • 644
  • 4
  • 16
  • When trying to use $this-> I get 'Fatal error: Uncaught Error: Call to undefined method Module\administrative\Models\helper\AdmsUploadImgRed::imagecreatefromjpeg() in' – Pedro Jun 12 '22 at 14:12
  • @Pedro in same class this function exiting or not? – Uttam Nath Jun 12 '22 at 14:30
  • The problem was because the GD was disabled, enabling it in php.ini, it worked perfectly. https://stackoverflow.com/questions/13338339/imagecreatefromjpeg-and-similar-functions-are-not-working-in-php Thank you very much. – Pedro Jun 12 '22 at 21:23