1

I need to create images dynamically, i.e the text on the image will be dynamic. Is there any way to generate the images with dynamic text and save it? I have tried the following

<?php

header("Content-type: image/jpeg");
$imgPath = 'image.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = "THIS TEXT IS DYNAMIC";
$fontSize = 3;
$x = 115;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
?> 

but it shows me the image with the text, but it does not save the image with this text written on it. I need to save the image with the text written on it

1 Answer 1

1

Using the function signature

bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )

you need to set a path to a file using the second parameter.

Do it like this:

$saveToPath = dirname( __FILE__ ) . '/imageCache/myImageFile.jpg';
imagejpeg( $image, $saveToPath );

header( 'Content-Type: image/jpeg' );
readfile( $saveToPath );
Sign up to request clarification or add additional context in comments.

2 Comments

it shows me cannot display because it contains errors
still the same, can you please change in my code. i don't know in which line i have to add this code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.