I am trying to create an image with a circle and some text on it where the text curves around the inside of the circle. The circle is adaptive and I want the text inside it to be adaptive.
Until now I have the following code that generates a text that curves:
$row1="line1";
$degrees = (130/strlen($row1));
imageellipse ( $im , 250 , 250 , 390 , 390 , $black );
imageellipse ( $im , 250 , 250 , 398 , 398 , $black );
for ($i=0;$i<strlen($row1);$i++)
{
$a = ($degrees*$i)+126-strlen($row1)/3;
$cos = cos(deg2rad($a));
$sin = sin(deg2rad($a));
$x = 0;
$y = 180;
$xt = round($cos*($x) - $sin*($y));
$yt = round($sin*($x) + $cos*($y));
imagettftext($im,14,180-($a),250+$xt,270+$yt,$red,'fonts\times.TTF',$row1[$i]);
}
Can you please tell me how can I make it adaptive, so it can adapt with the generated ellipse?
Hope this helps:
$image = imagecreatetruecolor(400,400);
$white = imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$white);
$red = imagecolorallocate($image,255,0,0);
$degrees = (360/strlen($text));
for($i=0;$i<strlen($text);$i++)
{
$a = ($degrees*$i)+180;
$cos = cos(deg2rad($a));
$sin = sin(deg2rad($a));
$x = 0;
$y = 180;
$xt = round($cos*($x) - $sin*($y));
$yt = round($sin*($x) + $cos*($y));
imagettftext($image,20,180-($a),200+$xt,200+$yt,$red,"C:/WINNT/Fonts
arial.ttf",$text[$i]);
}
Related
I am using one of third party class file to generate barcode with php dynamically as per user inputs. I have problem with it's variable size of width.
When user enters 3 char then it creates small width of barcode and when user enters 10-14 chars, then it generates big barcode in width. I want barcode with same height - width in all cases.
See my app details:
PHP 5.6
CI3 framework
barcode type : code128
Please see my code below:
public function barcode( $filepath="", $text="USPC", $size="43", $orientation="horizontal", $code_type="code128", $print=false, $SizeFactor=1 ) {
$code_string = "";
$chksum = 104;
// Must not change order of array elements as the checksum depends on the array's key to validate final code
$code_array = array(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","#"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","\`"=>"111422","a"=>"121124","b"=>"121421","c"=>"141122","d"=>"141221","e"=>"112214","f"=>"112412","g"=>"122114","h"=>"122411","i"=>"142112","j"=>"142211","k"=>"241211","l"=>"221114","m"=>"413111","n"=>"241112","o"=>"134111","p"=>"111242","q"=>"121142","r"=>"121241","s"=>"114212","t"=>"124112","u"=>"124211","v"=>"411212","w"=>"421112","x"=>"421211","y"=>"212141","z"=>"214121","{"=>"412121","|"=>"111143","}"=>"111341","~"=>"131141","DEL"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","FNC 4"=>"114131","CODE A"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
$code_keys = array_keys($code_array);
$code_values = array_flip($code_keys);
for ( $X = 1; $X <= strlen($text); $X++ ) {
$activeKey = substr( $text, ($X-1), 1);
$code_string .= $code_array[$activeKey];
$chksum=($chksum + ($code_values[$activeKey] * $X));
}
$code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];
$code_string = "211214" . $code_string . "2331112";
$code_length = 20;
if ($print) {
$text_height = 30;
} else {
$text_height = 0;
}
for ( $i=1; $i <= strlen($code_string); $i++ ){
$code_length = $code_length + (integer)(substr($code_string,($i-1),1));
}
if ( strtolower($orientation) == "horizontal" ) {
$img_width = $code_length*$SizeFactor;
$img_height = 192;
$img_width=384;// set hardcoded height -width as client suggested
} else {
$img_width = 384;
$img_height = $code_length*$SizeFactor;
}
$image = imagecreate($img_width, $img_height + $text_height);
$black = imagecolorallocate ($image, 0, 0, 0);
$white = imagecolorallocate ($image, 255, 255, 255);
imagefill( $image, 0, 0, $white );
if ( $print ) {
imagestring($image, 5, 31, $img_height, $text, $black );
}
$location = 10;
for ( $position = 1 ; $position <= strlen($code_string); $position++ ) {
$cur_size = $location + ( substr($code_string, ($position-1), 1) );
if ( strtolower($orientation) == "horizontal" )
imagefilledrectangle( $image, $location*$SizeFactor, 0, $cur_size*$SizeFactor, $img_height, ($position % 2 == 0 ? $white : $black) );
else
imagefilledrectangle( $image, 0, $location*$SizeFactor, $img_width, $cur_size*$SizeFactor, ($position % 2 == 0 ? $white : $black) );
$location = $cur_size;
}
// Draw barcode to the screen or save in a file
if ( $filepath=="" ) {
header ('Content-type: image/png');
imagepng($image);
imagedestroy($image);
} else {
imagepng($image,$filepath);
imagedestroy($image);
}
}
Can this be achieved?
If Someone is still looking for this answer, this is how it can be done.
I am using the CodeItNow barcode generator available at Github. Details https://github.com/codeitnowin/barcode-generator here.
After Setup, use this code.
<?php
use CodeItNow\BarcodeBundle\Utils\QrCode;
use CodeItNow\BarcodeBundle\Utils\BarcodeGenerator;
$barcode = new BarcodeGenerator();
$barcode->setText("Any Data You Need To Be Here");
$barcode->setType(BarcodeGenerator::Code128);
$barcode->setScale(2);
$barcode->setThickness(25);
$barcode->setFontSize(10);
$barcode->setlabel("Any Label for Barcode");
$code = $barcode->generate();
echo '<img src="data:image/png;base64,'.$code.'" width="400" height="60"/>';
?>
The width and height of the barcode can be defined in the image tag.
I have an array of a string to fit on a page, i managed to center it horizontal now i need to do that vertical in a specific part of image.
$imgWidth = 240;
$imgHeight = 900;
$IMG = imagecreatetruecolor($imgWidth,$imgHeight);
$font_type_bold = '/dejavu/DejaVuSansCondensed-Bold.ttf';
$background = imagecolorallocate($IMG, 78,129,154);
$text_white = imagecolorallocate($IMG, 255,255,255);
$IMG = imagecreatetruecolor($imgWidth,$imgHeight);
$max_lenght_of_title = 15;
$special_issue_name_mod="This is some example text to be put on the page and split into array to fit he width of frame";
$text = explode("\n", wordwrap($special_issue_name_mod, $max_lenght_of_title));
$line_height=30;
imageline($IMG, 0, 500, 240, 500, $text_white);
imageline($IMG, 0, 100, 240, 100, $text_white);
for($i=0;$i<count($text);$i++) {
$font_size_si_name_horizontal = 21;
//Center the text
$size = imagettfbbox(20, 0, $font_type_bold, $text[$i]);
$long_text = $size[2] + $size[0];
$posx = ($imgWidth - $long_text) / 2;
imagettftext($IMG, $font_size_si_name_horizontal, 0, $posx - 5, 150+ $line_height + $line_height * $i , $text_white, $font_type_bold, $text[$i]);
}
header("Content-type: image/png");
imagepng($IMG);
imagecolordeallocate($IMG, $text_color );
imagecolordeallocate($IMG, $background );
The result is this and i need it to be in specific part of page, for example selected one
So how can i make this to not be the fixed middle but adjustive one depending on what part of page the text will be.
Note: Text can be longer, so thats the main problem. Depending on lenght of text the title should be in the middle
<?php
$imgWidth = 240;
$imgHeight = 900;
$IMG = imagecreatetruecolor($imgWidth,$imgHeight);
$font_type_bold = 'BioRhyme/BioRhyme-Regular.ttf';
$background = imagecolorallocate($IMG, 78,129,154);
$text_white = imagecolorallocate($IMG, 255,255,255);
$IMG = imagecreatetruecolor($imgWidth,$imgHeight);
$max_lenght_of_title = 15;
$special_issue_name_mod="This is some example text to be put on the page and split into array to fit he width of frame";
$text = explode("\n", wordwrap($special_issue_name_mod, $max_lenght_of_title));
$line_height=30;
$vert = ($imgHeight/5);
for($i=0;$i<count($text);$i++) {
$font_size_si_name_horizontal = 21;
//Center the text
$size = imagettfbbox(20, 0, $font_type_bold, $text[$i]);
$long_text = $size[2] + $size[0];
$posx = ($imgWidth - $long_text) / 2;
imagettftext($IMG, $font_size_si_name_horizontal, 0, $posx - 5, $vert+ $line_height + $line_height * $i , $text_white, $font_type_bold, $text[$i]);
}
header("Content-type: image/png");
imagepng($IMG);
imagecolordeallocate($IMG, $text_color );
imagecolordeallocate($IMG, $background );
?>
I have added $vert = ($imgHeight/5); and put into imagettftext() for dynamically calculted vertical Y index!
I figured out a way to solve this
$middle_of_page = 350;
for($i=0;$i<count($text);$i++) {
$middle_of_page = $middle_of_page - 21;
}
imagettftext($IMG, $font_size_si_name_horizontal, 0, $posx - 5, $middle_of_page+$line_height + $line_height * $i , $text_white, $font_type_bold, $text[$i]);
With the $middle_of_page part i regulate the point where text with array size of one will be, and then i just remove by font size of 21 for every $i that stands for array size of string.
I'm working on a project where I have to convert some instructions text to images and send them by email.
For testing purpose, I'm trying a simple code to output a converted text as an image on the browser, but it's always returning a small white box like this:
I have GD installed on my server.
Here's my code:
<?php
header("Content-type: image/png");
function drawImage()
{
$width = 0;
$height = 0;
$offset_x = 0;
$offset_y = 0;
$bounds = array();
$image = "";
$msg = "Some Sample Text....";
$font = "ARIAL.TTF";
$size = 24; // default font size.
$rot = 0; // rotation in degrees.
$pad = 0; // padding.
$transparent = 1; // transparency set to on.
$red = 0; // black text...
$grn = 0;
$blu = 0;
$bg_red = 255; // on white background.
$bg_grn = 255;
$bg_blu = 255;
// get the font height.
$bounds = ImageTTFBBox($size, $rot, $font, "W");
if ($rot < 0)
{
$font_height = abs($bounds[7]-$bounds[1]);
}
else if ($rot > 0)
{
$font_height = abs($bounds[1]-$bounds[7]);
}
else
{
$font_height = abs($bounds[7]-$bounds[1]);
}
// determine bounding box.
$bounds = ImageTTFBBox($size, $rot, $font, $msg);
if ($rot < 0)
{
$width = abs($bounds[4]-$bounds[0]);
$height = abs($bounds[3]-$bounds[7]);
$offset_y = $font_height;
$offset_x = 0;
}
else if ($rot > 0)
{
$width = abs($bounds[2]-$bounds[6]);
$height = abs($bounds[1]-$bounds[5]);
$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
$offset_x = abs($bounds[0]-$bounds[6]);
}
else
{
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$offset_y = $font_height;;
$offset_x = 0;
}
$image = imagecreate($width+($pad*2)+1,$height+($pad*2)+1);
$background = ImageColorAllocate($image, $bg_red, $bg_grn, $bg_blu);
$foreground = ImageColorAllocate($image, $red, $grn, $blu);
if ($transparent) ImageColorTransparent($image, $background);
ImageInterlace($image, false);
// render the image
ImageTTFText($image, $size, $rot, $offset_x+$pad, $offset_y+$pad, $foreground, $font, $msg);
// output PNG object.
imagePNG($image);
}
drawImage();
?>
Is this as simple as changing:
$image = imagereate
to
$image = imagecreate
Looks like a typo to me.
I am using PHP GD library to merge two icons into one. But in one of the cases the smaller icon goes into back and bigger icon overlaps it completely, I need the small icon to be on top.
Here is my code for merging two icons,
MergeIcons.php
$firstIcon = $_GET['icon1'];
$secondIcon = $_GET['icon2'];
$image = imagecreatefrompng($firstIcon);
$x1 = -1;
$y1 = -1;
$i = 0;
$xCords = array(); // Array to save non-transperent x cords
$yCords = array(); // Array to save non-transperent y cords
for($x=0;$x<16;$x++)
{
for($y=0;$y<16;$y++)
{
if (!transparent(imagecolorat($image, $x, $y)))
{
$xCords[$i] = $x;
$yCords[$i] = $y;
$i++;
}
}
}
$minX = min($xCords);
$minY = min($yCords);
$width = 16 - $minX;
$height = 16 - $minY;
$canvas = imagecreatetruecolor(16,16);
$col = imagecolorallocatealpha($canvas,0,0,0,127);
imagefilledrectangle($canvas,0,0,16,16,$col);
imagealphablending($canvas, true);
imagesavealpha($canvas, true);
imagefill($canvas, 0, 0, $col);
imagecopy($canvas, $image, 0, 0, $minX , $minY, $width, $height);
$dest = $canvas;
$src = imagecreatefrompng($secondIcon);
imagealphablending($dest, true);
imagesavealpha($dest, true);
$swidth = imagesx($src);
$sheight = imagesy($src);
imagecopy($dest, $src, 0,0,0,0,$swidth,$sheight);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
function transparent($pixelValue)
{
$alpha = ($pixelValue & 0x7F000000) >> 24;
$red = ($pixelValue & 0xFF0000) >> 16;
$green = ($pixelValue & 0x00FF00) >> 8;
$blue = ($pixelValue & 0x0000FF);
if($alpha == 127)
return true;
else
return false;
}
Here is how I call the mergeicons.php
echo '<li><img src="MergeIcons.php?icon1='.$secondIconPath.'&icon2='.$firstIconPath.'"/></li>';
In this case, second icon is a small icon and first icon is bigger icon, I want smaller icon on top of bigger icon ( assume its like "Bring it to Front ").
Is that possible?
"bring it front " - i think you want to put one image as a layer on the other
http://phpimageworkshop.com/
so I am making three images transparent and layer them over each other. I followed example code in this post: Remove Image background with php and save transparent png but the extracted images all have ugly white borders around them due to the images do not have a complete uniform single-color background. Even though they may appear white, in fact there are usually different shades of grey or even blue involved.
So now I want to remove these ugly white borders in the image.
I found a java function online which solves the problem: http://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/ Here is the code he uses:
private Image makeColorTransparent(final BufferedImage im, final Color color, int tolerance) {
int temp = 0;
if (tolerance < 0 || tolerance > 100) {
System.err.println("The tolerance is a percentage, so the value has to be between 0 and 100.");
temp = 0;
} else {
temp = tolerance * (0xFF000000 | 0xFF000000) / 100;
}
final int toleranceRGB = Math.abs(temp);
final ImageFilter filter = new RGBImageFilter() {
// The color we are looking for (white)... Alpha bits are set to opaque
public int markerRGBFrom = (color.getRGB() | 0xFF000000) - toleranceRGB;
public int markerRGBTo = (color.getRGB() | 0xFF000000) + toleranceRGB;
public final int filterRGB(final int x, final int y, final int rgb) {
if ((rgb | 0xFF000000) >= markerRGBFrom && (rgb | 0xFF000000) <= markerRGBTo) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
} else {
// Nothing to do
return rgb;
}
}
};
final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
But I don't know how to do this with php.
Anyone who can help me?
You can use the IMagick::paintTransparentImage
The signature for this method is as follows
bool Imagick::paintTransparentImage ( mixed $target , float $alpha , float $fuzz );
An example use case for this would be:
$im = new Imagick("test.jpg");
$im->paintTransparentImage(($im->getImagePixelColor(0, 0), 0, 1200));
$im->setImageFormat("png");
$im->writeImage("test.png");
You will have to play arround with the $fuzz parameter to get the kind of results you are looking for.
function transparant($url){
$img = urldecode(trim($url));
$imgx = imagecreatefromjpeg($img);
$img_w = imagesx($imgx);
$img_h = imagesy($imgx);
$im = imagecreatetruecolor($img_w, $img_h);
imagesavealpha( $im, true );
$rgb = imagecolorallocatealpha( $im, 0, 0, 0, 127 );
imagefill( $im, 0, 0, $rgb );
$color = imagecolorat( $imgx, 1, 1);
$temp = 0;
$tolerance = 20;
$temp = $tolerance * (0xFF000000 | 0xFF000000) / 100;
$toleranceRGB = abs($temp);
$startcolor = $color - $toleranceRGB;
$endcolor = $color + $toleranceRGB;
for( $x = 0; $x < $img_w; $x++ ) {
for( $y = 0; $y < $img_h; $y++ ) {
$c = imagecolorat( $imgx, $x, $y );
$startcolor."|".$c."|".$endcolor."<br>";
if ($c > $startcolor && $c < $endcolor){}else{
imagesetpixel( $im, $x, $y, $c);
}
}
}
$filename = basename($img);
header('Content-type: image/png');
imagepng($im, null);
imagedestroy($im);
imagedestroy($imgx);
}
transparant([url encoded image]);