Joining 2 images by using PHP GD & Image Functions - php

I am creating a plugin where i am using a font file to generate the text, then converting it to image and attaching 2 chains on both side to give it a proper look.
But problem is the chains on both side are given by position, so that's obviously not going to touch the text for each and every alphabet.
Any different solutions to make it as intended. I am posting the current code, please let me know how to fix this and attch the chains to the starting and ending alphabet.
$text = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '';
$color = isset( $_POST['color'] ) ? sanitize_text_field( wp_unslash( $_POST['color'] ) ) : '';
$st yle = isset( $_POST['style'] ) ? sanitize_text_field( wp_unslash( $_POST['style'] ) ) : '';
$align = isset( $_POST['align'] ) ? sanitize_text_field( wp_unslash( $_POST['align'] ) ) : '';
ob_start();
// Set the content-type
header('Content-Type: image/png');
$file_id = get_field( "style", $style );
$font = get_attached_file( $file_id['id'] );
// Create the image
$im = imagecreatetruecolor(400, 400);
$color_code = imagecolorallocate($im, 192, 192, 192);
// Create some colors
$grey = imagecolorallocate($im, 128, 128, 128);
$white = imagecolorallocate($im, 255, 255, 255);
imagealphablending($im, false);
$transparency = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparency);
imagesavealpha($im, true);
list($width, $height) = getimagesize($chain_link_l);
$chain_l = imagecreatefrompng($chain_link_l);
$chain_r = imagecreatefrompng($chain_link_r);
$font_size = kp_create_fontsize(60, $font, $text, $align);
// Get Bounding Box Size
$text_box = imagettfbbox($font_size, 0,$font,$text);
// Get your Text Width and Height
$text_width = $text_box[2]-$text_box[0];
$text_height = $text_box[7]-$text_box[1];
// Calculate coordinates of the text
$x = 200 - ($text_width/2);
$y = 250 - ($text_height/2);
$chain_l_x = $x - 80 + 15 ;
$chain_r_x = $x + $text_width - 23 ;
imagecopyresampled($im, $chain_l, $chain_l_x, 50, 0, 0, 100, 200, $width, $height);
imagecopyresampled($im, $chain_r, $chain_r_x, 50, 0, 0, 100, 200, $width, $height);
// Add the text
imagettftext($im, $font_size, 0, $x+2, $y, $grey, $font, $text);
imagettftext($im, $font_size, 0, $x, $y, $color_code, $font, $text);
imagepng($im);
$imagedata['src'] = base64_encode ( ob_get_contents() );
ob_get_clean();

Related

How to add text on image using PHP

I have this image:
Between the text, I want to add some text. To do that I have the following code:
<?php
error_reporting(E_ALL);
$thumb_src = 'copyright-symbol.png';
$explode = explode('.', $thumb_src);
$extension = strtolower( end ( $explode ) );
$file_name = basename( $thumb_src );
//image_resize_base_width( $relative_url, $relative_url, 350, $extension);
$jpg_image = imagecreatefrompng( $thumb_src );
// set font size
$font = #imageloadfont($jpg_image);
$fontSize = imagefontwidth($font);
$orig_width = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);
// Create your canvas containing both image and text
$canvas = imagecreatetruecolor( $orig_width, ($orig_height + 40 ) );
// Allocate A Color For The background
$bcolor = imagecolorallocate( $canvas, 255, 255, 255 );
// Add background colour into the canvas
imagefilledrectangle( $canvas, 0, 0, $orig_width, ($orig_height + 40), $bcolor );
// Save image to the new canvas
imagecopyresampled( $canvas, $jpg_image, 0, 0, 0, 0, $orig_width, $orig_height, $orig_width, $orig_height );
$font_path = 'font/arial.ttf';
$path = 'upload';
$text = 'cc-by-nd-';
// Allocate A Color For The Text
$color = imagecolorallocate($canvas, 0, 0, 0);
// Print Text On Image
imagettftext( $canvas, 13, 0, 0, $orig_height + 25, $color, $font_path, $text) ;
// Send Image to Browser
imagepng( $canvas, $path . '/' . $file_name );
// Clear Memory
imagedestroy($canvas);
But the code is not adding text to the image. Is there anything wrong in my code?
You may change the codes to
<?php
error_reporting(E_ALL);
header('Content-Type: image/png');
$thumb_src = 'copyright-symbol.png';
$explode = explode('.', $thumb_src);
$extension = strtolower( end ( $explode ) );
$file_name = basename( $thumb_src );
//image_resize_base_width( $relative_url, $relative_url, 350, $extension);
$jpg_image = imagecreatefrompng( $thumb_src );
// set font size
$font = #imageloadfont($jpg_image);
$fontSize = imagefontwidth($font);
$orig_width = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);
// Create your canvas containing both image and text
$canvas = imagecreatetruecolor( $orig_width, ($orig_height + 40 ) );
// Allocate A Color For The background
$bcolor = imagecolorallocate( $canvas, 255, 255, 255 );
// Add background colour into the canvas
imagefilledrectangle( $canvas, 0, 0, $orig_width, ($orig_height + 40), $bcolor );
// Save image to the new canvas
imagecopyresampled( $canvas, $jpg_image, 0, 0, 0, 0, $orig_width, $orig_height, $orig_width, $orig_height );
$font_path = './font/arial.ttf';
//$path = 'upload';
// Create some colors
$white = imagecolorallocate($canvas, 255, 255, 255);
$grey = imagecolorallocate($canvas, 128, 128, 128);
$black = imagecolorallocate($canvas, 0, 0, 0);
imagefilledrectangle($canvas, 0, 0, 399, 29, $white);
// The text to draw
// Replace path by your own font path
$font = 'font/arial.ttf';
$text = 'cc-by-nd-';
// Add some shadow to the text
imagettftext( $canvas, 100, 0, ($orig_width / 2) - 300, ($orig_height/2) , $color, $font_path, $text) ;
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($canvas);
imagedestroy($canvas);
?>

The image is not shown when merging two images in PHP GD library in wordpress

I got this code to work but after few weeks the code is not working ,i did try to see where the bug come from but i have no idea.
I did create image with GD library and it is created successfully but when i want to merge it with another picture it show only black screen.Any idea of how to solve the issue please .Here is my code:
Please note that the image is created and saved successfully,so there is no problem with create_image() function.
function create_image()
{
$cer_id = get_the_ID();
$cer_org_nummer = get_field('orgnummer_dqm');
$cer_today = date('Y-m-d');
$cer_org_name = get_field('foretags_namn_dqm');
$cer_org_date = $cer_org_nummer . ' | ' . $cer_today;
$im = #imagecreate(500, 450);
$yellow = imagecolorallocate($im, 255, 255, 255); // yellow
imagecolortransparent($im, $yellow);
$black = imagecolorallocate($im, 252, 195, 91);
$string = "Digital Quality Managment";
$font = FUNC_PLUGIN_D_DQM . '/font/Roboto-Regular.ttf';// black
/* imagestring($im, $font, 50, 300, $string, $black);*/
/* imagestring($im, $font, 3, 300, $cer_org_name, $black);*/
/* imagestring($im, $font, 0, 10, $cer_org_date, $black);*/
imagettftext($im, 35, 0, 60, 270, $black, $font, 'CERTIFIED');
imagettftext($im, 25, 0, 0, 320, $black, $font, $cer_org_date);
imagepng($im, "wp-content/plugins/certificates/cer_images/image_$cer_id.png");
imagedestroy($im);
}
create_image();
$image1 = "wp-content/plugins/certificates/cer_images/dqm_png_logo.png";
$image2 = "wp-content/plugins/certificates/cer_images/image_$cer_id.png";
list($width, $height) = getimagesize($image2);
$image1 = imagecreatefromstring(file_get_contents($image1));
$image2 = imagecreatefromstring(file_get_contents($image2));
imagealphablending($image1, FALSE);
imagesavealpha($image1, TRUE);
imagecopymerge($image1, $image2, 110, 50, 0, 0, $width, $height, 100);
header("Content-type: image/png");
imagepng($image1);
imagedestroy($image1);
please try the code below
This code works for me..
$pro_directory_path = get_attached_file( get_post_thumbnail_id($_REQUEST['apid']),'full' );
$mainmetadata = wp_get_attachment_metadata( get_post_thumbnail_id( $_REQUEST['apid'] ) );
$pro_img_name = $mainmetadata['file'];
$pro_img_name_extra = '1'.$mainmetadata['file'];
// $val_id is id of image(bigger image/background Image)
$directory_path = get_attached_file( $val_id,'full' );
$metadata = wp_get_attachment_metadata( $val_id );
$name = $metadata['file'];
$bgFile = $directory_path;
// We want our final image to be 76x76 size
//user $metadata to get ordiginal height width of image
// $value contain height width of wall mentioned by admin
$x = $metadata['width'];
$y = $metadata['height'];
$x_inch = $metadata['width']/96;
$y_inch = $metadata['height']/96;
$width_proption = $wall_w_inch/$x_inch;
$height_proption = $wall_h_inch/$y_inch;
$img_pos_x = ($x/2)-($resizedwidth/2);
$img_pos_y = ($y/2)-($resizedheight);
/* Code to generate custom wall images - STart */
$inn_width = ($innerimg_width / $width_proption) * 12 ;
$inn_height = ($innerimg_height / $height_proption) * 12 ;
$resizedFilename = get_template_directory().'/woocommerce/temp_images/cust_'.$pro_img_name;
$img_url_extra = get_stylesheet_directory_uri().'/woocommerce/temp_images/cust_'.$pro_img_name;
//function to resize Artist product image in desired size's (Variation sizes).
$imgData = resize_image($pro_directory_path, $inn_width, $inn_height);
imagepng($imgData, $resizedFilename);
list($resizedwidth, $resizedheight) = getimagesize($resizedFilename);
/* Code to generate custom wall images - ENDS */
$imageFile = $resizedFilename;
// dimensions of the final image
$final_img = imagecreatetruecolor($x, $y);
// Create our image resources from the files
$image_1 = imagecreatefrompng($bgFile);
$image_2 = imagecreatefrompng($imageFile);
// Enable blend mode and save full alpha channel
imagealphablending($final_img, true);
imagesavealpha($final_img, true);
// Copy our image onto our $final_img
imagecopy($final_img, $image_1, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_2, $img_pos_x, $img_pos_y, 0, 0, $resizedwidth, $resizedheight);
ob_start();
$path = __DIR__ .'/custom_wall_img/'.$name;
imagepng($final_img,$path);
$watermarkedImg = ob_get_contents(); // Capture the output
ob_end_clean(); // Clear the output buffer

PHP - GD - how wrap a text

I created an image 500X200 with white background and text on it.
How can I wrap "Text" if it will be long in GD.
$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill ( $image, 0, 0, $white );
...
imagettftext($image, 18, 0, $x, 100, $black, $font, "text");
GD supports rotation, vertical and horizontal alignments, configurable line heights and leading, word wrap (obviously), character wrap, and text outlines. While this frankly belongs in a class (or a namespace) due its size, it’s in a function. You’re free to trim away unneeded functionality.
Example usage:
$size = 14;
$angle = 34;
$left = 10;
$top = 40;
$color = imagecolorallocate($im, 255, 0, 0);
$fontfile = "arial.ttf";
$text = "Test";
$opt = array('width' => 300,'line_height' => 15,'orientation' =>array(ORIENTATION_TOP, ORIENTATION_LEFT),
'align' => ALIGN_LEFT,
'v_align' => VALIGN_TOP,
'outlines' = array(
array(5, imagecolorallocate($im, 0, 255, 0)),
array(2, imagecolorallocate($im, 0, 0, 255)),
),
// More options
);
imagettftextboxopt($im, $size, $angle, $left, $top, $color, $fontfile, $text, $opt);
UPDATE:
And also refer this question

php GD image functions. Image half way filled with specific color

I am testing out the php GD functions as follows:
$height = 200;
$width = 200;
$im = imagecreatetruecolor( $width, $height );
$blue = imagecolorallocate( $im, 0, 0, 64 );
imagefill( $im, 0, 50, $blue );
Header( 'Content-type: image/png' );
imagepng( $im );
imagedestroy( $im );
I was trying to have the image half way filled with blue but am not getting that result. Any pointers on where the problem is?
I think drawing a rectangle will help:
$height = 200;
$width = 200;
$im = imagecreatetruecolor( $width, $height );
$blue = imagecolorallocate( $im, 0, 0, 64 );
$white = imagecolorallocate($im, 255, 255, 255);
imagefill( $im, 0, 0, $white);
imagefilledrectangle( $im, 0, 0, 100, 200, $blue );
Header( 'Content-type: image/png' );
imagepng( $im );
imagedestroy( $im );
Should produce an image with white background and a blue rectangle from 0 to 200 along Y and 0 to 100 along X.

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

Categories