Image print's but it stops every other thing - php

this is Index file , it's task is to provide values to the cover.php file and get the image in return...
<?php
require('cover.php');
$name="Tushar Kesarwani";
$id=112200333;
$dob="9/9/1990";
$email="tushar.kesarwani2#gmail.com";
$profilelink="http://www.facebook.com/iamtusharDOTcom";
$relationship= "single";
$me = imagecreatefrompng('me.png');
$ob= new createimage();
$ob->create($name,$id,$dob,$email,$profilelink,$relationship,$me);
?>
<h1>hdhdhdh</h1>
this is cover.php, it's task is to create a image
<?php
class createimage
{
function create($name,$id,$dob,$email,$profilelink,$relationship,$me)
{
$base = imagecreatefromjpeg('fb.jpg');
$white = ImageColorAllocate($base, 255, 255, 255);
imagecopy($base, $me, 93, 102, 0, 0, imagesx($me), imagesy($me));
$font = 'ARIAL.TTF';
$font2='Cacophony Loud.ttf';
imagettftext($base, 25, 0, 228, 133, $white, $font2, $name);
imagettftext($base, 16, 0, 268, 183, $white, $font, $id);
imagettftext($base, 16, 0, 291, 224, $white, $font, $dob);
imagettftext($base, 13, 0, 126, 294, $white, $font, $email);
imagettftext($base, 13, 0, 126, 323, $white, $font, $profilelink);
imagettftext($base, 13, 0, 126, 353, $white, $font, $relationship);
// Output and free memory
header('Content-type: image/png');
imagejpeg($base);
imagedestroy($base);
}
}
?>
The problem is that.. images is printing but tag or anything after the image is not working

This is because you're sending an actual image. The header image/png is what is doing it. If you want to set it as an img, save the file to the server and load it in the src attribute, or base64 encode it in the src attribute (properly)
To save it as an image do something to the tune of:
// Remove the proceeding line
// header('Content-type: image/png');
$name = '/location/where/you/want/to/save/file.jpg';
imagejpeg($base, $name);
echo "<img src='$name' />";
Also, you're sending headers for png files but you're creating a jpeg file. You might want to correct that.

Related

German umlauts with imagepng bad output

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:

PHP imagejpeg not displaying image on Browser

I am using imagejpg() to display an image on the browser and place text over the image.
I have used this before and it has worked. Now I switched domain hosts and perhaps the PHP version is different as the image is now not displaying.
It displays a small square in the middle of the screen, like a broken link, but the image is definitely there.
Could someone please assist with this?
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('images1/new_image.jpg');
// Allocate A Color For The Text
$red = imagecolorallocate($jpg_image, 255, 0, 0);
$green = imagecolorallocate($jpg_image, 0, 255, 0);
// Set Path to Font File
$font_path = 'canadian.ttf';
$text1 = "hello";
$text2 = "there";
$text3 = "world";
//Floor 17
imagettftext($jpg_image, 12, 0, 272, 17, $green, $font_path, $text1);
imagettftext($jpg_image, 12, 0, 200, 38, $green, $font_path, $text2);
imagettftext($jpg_image, 12, 0, 480, 17, $green, $font_path, $text3);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
Thank you in advance.
Rob
I just figured it out. Answer below
I moved the header down and it sorted out the problem.
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('images1/new_image.jpg');
// Allocate A Color For The Text
$red = imagecolorallocate($jpg_image, 255, 0, 0);
$green = imagecolorallocate($jpg_image, 0, 255, 0);
// Set Path to Font File
$font_path = 'canadian.ttf';
$text1 = "hello";
$text2 = "there";
$text3 = "world";
//Floor 17
imagettftext($jpg_image, 12, 0, 272, 17, $green, $font_path, $text1);
imagettftext($jpg_image, 12, 0, 200, 38, $green, $font_path, $text2);
imagettftext($jpg_image, 12, 0, 480, 17, $green, $font_path, $text3);
header('Content-type: image/jpeg');
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
I had the same problem, found out that if there is a blank space before the header, the image doesn't work.
I was able to save the image on the server but not to get the browser to display it.
Removing every blank space did the trick for me.
I think it's the same reason why your script didn't work with the header at the beginning.

Working with Multiline Texts on imagettftext

My code is here, Im trying to create png widget for a website. I do correct others but when im trying to fit my long text in to my cards 181*312 px
I must fit my text in to text area, and its impossible to change my widget sizes
So how can wrap my text?
$kartquery = mysql_query("SELECT * FROM status");
While($kartqueryyaz=mysql_fetch_array($kartquery)){
// Q & A
$id = $kartqueryyaz['id'];
$longdesc = $kartqueryyaz['longdesc'];
$correct = $kartqueryyaz['correct'];
$wrong1 = $kartqueryyaz['wrong1'];
$wrong2 = $kartqueryyaz['wrong2'];
$wrong3 = $kartqueryyaz['wrong3'];
$wrong4 = $kartqueryyaz['wrong4'];
// carttype
$png_image = imagecreatefrompng('card1.png');
// Text Color #white ffffff
$white = imagecolorallocate($png_image, 255, 255, 255);
// Font
$font_path = 'GothamBook.ttf';
$font_pathdogru = 'GothamRoundedBold.ttf';
imagettftext($png_image, 25, 0, 55, 230, $white, $font_path, $longdesc);
imagettftext($png_image, 25, 0, 150, 730, $white, $font_pathdogru, $correct);
imagettftext($png_image, 25, 0, 150, 850, $white, $font_path, $wrong1);
imagettftext($png_image, 25, 0, 150, 970, $white, $font_path, $wrong2);
imagettftext($png_image, 25, 0, 150, 1100, $white, $font_path, $wrong3);
imagettftext($png_image, 25, 0, 150, 1220, $white, $font_path, $wrong4);
//imagepng($png_image);
imagepng($png_image, "img/".$id.".png");
imagedestroy($png_image);
}
?>

Resize image before using in imagecopymerge

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);

How to save generated image (from imagecreatefromjepeg) in the server (not in the PC)?

i am asked to create a from that will ask for text inputs and a photo to be uploaded. then create an image wherein those are displayed. the generated image must then be saved to the server (not using database). i have a code here that successfully generates the image (i used imagejpeg and imagecreatefromjpeg in this case. but then i still need to save it to the server with the file extension .jpg. i also need to give it a unique name.
i tried using header('Content-Disposition: attachment; filename="image.jpg"'); but what it does is save it to the PC not in the server. below is my code. please feel frre to edit it. also, please leave some comments so that i can understand it and not just do copy paste. thank you so much in advance for you help. i really need to make it working by now. thanks again
//for textbox input
$title = $_POST['title'];
$story = "My super story begins with" . $_POST['story'] . " My task was " . $_POST['task'] ." With the super power of ". $_POST['power'] ." I solved it by ". $_POST['solve'] ." The result was". $_POST['result'];
//header('Content-Disposition: attachment; filename="image.jpg"'); //this works for saving img to PC or downloading it force .jpg ext
header('Content-Type: image/jpeg');
$upload = $uploadFilename; //this is for getting the uploaded file
$im = imagecreatefromjpeg("bg2.jpg");
$img2 = imagecreatefromjpeg($upload);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'arialbi.ttf';
$font2 = 'ariali.ttf';
imagettftext($im, 24, 0, $width_sum, 300, $black, $font, $title);
$newtext = wordwrap($story, 35, "\n", true);
$newertext2 = explode ("\n", $newtext);
imagettftext($im, 8, 0, 300, 362, $black, $font, $story);
imagettftext($im, 8, 0, 300, 374, $black, $font2,$story);
imagettftext($im, 8, 0, 300, 386, $black, $font, $story);
imagettftext($im, 8, 0, 300, 398, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 410, $black, $font, $story);
imagettftext($im, 8, 0, 300, 422, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 434, $black, $font,$story);
imagettftext($im, 8, 0, 300, 446, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 458, $black, $font,$story);
imagettftext($im, 8, 0, 300, 570, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 582, $black, $font, $story);
imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
imagejpeg($im, null, 100);
//closing for imagejpeg
imagejpeg($im);
imagedestroy($im);
Well, as the manual of imagejpeg says: the second argument is the filename where you want to store the image.
Try imagejpeg($im, "/PATH/IMAGE_NAME.jpeg")
This should help.

Categories