been messing with imagestring trying to change font & font size but i really dont have a clue, i'm new to php and have worked on some code that i came across on a forum and have made it work for me just need to find a way to change font and font size but everything i've tried hasn't given me a good outcome.. any solutions? Please bare in mind i'm new to php and if you could explain as much as possible for your answer thank you so much
$im = imagecreatefrompng("image.png");
// if there's an error, stop processing the page:
if(!$im)
{
die("");
}
// define some colours to use with the image
$black = imagecolorallocate($im, 80, 77, 77);
$font = 'cour.ttf';
// get the width and the height of the image
$width = imagesx($im);
$height = imagesy($im);
$textt = $_GET['textt'];
$dealer = $_GET['dealer'];
$purchase = $_GET['purchase'];
$name = $_GET['name'];
$address = $_GET['address'];
$address1 = $_GET['address1'];
// store the text we're going to write in $textt
putenv('GDFONTPATH=' . realpath('cour.ttf'));
// calculate the left position of the text:
$leftTextPos = ( $width - imagefontwidth($font)*strlen($textt) )/2;
$leftTextPos = ( $width - imagefontwidth($font)*strlen($dealer) )/2;
// finally, write the string:
imagestring($im,$font,$leftTextPos-210,$height-295,$textt,$black);
imagestring($im,$font,$leftTextPos-210,$height-165,$dealer,$black);
imagestring($im,$font,$leftTextPos-200,$height-115,$purchase,$black);
imagestring($im,$font,$leftTextPos-130,$height-70,$name,$black);
imagestring($im,$font,$leftTextPos-100,$height-50,$address,$black);
imagestring($im,$font,$leftTextPos-00,$height-50,$address1,$black);
// 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);
?>
You should try to use imagettftext function.
Related
<?php
// Set the font file and font size for the text
$font_file = '/path/to/font.ttf';
$font_size = 12;
// Create an array of image filenames
$images = array('image1.png', 'image2.png', 'image3.png', 'image4.png', 'image5.png');
// Select a random image filename from the array
$random_image = $images[array_rand($images)];
// Load the image using the GD library
$image = imagecreatefrompng($random_image);
// Get the width and height of the image
$width = imagesx($image);
$height = imagesy($image);
// Send the appropriate HTTP headers and output the image
header('Content-Type: image/png');
imagepng($image);
// Free up memory
imagedestroy($image);
?>
*The code works fine, don't get any errors but when I load the code I get this small white square and nothing else. GD is enabled, but don't know how to solve this solution. *
using the example given at http://www.php.net/manual/en/function.imagecopyresized.php ... how to get image sizes afterward using getimagesize() function?
CODE:
<?php
if(isset($_FILES['images'])){
//TEST1:
$img = resize_this_image_now($_FILES['images']['tmp_name']);
//TEST2:
$img = resize_this_image_now($_FILES['images']['name']);/// This Drastically failed.
$new_image = getimagesize($img);
var_dump($new_image[0]);// I guessed this should have printed out the WIDTH_OF_THE_IMAGE... but, it prints some NON_READABLE stuffs (why?)
}
// The PHP.NET CODE in a Function
function resize_this_image_now($filename){
// File and new size
// $filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
return imagejpeg($thumb);
}
?>
All I want is to get the Size of the Image.... also, is it possible to do something like:
$_FILES['images']['tmp_name'] = $the_newly_resized_image_returned_from_the_PHP_dot_NET_code'; .... So that the ['images']['tmp_name'] will now have as source this new image??
Any suggestion is highly appreciated...
I decided to spend sometime examining you question. What I found out is that, I don't think you'd need to return the resized image through imagejpeg() the way you did. You might also need to add a imagedestroy(), after you call imagejpeg() in you function, to destroy the temporary memory used.
You need to first fully Upload the Image before resizing it. If you'd like, you could send the image in a temporary storage while you do whatever you want to it so that Php does not have to deal with it in a 'tmp_name' format...Then You can destroy the image later on.
Once the image is fully uploaded, things become easier. The codes might look something like:
if(isset($_FILES['images'])){
//may be some random numbers to accompany it.
$rand = floor((mt_rand()+rand()+mt_rand())/3);
//Send it to the temporary folder you have had to create.
if(move_uploaded_file(
$_FILES['images']['tmp_name'],'temporary_storage/image_'.$rand.'.jpg')){
//Then run the `resize` function from here.
$image_resized = resize_this_image_now('temporary_storage/image_'.$rand.'.jpg');
//Now You can get the size if you wish.
list($width,$height) = getimagesize('temporary_storage/image_'.$rand.'.jpg');
// Out put
echo "W:".$width."<br>H:".$height;
//After you use it as desired, you can now destroy it using unlink or so.
unlink('temporary_storage/image_'.$rand.'.jpg');
}else{
echo "Upload Error goes here";
}
}
Note this answer is produced after several trials and errors... Please use this strategy wisely.
Hope it helps.
I have a php file for captcha. But image is not there in web page. I cant find out what is the prob. Please help me. Here is my code.
<?php
ob_start();
session_start();
// Set the content-type
header('Content-Type: image/jpeg');
mimetypes.add_type('application/font-ttf', '.ttf', True)
// Create the image
$im = imagecreatefromjpeg('bg.jpg');
// Create some colors
$R = rand(0,100);
$G = rand(0,100);
$B = rand(0,100);
$cc = imagecolorallocate($im, $R, $G, $B);
// The text to draw
$text = rand(100,10000);
$_SESSION['text'] = $text;
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, rand(40,45), rand(0,1), rand(10,70), rand(38,50), $cc, $font, $text);
$NumberOfLines=15;
imagecolorallocate($im, 15, 142, 210);
$cc=0;
while($cc < $NumberOfLines){
// set random color:::
//assign random rgb values
$c1 = mt_rand(50,200); //r(ed)
$c2 = mt_rand(50,200); //g(reen)
$c3 = mt_rand(50,200); //b(lue)
//test if we have used up palette
if(imagecolorstotal($im)>=255) {
//palette used up; pick closest assigned color
$color = imagecolorclosest($im, $c1, $c2, $c3);
} else {
//palette NOT used up; assign new color
$color = imagecolorallocate($im, $c1, $c2, $c3);
}
// done...
$startH =rand(3,200);
$startTOP = rand(0,8);
$stopH=rand(3,200);
$stopTOP =50;
imageline($im, $startH, $startTOP, $stopH, $stopTOP, $color);
$cc++;
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($im);
imagedestroy($im);
?>
Name of this script file is img.php And it is set as src of img
tag like
img src='img.php'
Here arial.ttf file is in the same folder where this php file resides. Please help me for this. This captcha image is not being loaded.
First remove the ob_start() command. It does not make much sense (at least to me).
Then uncomment the header(..) line so that you see error messages in the browser.
I had to do the following things to get the code up and running:
I removed the mimetypes line (is this PHP at all? If yes, a semicolon is missing)
the script did not find the font - I had to use absolute path and set the permissions right
When you are done with debugging and see fancy chars in your browser window, add the header(..) line again.
So I have this php script which generates random text on an image.
The text file is a different php file and the image file is a seperate php file.
The image.php file calls this text.php to select a random text.
This version works fine, but is it possible to generate a random image on my existing image file?
I have included the current versions of my code.
This is text.php:
<?php
$t[] = 'Sample text 1 ';
$t[] = 'Sample text 2';
shuffle($t);
?>
This is image.php:
<?php
require_once 'text.php';
$text = wordwrap($t[0], 31, "\n", true); //text
$image = imagecreatefromjpeg('img_empty.jpg'); //background image
?>
Any suggestions are welcome.
Your question is bit unclear.. You don't want text.php then you can directly use it's code in image.php as shown below.
image.php
<?php
$t[] = 'Sample text 1 ';
$t[] = 'Sample text 2';
shuffle($t);
$text = wordwrap($t[0], 31, "\n", true); //text
$image = imagecreatefromjpeg('img_empty.jpg'); //background image
Several options here
Have several background-images saved and select one randomly among them.
Use GD and Image Functions to draw on your existing image before outputting it (look at imageline() for drawing lines for example).
Use GD's imagecreatetruecolor() to create the image, to even get random width/height.
if you want to create an image with random text this will do it
image.php
require_once 'text.php';
header("Content-type: image/png");
$string = wordwrap($t[0], 31, "\n", true); //text
$font = 2;
$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);
UPDATED CODE TO YOUR RESPONSE:
if you want to get your random text over a specific image
require_once 'text.php';
header("Content-type: image/png");
$string = wordwrap($t[0], 31, "\n", true); //text
$image = ImageCreateFromJPEG("img_empty.jpg");
//Defining color. Making the color of text as red (#FFF) as 255,000,000
$color = imagecolorallocate($image , 255, 000, 000); //
//put string over image with $color as color
imagestring($image,5,126,22,$string,$color);
imagejpeg($image,NULL,100);
The above code will put a red color random text over your specified image.this works for me. Also try changing the colors defined in imagecolorallocate() function.
reference: http://blog.doh.ms/2008/02/12/adding-text-to-images-in-real-time-with-php/
Having a few teething problems watermarking a photo. It all works fine apart from the watermarked photo's colors become duller than they should be - very noticeable in-fact.
I'm using imagecopyresized to do my watermarking, as this specifically allows me to use PNG-24 watermarks, the others do not. I know the colors are usually OK, as I have just used readfile($url) as a test, and the photos are perfect.
Here is my script:
<?php
// get parent and watermark images & sizes
$image = imagecreatefromjpeg($url);
$imageSize = getimagesize($url);
$watermark = imagecreatefrompng('watermark.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
// calculate new watermark width and position
if ($imageSize[0] > $imageSize[1] || $imageSize[0] == $imageSize[1]) {
$leftPercent = 23;
} else {
$leftPercent = 7;
}
$leftPixels = ($imageSize[0]/100)*$leftPercent;
$newWatermarkWidth = $imageSize[0]-$leftPixels;
$newWatermarkHeight = $watermark_o_height * ($newWatermarkWidth / $watermark_o_width);
// place watermark on parent image, centered and scaled
imagecopyresized(
$image,
$watermark,
$imageSize[0]/2 - $newWatermarkWidth/2,
$imageSize[1]/2 - $newWatermarkHeight/2,
0,
0,
$newWatermarkWidth,
$newWatermarkHeight,
imagesx($watermark),
imagesy($watermark)
);
// print
imagejpeg($image);
// destroy
imagedestroy($image);
imagedestroy($watermark);
?>
How can I stop this from happening? I'm reading about imagecreatetruecolor, does that solve the issue? I'm Googling "imagecreatetruecolor color loss photos" and variations but nobody really talks about this issue. If I do need this function, where would I add that to this script?
This has totally thrown a spanner in the works for me and would love for somebody to tell me where to stick it (not literally).
Here is an example of the color loss. The preview image should be exactly the same colors as the thumbnail. The thumbnails are created using readfile() whereas the previews are created using imagecreatefromjpeg and imagecopresized.
This example code works fine, by using the same characteristics as your images:
Original JPG: dark background; beautiful girl; red dress.
Watermark PNG: transparent background; text; gray color.
<?php
// Path the the requested file (clean up the value if needed)
$path = $url;
// Load image
$image = imagecreatefromjpeg($path);
$w = imagesx($image);
$h = imagesy($image);
// Load watermark
$watermark = imagecreatefrompng('watermark.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);
// Merge watermark upon the original image (center center)
imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);
// Output the image to the browser
header('Content-type: image/jpeg');
imagejpeg($image);
// destroy both images
imagedestroy($image);
imagedestroy($watermark);
// kill script
exit();
?>
Left: Output Image | Right: Original Image
Note:
The output image was compressed several times until: Original -> PHP Output -> GIMP -> Here.
After much testing, I came to the conclusion that PHP's GD Image does not support color profiles on the images that are being watermarked. I am now using Imagick and the colors are perfect.