Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PhpOffice\PhpWord\Exception\InvalidImageException #1223

Open
iamsayantan opened this issue Dec 18, 2017 · 4 comments
Open

PhpOffice\PhpWord\Exception\InvalidImageException #1223

iamsayantan opened this issue Dec 18, 2017 · 4 comments

Comments

@iamsayantan
Copy link

iamsayantan commented Dec 18, 2017

I am getting the above exception when I try to add image to word document.

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$style = [
    'height' => 104,
    'width' => 190,
    'marginLeft' => -2500
];

$section = $phpWord->addSection();
$src = base_url() . "/public/images/logo.jpg";

$headerTable = $section->addTable();
$headerTable->addRow();
$headerTable->addCell(5000)->addImage($src, $style);

This is the related code. And following is the stack trace,

#0 /home/sencillo/public_html/app/vendor/phpoffice/phpword/src/PhpWord/Element/Image.php(139): PhpOffice\PhpWord\Element\Image->checkImage('http:https://sencillo...')
#1 [internal function]: PhpOffice\PhpWord\Element\Image->__construct('http:https://sencillo...', Array)
#2 /home/sencillo/public_html/app/vendor/phpoffice/phpword/src/PhpWord/Element/AbstractContainer.php(145): ReflectionClass->newInstanceArgs(Array)
#3 [internal function]: PhpOffice\PhpWord\Element\AbstractContainer->addElement('Image', 'http:https://sencillo...', Array)
#4 /home/sencillo/public_html/app/vendor/phpoffice/phpword/src/PhpWord/Element/AbstractContainer.php(112): call_user_func_array(Array, Array)
#5 /home/sencillo/public_html/app/application/views/quotations-docx.php(41): PhpOffice\PhpWord\Element\AbstractContainer->__call('addImage', Array)
#6 /home/sencillo/public_html/app/application/views/quotations-docx.php(41): PhpOffice\PhpWord\Element\Cell->addImage('http:https://sencillo...', Array)
#7 /home/sencillo/public_html/app/system/core/Loader.php(962): include('/home/sencillo/...')
#8 /home/sencillo/public_html/app/system/core/Loader.php(489): CI_Loader->_ci_load(Array)
#9 /home/sencillo/public_html/app/application/controllers/Reports.php(153): CI_Loader->view('quotations-docx', Array, true)
#10 [internal function]: Reports->quotation('122')
#11 /home/sencillo/public_html/app/system/core/CodeIgniter.php(537): call_user_func_array(Array, Array)
#12 /home/sencillo/public_html/app/index.php(330): require_once('/home/sencillo/...')
#13 {main}

I was checking the code for checkImage

// Check image data
        if ($this->sourceType == self::SOURCE_ARCHIVE) {
            $imageData = $this->getArchiveImageSize($source);
        } else {
            $imageData = @getimagesize($source);
        }

Here getimagesize($source) is returning false. But $source is valid and I have checked with the following snippet which is working fine,

<?php
$file = "http:https://example.com/public/images/logo.jpg"; // this is same as $source
$info = getimagesize($file);

echo "<pre>" . print_r($info, true) . "</pre>";

?>

Output:

Array
(
    [0] => 190
    [1] => 104
    [2] => 2
    [3] => width="190" height="104"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)

5.6.32

Context

  • PHP version 5.6.32
  • PHPWord version: 0.13.0
@FBnil
Copy link

FBnil commented Dec 19, 2017

Running your test php script is from your user, but your website is running as apache or nobody, and thus it could be you do not have permissions to access that file under that apache user.
Try removing the @ in @getimagesize (if it actually fails there, it will throw an error that could be useful) or check if the file is readable first: is_readable($filename ).

If you have SELinux, you also need to check for file context (ls -lZ) and maybe explicitely set the context chcon -Rv --type=httpd_sys_content_t $imagefile

but maybe the basic permissions are not set:

chgrp -R www public/images && chmod -R g+w public/images (and check that apache is in the www group; and maybe you need to create that www group first)

@iamsayantan
Copy link
Author

Thanks for the reply. In my case the $src is actually the url of the image and not the file path. So there should not be any issues with permission. And even so the project is hosted on Cpanel and there isn't much I can do about file permissions.

@troosan
Copy link
Contributor

troosan commented Dec 19, 2017

@iamsayantan do you have the same with the latest 0.14 version?

@ahmed-bhs
Copy link

@iamsayantan try to display all php errors and warnings ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
Most of the time, it's related to allow_url_fopen = off php.ini configuration, enabling it is not a good idea for security reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants