Still on imagettftext topic, now I have question how to display blob image on the page that created from imagecreatefrompng.
<?php
include('../db.php');
$id = $_GET['id'];
$sql = "SELECT * FROM data WHERE id=$id";
$result = $conn->query($sql);
$row = $result->fetch_array();
$id = $row['id']; //varchar
$name = $row['name']; //varchar
$address = $row['address']; //varchar
$photo = base64_encode($row['photo']); //blob image
$im = imagecreatefrompng('../img/idcard.png');
$black = imagecolorallocate($im, 0, 0, 0);
$font = "../fonts/Ubuntu-R.ttf";
imagettftext($im, 15, 0, 200, 175, $black, $font, $id);
imagettftext($im, 15, 0, 240, 200, $black, $font, $name);
imagettftext($im, 15, 0, 280, 275, $black, $font, $address);
imagettftext($im, 15, 0, 350, 315, $black, $font, $photo);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
My code display id card, text, but image/photo just display as text character
$im2 = imagecreatefromstring($photo);
imagecopyresized ($im,$im2,2,2,0,0,46,46,183,173);//see parameters at: http://php.net/manual/en/function.imagecopyresized.php
(if You not need resizing You can use imagecopy too)
<?php
$dest = imagecreatefrompng($im);
$src = imagecreatefromjpeg('profile_pic.jpg');
imagecopymerge($dest, $src, 250, 650, 0, 0, imagesx($src), imagesx($src), 100);
imagecopyresampled($dest, $src, 250, 650, 0, 0, 577, 540, imagesx($src), imagesy($src));
imagejpeg($dest, $new_image_name, 100);
imagedestroy($dest);
?>
Related
I know that the German umlauts output with PHP, can get fixed by using the
header('Content-Type: text/html; charset=utf-8');
at the top of the PHP as shown below.
<?php
header('Content-Type: text/html; charset=utf-8');
echo "<h2>German umlauts: ÄÖÜäöüß</h2>";
echo "<br />";
echo "<h2>Dr. Jörg Großhaderner</h2>";
echo "<br /><br />";
echo '<img src="image.php">';
?>
But there is trouble displaying them on an imagepng code shown below for your review and test.
File: image.php
<?php
header('Content-Type: text/html; charset=utf-8');
$idnum = "ER-CW-R112-DOC1297";
$title = "Dr.";
$firstname = "Jörg";
$lastname = "Großhaderner";
$ward = "Cardiothoracic Ward";
$callcode = "CW894";
// load the image from the file specified as background layout
$im = imagecreatefrompng("images/tempcard1.png");
// if there's an error, stop processing the page:
if(!$im)
{
die("Error creating the temp card!");
}
// define some colours to use with the fonts
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);
$blue = imagecolorallocate($im, 0, 0, 255);
// define the font and some font sizes
$fontsize = 5;
// finally, write the string:
imagestring($im, $fontsize, 130, 105, $idnum , $red);
imagestring($im, $fontsize, 110, 135, $title , $black);
imagestring($im, $fontsize, 140, 135, $firstname , $black);
imagestring($im, $fontsize, 190, 135, $lastname , $black);
imagestring($im, $fontsize, 125, 155, $ward, $black);
imagestring($im, $fontsize, 190, 175, $callcode, $blue);
// output the image
// tell the browser what we're sending it
header('Content-type: image/png; charset=utf-8');
//header('Content-type: image/png;');
// output the image as a png
imagepng($im);
// tidy up
imagedestroy($im);
?>
Files/Links:
screen_shot
temp_card
Updated working image.php:
Thanks to Bernard for his guiding comment.
<?php
header('Content-Type: text/html; charset=utf-8');
$idnum = "ER-CW-R112-DOC1297";
$title = "Dr.";
$firstname = "Jörg";
$lastname = "Großhaderner";
$ward = "Cardiothoracic Ward";
$callcode = "CW894";
// load the image from the file specified as background layout
$im = imagecreatefrompng("images/tempcard1.png");
// if there's an error, stop processing the page:
if(!$im)
{
die("Error creating the temp card!");
}
// define some colours to use with the fonts
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);
$blue = imagecolorallocate($im, 0, 0, 255);
// define the font and some font sizes
$font = 'fonts/OpenSans-Bold.ttf'; // <- This is the font supporting German umlauts
$fontsize = 17;
// finally, write the string:
/*
imagestring($im, $fontsize, 130, 105, $idnum , $red);
imagestring($im, $fontsize, 110, 135, $title , $black);
imagestring($im, $fontsize, 140, 135, $firstname , $black);
imagestring($im, $fontsize, 190, 135, $lastname , $black);
imagestring($im, $fontsize, 125, 155, $ward, $black);
imagestring($im, $fontsize, 190, 175, $callcode, $blue);
*/
/**
* Changed to imagettftext instead of imagestring as per Bernard's comment and works.
* https://stackoverflow.com/users/4401439/bernhard
*/
imagettftext($im, $fontsize, 0, 90, 130, $red, $font, $idnum);
imagettftext($im, $fontsize, 0, 85, 160, $black, $font, $title);
imagettftext($im, $fontsize, 0, 125, 160, $black, $font, $firstname);
imagettftext($im, $fontsize, 0, 175, 160, $black, $font, $lastname);
imagettftext($im, $fontsize, 0, 90, 195, $black, $font, $ward);
imagettftext($im, $fontsize, 0, 160, 220, $blue, $font, $callcode);
// output the image
// tell the browser what we're sending it
header('Content-type: image/png;');
// output the image as a png
imagepng($im);
// tidy up
imagedestroy($im);
Hope it helps others as well.
Screenshot:
I have a prorgam that uses imagecopymerge. Here, I'm using two images. First image for background and second one to be placed inside it. I am able to do so, but now I want the second image to be on a certain size. this image is uploaded by user and saved at my server so i dont have control on it's size. How can I resize the second image before using it in my imagecopymerge? Thanks a lot for answering. Please also consider that resizing it during upload is my least option. I want the size to be 255x175.
This is what I have:
$uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'])
$upload = $uploadFilename;
$im = imagecreatefromjpeg("bg.jpg");
$img2 = imagecreatefromjpeg($upload);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'arialbi.ttf';
$font2 = 'ariali.ttf';
$newtitle = wordwrap($title, 28, "\n", true);
$newertitle = explode("\n", $newtitle);
imagettftext($im, 28, 0, 7, 270, $black, $font, $newertitle[0]);
imagettftext($im, 28, 0, 7, 320, $black, $font, $newertitle[1]);
imagettftext($im, 10, 0, 320, 362, $black, $font, $namehere);
imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
imagedestroy($im);
http://php.net/manual/en/function.imagecopyresampled.php
Take your code...
imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
And change it to...
imagecopyresampled($im, $img2, 10, 350, 0, 0, 255, 175, imagesx($img2), imagesy($img2));
Just simply use php function
given as:
$img = imagecreatefromjpeg("source_of_img.jpg");
$imgresize = imagescale($dp,200,200);
I need to merge two pictures,
my code is like this:
$image = imagecreatefromjpeg("images/big.jpg");
$image1 = imagecreatefrompng("small_image/8.png");
$size = getimagesize("small_image/8.png");
imagecopymerge($image, $image1, 400, 30, 0, 0, $size[0], $size[1], 100);
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image1);
the frist picture is :
second is :
final result not transparent
Please help!!!Thanks!
new version of code:
$image = imagecreatefromjpeg("images/big.jpg");
$image1 = imagecreatefrompng("small_image/8.png");
$size = getimagesize("small_image/8.png");
$background = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $background);
imagealphablending($image1, false);
imagesavealpha($image1, true);
imagecopymerge($image, $image1, 400, 30, 0, 0, $size[0], $size[1], 100);
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image1);
and the result is :
Ok..i found a solution now..
$image = imagecreatefromjpeg("images/show01.jpg"); //
$image1 = imagecreatefrompng("small_image/1.png");//
$image3 = imagecreatefromgif("images/carBg.gif");//
$size = getimagesize("small_image/1.png");
$overlay = imagecreatetruecolor(80, 80);
$white = imagecolorallocate($overlay, 229, 229, 229);
imagefilledrectangle($overlay, 0, 0, 80, 80, $white);
imagecolortransparent($overlay,$white);
imagecopy($overlay, $image1, (80-$size[0])/2, (80-$size[1])/2, 0, 0, $size[0],$size[1]);
imagecopymerge($image3, $overlay, 0, 0, 0, 0, 80, 80, 100);
imagecopymerge($image, $image3, 280, 30, 0, 0, 80, 80, 100);
header('Content-Type: image/png');
// and finally, output the result
imagepng($image);
imagedestroy($image);
I'm trying to use the GD graphics library to make this captcha image, and the box shows up how Id like it to, but the text isn't being displayed over the image at all. I can't figure out why it's not showing up.
<?php
session_start();
putenv('GDFONTPATH=' . realpath('.') );
$font = 'Molle';
header('Content-Type: image/png');
$im = imagecreatetruecolor(260, 40);
$white = imagecolorallocate($im, 255, 255,255);
$grey = imagecolorallocate($im, 215, 215, 215);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 3, 3, 255, 34, $grey);
$text = 'Testing';
$_SESSION["captcha"] = $text;
imagettftext( $im, 20, 0, 16, 26, $white, $font, $text );
imagettftext( $im, 20, 0, 15, 25, $black, $font, $text );
imagepng($im);
imagedestroy($im);
?>
I have a jquery form that posts to a callback page. The callback page uses PHP's GD library to create an image.
The PHP code looks like this:
<?php
include 'inc/config.php';
$caseNum = $_POST['caseNum'];
$wish = $_POST['wish'];
$selectedWishes = join(", ", $wish);
$fname = $_POST['fname'];
$middlename = $_POST['middlei'];
$lname = $_POST['lname'];
$address1 = $_POST['adr1'];
$address2 = $_POST['adr2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$recipt = $_POST['recipt'];
$terms_accepted = $_POST['tou'];
if ($terms_accepted == 'accept') {
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_database);
$select = mysql_query('SELECT * FROM children WHERE caseNumber="'.$caseNum.'" LIMIT 1 ');
$row = mysql_fetch_array($select);
header('Content-type: image/png');
$im = imagecreatefrompng ("images/card_bg.png");
$color = imagecolorallocate($im, 0, 0, 0);
$font = 'inc/fonts/MyriadPro-Regular.otf';
$size = 14;
imagettftext($im, $size, 0, 130, 18, $color, $font, $caseNum);
imagettftext($im, $size, 0, 52, 105, $color, $font, $row['age']);
imagettftext($im, $size, 0, 108, 167, $color, $font, $row['name']);
// Print wishes and word wrap
$lines = explode('|', wordwrap($selectedWishes, 30, '|'));
$y = 238;
foreach ($lines as $line) {
imagettftext($im, $size, 0, 18, $y, $color, $font, $line);
$y += 23;
}
imagettftext($im, $size, 0, 339, 151, $color, $font, $fname.' '.$middlename.' '.$lname);
imagettftext($im, $size, 0, 351, 175, $color, $font, $address1);
imagettftext($im, $size, 0, 365, 196, $color, $font, $address2);
imagettftext($im, $size, 0, 326, 218, $color, $font, $city);
imagettftext($im, $size, 0, 474, 218, $color, $font, $zip);
imagettftext($im, $size, 0, 340, 242, $color, $font, $phone);
imagettftext($im, 9, 0, 333, 268, $color, $font, $email);
if ($row['gender'] == 'male') {
$check = imagecreatefrompng('images/checkmark.png');
imagealphablending($check, true);
imagesavealpha($check, false);
imagecopymerge($im, $check, 74, 3, 0, 0, 10, 15, 80);
}
else if ($row['gender'] == 'female') {
$check = imagecreatefrompng('images/checkmark.png');
imagealphablending($check, true);
imagesavealpha($check, false);
imagecopymerge($im, $check, 74, 38, 0, 0, 10, 15, 80);
}
if ($recipt == 'yes') {
$check = imagecreatefrompng('images/checkmark.png');
imagealphablending($check, true);
imagesavealpha($check, false);
imagecopymerge($im, $check, 324, 383, 0, 0, 10, 15, 80);
}
else if ($recipt == 'no') {
$check = imagecreatefrompng('images/checkmark.png');
imagealphablending($check, true);
imagesavealpha($check, false);
imagecopymerge($im, $check, 367, 383, 0, 0, 10, 15, 80);
}
$imageName = rand();
$ourFileName = $imageName.'.png';
$ourFilePath = 'images/created_tags/';
imagepng ( $im, $ourFilePath.$ourFileName );
imagepng($im);
imagepng($check);
imagedestroy($check);
imagedestroy($im);
readfile ( $ourFileName );
print $ourFilePath.$ourFileName;
exit ();
}
?>
and here is the jQuery:
$.post('selectChildCallback.php', $("#select_child_form").serialize(), function(data) {
$("#post_image").append('<img src="'+ data.path +'" alt="card" />');
}, 'json');
What I can't figure out is how to make the callback page not display the image, but just the path. Right now it prints the image data instead of an image path.
readfile ( $ourFileName );
This line is the problem. According to the docs, it echos the contents of a file to the screen. So PHP is returning a image file (binary) with its file name concatenated to the end of the stream back to jQuery.
Remove this line, and this should work.
EDIT:
imagepng ( $im, $ourFilePath.$ourFileName );
imagepng($im);
imagepng($check);
Why do you call imagepng 3 times? If you don't give it a second parameter, it echos the image to the screen. Also remove the last 2 imagepng lines. PHP is actually returning 3 images concatenated together as well as the file name.
It should look like this:
$imageName = rand();
$ourFileName = $imageName.'.png';
$ourFilePath = 'images/created_tags/';
imagepng($im, $ourFilePath.$ourFileName);
imagedestroy($check);
imagedestroy($im);
echo $ourFilePath.$ourFileName;
exit();