Convert php string to image - php

I want to convert a php string to an image.
I use this code
header("Content-type: image/png");
$string = '.the_title().';
$font = 5;
$width = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);
$image = imagecreatetruecolor ($width,$height);
$white = imagecolorallocate ($image,255,255,255);
$black = imagecolorallocate ($image,0,0,0);
imagefill($image,0,0,$white);
imagestring ($image,$font,0,0,$string,$black);
imagepng ($image);
imagedestroy($image)
but its shows the_title as text instead of executing the string

Use imagecreatefromstring
$string = '.the_title().';
$data = base64_decode($string);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}

Use imagestring like:
<?php
$string = the_title();
$im = imagecreate(150, 20); // image size 150x20px
imagecolorallocate($im, 255, 255, 255); // background white
$text_color = imagecolorallocate($im, 0, 0, 0); // text color black
imagestring($im, 3, 5, 5, $string, $text_color); // append string to image
header('Content-type: image/png'); // filetype
imagepng($im, 'image.png'); // save as image.png
imagedestroy($im); // free up memory

Related

imagecreatefrompng() generating black background instead of transparent part in image

I make an image using PHP but my code turn png transparency into a solid black color, Is there a solution to improve my code?
$filename = 'https://i.ibb.co/9sB9rTM/small-408587143-15774386991of1-2-2-Original-0-test.png';
$widthsrc=0;
$heightsrc=0;
$src1 = imagecreatefrompng($filename);
list($widthsrc, $heightsrc, $typesrc, $attrsrc) = getimagesize($filename);
$background = imagecolorallocatealpha($src1, 255, 255, 255, 127);
$pre = imagecolortransparent($src1, $background);
imagealphablending($src1, false);
imagesavealpha($src1, true);
imagepng($src1);
First use always header like:
header("Content-type: image/png");
Second are you sure when you save image 'black solid' is not trasparent?
Example of creating image:
header("Content-type: image/png");
$string = 'TEXT';
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>

How to convert text to image in PHP with line break?

Currently I am using the PHP code below to convert string text to image with transparent background. Now I need to convert the string text to image with line break, transparent background and also with custom font. Code I am using:
$width = 200;
$height = 40;
$text = "Test text -- line break here -- with line break";
$fontsize = 3;
$img = imagecreate($width, $height);
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
$color = imagecolorallocate($img, 0, 0, 0);
imagestring($img, $fontsize, 0, 0, $text, $color);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
Doing it like this will help you
header("Content-type: image/png");
$str1= 'Test text ';
$str2= 'with line break';
$image= imagecreate(200,40);
$background = imagecolorallocate($image,255,255,255);
$color= imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$background);
imagestring($image,10,5,5,$str1,$color);
imagestring($image,10,5,20,$str2,$color);
imagepng($image);
output look like this

How to change Image to jpg and set dpi to 300 using php?

I have following code in a controller under pngtojpgAction() which I am calling using ajax.
Through this
$this->getRequest()->getParam('imagedata'));
statement I am getting a pattern like this data:image/jpeg;base64,/9j/4AAQSkZJR......AH9T796KtUV1HGf/Z
Which is a png image data.
now I am using following code to convert this png image to jpeg and to increase dpi to 300.
public function pngtojpgAction()
{
//Code to convert png to jpg image
$input = imagecreatefrompng($this->getRequest()->getParam('imagedata'));
$width=imagesx($input);
$height=imagesy($input);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
ob_start();
imagejpeg($output);
$contents = ob_get_contents();
ob_end_clean();
//echo 'data:image/jpeg;base64,'.base64_encode($contents); /*Up to here code works well*/
$jpgImage='data:image/jpeg;base64,'.base64_encode($contents);
$image = file_get_contents($jpgImage);
$image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);
header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="'.basename($jpgImage).'"');
echo $image;
}
using this
$image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);
I want to increase dpi of image to 300 dpi.
I am unable to change the image dpi using these line of code
$jpgImage='data:image/jpeg;base64,'.base64_encode($contents);
$image = file_get_contents($jpgImage);
$image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);
header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="'.basename($jpgImage).'"');
echo $image;
I used this link as reference Change image dpi using php
After making some changes it worked for me.
public function pngtojpgAction()
{
//Code to convert png to jpg image
$input = imagecreatefrompng($this->getRequest()->getParam('imagedata'));
$width=imagesx($input);
$height=imagesy($input);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
ob_start();
imagejpeg($output);
$contents = ob_get_contents();
//Converting Image DPI to 300DPI
$contents = substr_replace($contents, pack("cnn", 1, 300, 300), 13, 5);
ob_end_clean();
echo 'data:image/jpeg;base64,'.base64_encode($contents);
}
I would use imagemagic instead:
convert Bird.jpg -density 300 Bird2.jpg
But you could allso do that with GD.
Link to class
$filename = 'Bird.jpg';
$source = imagecreatefromjpeg($filename);
list($width, $height) = getimagesize($filename);
$b = new Resampler;
$im = $b->resample($source, $height, $width, 300);
file_put_contents('Bird2.jpg', $im);
Tested on Windows enviroment.
Simple Code to Re-generate image with adjustable quality.
function compress_image($source_url, $destination_url, $quality) {
$info = getimagesize($source_url);
$image = imagecreatefromjpeg($source_url);
imagejpeg($image, $destination_url, $quality);
return $destination_url;
}
echo compress_image($_FILES["file"]["tmp_name"], "destination .jpg", 80);//Adjust Quality
Note: Ensure that the GD library for PHP is installed.

Gd Library Image Style imagettftext

How to make Like This Image?
I just want to learn how do i get output like this using GD library,
Thanks for your help guys..
Here is your code, you could easily find it on google tho.
<?php
header ("Content-type: image/png");
$string = "your text"; // Change this text
$font = 4; // try changing this as well
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("/path/to/yourimagefile");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
?>
Here's an example:
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
imagepng($image); // outputting the image
For more see imagettftext().
EDIT: An example of using multiple imagettftext():
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
$text = "Text 2";
imagettftext($image, 15, 0, 25, 45, $color, $font, $code); // adding the text
$text = "Text 3";
imagettftext($image, 15, 0, 30, 50, $color, $font, $code); // adding the text
imagepng($image); // outputting the image

Create transparent png with text from scratch in php

All the examples I've found on the web seem to create pngs with text from an existing png. Is it possible to create a transparent png from scratch and then add text?
The code ive got so far follows (but it doesnt work. just outputs a blank image source)
<?php
$width = 150;
$height = 30;
$text = "My Text";
$fontsize = 5;
$im = imagecreate($width, $height);
$transcolor = imagecolortransparent($im);
imagestring($im, $fontsize, 0, 0, $text, $transcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
<?php
$font = 25;
$string = 'My Text';
$im = #imagecreatetruecolor(strlen($string) * $font / 1.5, $font);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "droid_mono.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Use imagestring instead of imagettftext if you don't want custom font.
Here's the solution based on your original code.
<?php
$width = 640;
$height = 480;
$text = "My Text";
$fontsize = 5;
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 0, 0);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
I Think GD is one of the most popular for generating images, with imagettftext.
<?php
$text = 'SOME TEXT';
$font="c:/windows/Fonts/Latinwd.ttf"; //Load font file for windows
$im = ImageCreate(700, 140);
$bla = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $bla); //transparent background
$black = imagecolorallocate($im, 255,255,255);
ImageTTFText ($im, 38, 0, 10, 40, $black, $font, $text);
header('Content-Type: image/png');
ImagePNG($im, 'name.png');
imagedestroy($im);
?>

Categories