Good time. I create and display a picture with a phone like this:

header("Content-Type: image/png"); $im = @imagecreate(150, 20)or die("Невозможно создать поток изображения"); $background_color = imagecolorallocate($im, 0, 0, 0); $text_color = imagecolorallocate($im, 233, 14, 91); imagestring($im, 4, 2, 2, "+7 111 111 11 11", $text_color); imagepng($im); imagedestroy($im); 

Further, I need to withdraw it right away, but only on base64. Tell me the logic of my further actions, otherwise I don’t think: you need to copy it to the host and then output it or, without copying, immediately output it so that it doesn’t take up space?

I display other pictures on the host like this:

 $file = "../../../../../demo/8.x/test.png"; if($fp = fopen($file,"rb", 0)) { $picture = fread($fp,filesize($file)); fclose($fp); $base64 = chunk_split(base64_encode($picture)); $tag = '<img src="data:image/gif;base64,'.$base64.'" />'; echo $tag; } 

example: avito

2 answers 2

The main thing: to generate a picture with a phone number on the fly is a very bad idea. This is a difficult operation and your piece of iron will be bent on some kind of completely ridiculous load.

Create a picture immediately when you create an ad and put it with some name that does not contain the original phone number. In essence, md5 from a phone with some global line for a project will already be enough. After all, you are hiding only from stupid parsers who will not disassemble what is written in the picture, but it’s written without any distortion, it’s a contrast. Accordingly, to insert a picture inline in base64 or usual src - no longer has much of a difference.

Or, as another option, generate a picture when you first access it and save it somewhere in the cache.

If you work with the disk closely (for example, you want to shove the cache of images in memcached ), and use tempnam not for sports (and, moreover, for some reason you want to use GD2 , not imagemagick ), then the question arises how to get the generated pictures in a row. GD does not provide standard tools for this, but you can get by buffering the output:

 $im = @imagecreate(150, 20)or die("Невозможно создать поток изображения"); $background_color = imagecolorallocate($im, 0, 0, 0); $text_color = imagecolorallocate($im, 233, 14, 91); imagestring($im, 4, 2, 2, "+7 111 111 11 11", $text_color); ob_start(); imagepng($im); $img = ob_get_clean(); imagedestroy($im); // в $img теперь лежит сгенерированный бинарник 
  • thank. Completely constructively wrote .. Uchtu - Sarkis Allahverdian

 <?php $image = 'img/phone.png'; $imageData = base64_encode(file_get_contents($image)); $src = 'data: '.mime_content_type($image).';base64,'.$imageData; echo "<img src=\"$src\" alt=\"\" />"; ?> 

This is very simple, the solution on SO.com is complicated.

 <?php $path = 'img/pic.jpg'; $type = pathinfo($path, PATHINFO_EXTENSION); $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "<img src=\"$base64\" alt=\"\" />"; ?> 

This is the second option - just checked: worker, this is what the scripts output: enter image description here