Mount an image from other images with PHP

Asked

Viewed 539 times

0

Hello;

I wonder if I can mount an image from other images sequentially.

For example: Let’s say I have a register of words and images defined for each letter of the alphabet. When registering a word, Bee for example, I want to create an image from 6 images(A B E L H A). Obs: I don’t want to give an imagecreate from a string, I want to create from already defined images.

Solution

$string = "ABELHA";
$len = strlen($string);
$im = imagecreatetruecolor( 60 * $len, 60 );
for( $i = 0; $i < $len; ++$i ) {
    $letra = substr( $string, $i, 1 );
    $lim= imagecreatefrompng("./$letra.png");
    imagecopy ( $im, $lim, $i * 60, 0, 0, 0, 60, 60 );
    imagedestroy( $lim );
}
header('Content-Type: image/jpeg');
imagejpeg( $im );
imagedestroy( $im );
  • Hi Bacco, in this specific situation I have not tried anything yet, I have generated image from a text for example (captcha), but not in this way as I described. I searched the image commands in PHP and what I found were commands that merge image basically, so I wanted to know if it is possible.

  • I practically made the essential for you, and posted as an answer. Of course it will need adjustments, but the essential is there.

1 answer

3


Your question is a little broad, but it follows a basic code:

Suppose the letters are 60 60px in size and saved as A.png, B.png etc..

$string = 'ABELHA';
$len = count( $string );
$im = imagecreatetruecolor( 60 * $len, 60 );
for( $i = 0; $i < $len; ++$i ) {
   $letra = ( $string, $i, 1 );
   $lim= imagecreatefrompng("/caminho/$letra.png");
   imagecopy ( $im, $lim, $i * 60, 0, 0, 0, 60, 60 );
   imagedestroy( $lim );
}
imagejpeg( $im );
imagedestroy( $im );

I focused on the question problem, I did not consider encoding and accentuation problems, nor special characters not to extend too much.

To use characters other than numbers and from A to Z, you have to treat the string as you see fit.

  • Hmm, ball show, I’ll take a test with your example.

  • @Reji will probably bump into some syntax nonsense, or some error in the coordinates, but I think if you read in the PHP manual the functions, tidy easy. But anything, comment. I made the code right here in the answer without testing, may have passed something.

  • To tell you the truth, I followed your question, but I think it would be much more efficient to have an image with all the letters, and cut it out for the output image, instead of them saved separately.

  • Hit the nail on the head, exactly what I wanted to do. Thanks anyway, it’s because I had to define that you can do this but I don’t have much time to see if it was possible, I thought I would have to use some awesome command. In fact an image would only be easier, but I will not have output letters, will be signals, so the need ;)

Browser other questions tagged

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