Accents do not appear c/ PHP

Asked

Viewed 818 times

1

I’m making a certificate system for an event and when words with accents are inserted, they leave. I am using Laravel and a library called Intervention Image to generate the images of the certificates and add texts to the spaces left in the image. After the image is generated, a PDF is created.

Follow the image of the certificate:

Certificado

As shown in the figure, the accents are not being inserted.
Below is the code in which the texts are placed.

                    $img = Image::make(public_path().'/imagens/certificado.jpg');
                    //NOME
                    $img->text($usuario->name, 800, 320, function($font) {
                        $font->file(public_path().'/font/Asimov.otf');
                        $font->size(60);
                        $font->align('center');
                        $font->color('#000000');
                    });

                    //CPF
                    $img->text($usuario->cpf, 310, 383, function($font) {
                        $font->file(public_path().'/font/Asimov.otf');
                        $font->size(18);
                        $font->align('center');
                        $font->color('#000000');
                    });

                    //TIPO ATIVIDADE

                    $img->text('Organizacão', 640, 383, function($font) {
                        $font->file(public_path().'/font/Exo-Regular.otf');
                        $font->size(18);
                        $font->align('center');
                        $font->color('#000000');
                    });

                    //NOME ATIVIDADE
                    $img->text('Comissão organizadora', 1060, 383, function($font) {
                        $font->file(public_path().'/font/arial.otf');
                        $font->size(18);
                        $font->align('center');
                        $font->color('#000000');
                    });

                    //TIPO
                    $img->text('Organizador(a)', 670, 413, function($font) {
                        $font->file(public_path().'/font/Asimov.otf');
                        $font->size(19);
                        $font->align('center');
                        $font->color('#000000');
                    });

                    //HORAS
                    $img->text('40', 945, 413, function($font) {
                        $font->file(public_path().'/font/Asimov.otf');
                        $font->size(19);
                        $font->align('center');
                        $font->color('#000000');
                    });

                    //MINUTOS
                    $img->text('00', 1010, 413, function($font) {
                        $font->file(public_path().'/font/Asimov.otf');
                        $font->size(19);
                        $font->align('center');
                        $font->color('#000000');
                    });


                    //AUTENTICIDADE
                    $img->text('A autenticidade desse certificado pode ser verificada pela URL:', 362, 1030, function($font) {
                        $font->file(public_path().'/font/Asimov.otf');
                        $font->size(19);
                        $font->align('center');
                        $font->color('#ffffff');
                    });


                    $file = 'www.feiradascidades.canoas.ifrs.edu.br/validar/certificadocomissao'.$usuario->id;
                    //URL
                    $img->text($file, 392, 1050, function($font) {
                        $font->file(public_path().'/font/Asimov.otf');
                        $font->size(19);
                        $font->align('center');
                        $font->color('#ffffff');
                    });

                    $caminho[$key] = public_path().'/imagens/certificadocomissao'.$usuario->id.'.jpg';

                    $img->save(public_path().'/imagens/certificadocomissao'.$usuario->id.'.jpg');`

This generator was made via localhost on Windows, but is hosted on a Linux server. During development on Windows, the accents worked perfectly and after hosting not.



The header of all HTML files have the charset set set to UTF-8 and in the class that mount the image (code above), is being used the ini_set('default_charset', 'UTF-8');

When executing the following

ini_set('default_charset', 'UTF-8');
dd(ini_get('default_charset'));

as a result of

UTF-8

In fact the charset is set to UTF-8, but the words continue without accent. I tried numerous things to solve, but so far unsuccessful.

EDIT:
Looking further, the data arrive in UTF-8, verified by mb_check_encoding. Apparently, the problem is at the time of saving the image, because it is already saved with the problem of accents. I believe the problem is in this part:

$img->save(public_path().'/imagens/certificadocomissao'.$usuario->id.'.jpg');
  • Save your PHP file with UTF-8 encoding. The file must be saved with another encoding, but is being served as UTF-8. The encodings need to match.

  • Hello, Are these characters from the image coming from a correct database? Are the records in this database with the proper rises? The database database database database matches the application’s database database database (utf8_general_ci)?

  • Sorry @Fabio, I converted your reply to comment and maybe I was too quick. But I understand that even after the issue, remains a request for clarification, right?

  • @bfavaretto the PHP file is saved with UTF-8 encoding.

  • @Fabiowilliamconceição the records in the bank are accented correctly. Is in utf8_general_ci.

  • Have you tried using something like $img->text(utf8_encode("Organização"), ...)?

  • @Andersoncarloswoss yes, with utf8_enconde is like this

  • According to an Issue of the plugin (https://github.com/Intervention/image/issues/182) this may be a problem with the font. Also, try using mb_check_encoding to check which encoding of the strings and mb_convert_encoding (http://php.net/manual/ref.mb.string.php) to convert the encoding and ensure they are passed as UTF-8 to the plugin, use multibyte functions (mb_) instead of utf8_encode/Decode.

  • @Rômulogabrielrodrigues problem with the source I believe it is not, because it has support for Portuguese and the source Arial was also tested. I tried to use mb_convert_encoding, but the problem persisted. What I find strange is that in Windows, in localhost, everything went well. However, when hosting on Linux, it happened that the accents bugged

  • browser-independent?

  • @andrepaulo yes. The problem is occurring when saving the generated image. At this time. I debugged all the code and came all written right and in UTF-8.

  • @Rafaelborba mb_check_encoding returned UTF-8? If yes, open an Issue in the plugin’s frame, it may be that the plugin has some problem in this version.

  • @Romulogabrielrodrigues yes

  • @Rafaelborba tried to use htmlentities() (I think that’s the syntax) of php?

Show 9 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.