The correct way to make the dump (if the property was public):
imagepng( $img->img, 'test/imagen.png' );
The problem is that, as you can see in the source code of the class, this property is private and, therefore, it is not accessible from your code:
<?php
class textPainter{
private $img;
/* ... */
A possible solution (without altering the original class), could be to use ob_start()
to save the output in the buffer and then ob_get_contents()
to save the image stored in the buffer (without emptying it) in a file:
<?php
ob_start();
require_once 'class.textPainter.php';
$x = "470";
$y = "80";
$R = "0";
$G = "0";
$B = "0";
$size = "50";
$texto = $_GET["texto"];
//crea la imagen con el nombre recibido por metodo GET
$img = new textPainter(
'./game.jpg',
$texto,
'C:/xampp/htdocs/game/arial.ttf',
$size
);
if(!empty($x) && !empty($y)) {
$img->setPosition($x, $y);
}
if(!empty($R) && !empty($G) && !empty($B)) {
$img->setTextColor($R,$G,$B);
}
/* Envío la imagen al búfer de salida */
$img->show();
/* Ahora guardamos la imagen en un archivo (en JPEG porque el formato
se obtiene del archivo original) */
file_put_contents('test/imagen.jpg', ob_get_contents());
Another option would be to reimplement the show()
method to allow the image to be sent to a file or a getter method to obtain the original image.
PS: I do not recommend using this class for production, after taking a look at the code I have seen that it does not support inheritance (add functionality through extends
) and every time that show()
is called it is repainted the text on the image. As the class is developed, it should be done only once in the constructor and that show()
allow output without modifying the content.
PD2: The class code has an error in the sent mime type that causes some browsers not to display the image correctly.