Show image saved in the BD with mpdf


(Rafael Rosales) #1

Greetings community, once again needing the forum to solve something that I could not solve, I need to show an image saved in the BD in a report, I am doing it with mpdf, the problem is that the image is not shown in the report , I am going to show an image of the report so that they observe that it is not seen, in the image that I am going to see two images image 1 and image 2, the first one is shown because it is an image taken from a location or directory with this code:

 <img src="images/banner/logo.png"> 

the second is not shown (photo of the person), it is also imported for a folder but it is uploaded through the BD, that is if in the table which I use has 1000 articles, then there are 1000 images in that destination folder and the What you want to show is the one that contains the id of the article, I put the code with which I try to obtain the image:

<?php
             $dbImage = Yii::app()->createUrl('/tblDatosc/displaySavedImage', array('id' => $model->Id));
                echo CHtml::image($dbImage, 'foto', array('width' => 100, 'height' => 100));
        ?> 

in my controller:

public function actionDisplaySavedImage($id) {

        $model = $this->loadModel($id);
        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Transfer-Encoding: binary');
        header('Content-length: ' . $model->imagesize);
        header('Content-Type: ' . $model->imagetype);
        header('Content-Disposition: attachment; filename=' . $model->imagename);
        echo $model->foto;
    }

in the model:

 public function beforeSave() {

        if ($file = CUploadedFile::getInstance($this, 'foto')) {
            $this->imagename = $file->name;
            $this->imagetype = $file->type;
            $this->imagesize = $file->size;
            $this->foto = file_get_contents($file->tempName);
        }
        return parent::beforeSave();
    }

I await your answers, if it could not be resolved with mpdf, I need another extension, to show me the photo stored in the BD


#2

You can use base64 to embed the image.
https://mpdf.github.io/what-else-can-i-do/images.html#embedded-image-data

You show upload saving code - that has nothing todo with the actual pdf.
First make sure your pdf can be displayed as html. If that does not work, it will not work either for mpdf.

Your code actually does just output the image to the browser (with wrong mimetype), if that is what u want, thats a different question -> https://stackoverflow.com/questions/2633908/php-display-image-with-header.


(Rafael Rosales) #3

mathis. Thank you for responding, what I want is to choose an image saved in the database (the path where it is located is saved, in the table where the data of the people to whom I am going to show the photo are, there are many people each with your photo, but I want for each person to whom the report corresponds, select the corresponding photo, according to the id corresponding to that article in the BD.