Okay.. so I've been trying to make a 'signature' for a game that I play.
The only catch is, I've been running into quite a few problems when wanting to insert an Avatar & Online/Offline image.
The two things I've been trying to do is:
Get user status from a webpage habplus.com/home/[username] by checking if habbo_offline.gif is present.
if(strpos(file_get_contents('http://www.habpl.us/home/'.$username.''),
'habbo_offline.gif') == true) {
Grab users image and display on the final image
function habSigFigure($username){
$omgfig = 'http://www.habpl.us/figure.php?user='.$username.'&img_format=gif';
return $omgfig;
//place habbo avatar
$habsigfig = imagecreatefromgif($omgfig);
imagecopy($img, $habsigfig, 13, 32, 0, 0, imagesx($habsigfig), imagesy($habsigfig));*/
//place habbo avatar
I've included the entire source code, and the and the page can be accessed here
-Another link with variables included
Hope you can help..
Sincerely yours, Malmoc
<?php
include 'config.php';
$username=$_REQUEST["user"];
$grabstat3 = fopen("http://habplus.com/fansitetools/userStats.php?user={$username}&stat=motto", "r");
while (!feof($grabstat3)){ $motto1 = fgets($grabstat3);
}
fclose($grabstat3);
$username=$_REQUEST["user"];
$grabstat2 = fopen("http://habplus.com/fansitetools/userStats.php?user={$username}&stat=pixels", "r");
while (!feof($grabstat2)){ $pixels1 = fgets($grabstat2);
}
fclose($grabstat2);
$username=$_REQUEST["user"];
$grabstat1 = fopen("http://habplus.com/fansitetools/userStats.php?user={$username}&stat=credits", "r");
while (!feof($grabstat1)){ $credits1 = fgets($grabstat1);
}
fclose($grabstat1);
$pixels = 'Pixels: '.$pixels1.'';
$credits = 'Credits: '.$credits1.'';
$motto = 'Motto: '.$motto1.'';
/* Get custom img */
if(empty($_REQUEST['img'])){
$img = 'default.png';
}else{
$img =$_REQUEST['img'];
}
/* TEXT COLORS */
$red =$_REQUEST['red'];
$green =$_REQUEST['green'];
$blue =$_REQUEST['blue'];
/* Font size */
$fsize =$_REQUEST['fsize'];
/*function habSigStatus($username){
if(strpos(file_get_contents('http://www.habpl.us/home/'.$username.''), 'habbo_offline.gif') == true){
return false;
}else{
return true;
function habSigFigure($username){
$omgfig = 'http://www.habpl.us/figure.php?user='.$username.'&img_format=gif';
return $omgfig;
}
}
}*/
/*
//place habbo avatar
$habsigfig = imagecreatefromgif($omgfig);
imagecopy($img, $habsigfig, 13, 32, 0, 0, imagesx($habsigfig), imagesy($habsigfig));*/
//place habbo avatar
//habbo status
if(strpos(file_get_contents('http://www.habpl.us/home/'.$username.''), 'habbo_offline.gif') == true){
$status_img = imagecreatefromgif('habbo_offline.gif');
}else{
$status_img = imagecreatefromgif('habbo_online.gif');
}
imagecopy($image, $status_img, 403, 96, 0, 0, 50, 16);
//habbo status
$image = imagecreatefrompng($img);
$font_color = imagecolorallocate($image, $red, $green, $blue);
imagefttext($image, $fsize, 0, 3, 12, $font_color, './volt.ttf', $credits); /* top left */
imagefttext($image, $fsize, 0, 403, 12, $font_color, './volt.ttf', $pixels); /* top right */
imagefttext($image, $fsize, 0, 3, 96, $font_color, './volt.ttf', $motto); /* bottom left */
imagefttext($image, $fsize, 0, 403, 96, $font_color, './volt.ttf', $online); /* bottom right */
/* imagefttext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text [, array $extrainfo]) */
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
$image should be a valid resource, in your code $image is null
$image = imagecreatefrompng($img);
imagecopy($image, $status_img, 403, 96, 0, 0, 50, 16);
you can use
$image = imagecreatetruecolor(50, 16); //width,height
imagecopy($image, $status_img, 403, 96, 0, 0, 50, 16);
Here's your problem, fairly derpy to be honest:
imagecopy($image, $status_img, 403, 96, 0, 0, 50, 16);
$image = imagecreatefrompng($img);
You kind of need to create the image before you can copy to it. Swap those two lines around and you should be good.
Related
I'm trying to write a small program to create cartoon eye's and output them to a folder (PHP CLI) and I'm running into trouble with the expected output. I have two functions that copy sections from an image, one for rotating while preserving the original size and one to get a small section for styling needs. When the functions are used in the order of rotatePreserve() then partialEye() the addcolorallocate in partialEye always becomes transparent. I've tried various colors, all end up with the same result. I've searched and read, but I don't understand why this is happening?
I've tried outputting the image halfway through and createimagefrompng with the same results, I'm failing to understand why the color isn't added correctly. If you run the code, you'll see that the output testing along the way works unless rotatePreserve is called before the partialEye function.
Please help me understand what going wrong here.
Thanks
// Setup Variables
$fixed_x = 512;
$fixed_y = 512;
$output_dir = './output';
$output_filename = 'eye_test.png';
// Output info
echo "\e[1;30;40mBeginning Eye Creator......\n";
echo "Image Size: $fixed_x x $fixed_y \n";
// Functions
function partialEye ($img, $cut, $x, $y, $brow) {
//New image, same size, get bottom part of Eye (Optionally add eye lid )
$part_eye = imagecreate($x, $y);
imagealphablending($part_eye, true);
$transparent = imagecolorallocatealpha($part_eye , 0, 0, 0, 127);
imagefill($part_eye, 0, 0, $transparent);
imagecopy($part_eye, $img, 0, $cut, 0, $cut, $x, $y-$cut);
$blk = imagecolorallocate($part_eye, 0,0,0);
if ($brow === 1) {
//If ran after the rotate// draws in transparent??
imagefilledellipse($part_eye, 256-15, $cut, 256, 15, $blk);
} elseif ($brow === 2) {
imagefilledellipse($part_eye, 256+15, $cut, 256, 15, $blk);
}
return $part_eye;
imagedestroy($part_eye);
}
function rotatePreserve($img, $orig_x, $orig_y, $deg) {
$width_before = $orig_x;
$height_before = $orig_y;
$rotateImgTransparency = imagecolorallocatealpha($img , 1, 1, 1, 127);
$img = imagerotate($img, $deg, $rotateImgTransparency);
//but imagerotate scales, so we clip to the original size
$img2 = imagecreatetruecolor($orig_x, $orig_y);
imagealphablending($img2, false);
imagesavealpha($img2, true);
$rotateImg2Transparency = imagecolorallocatealpha($img2 , 0, 0, 0, 127);
imagefill($img2, 0, 0, $rotateImg2Transparency);
$new_width = imagesx($img); // where dimensions are
$new_height = imagesy($img);// the scaled ones (by imagerotate)
imagecopyresampled(
$img2, $img,
0, 0,
($new_width-$orig_x)/2,
($new_height-$orig_y)/2,
$orig_x,
$orig_y,
$orig_x,
$orig_y
);
return $img2;
imagedestroy($img);
imagedestroy($img2);
}
//New Image
$new_image = imagecreatetruecolor($fixed_x, $fixed_y);
imagealphablending($new_image, false);
//Colors for $new_image
$white_color = imagecolorallocate($new_image, 255, 255, 255 );
$blk_color = imagecolorallocate($new_image, 0,0,0);
$pngTransparency = imagecolorallocatealpha($new_image , 0, 0, 0, 127);
$rand_R = rand(0,255);
$rand_G = rand(0,255);
$rand_B = rand(0,255);
$iris_color = imagecolorallocate($new_image, $rand_R,$rand_G,$rand_B);
imagefill($new_image, 0, 0, $pngTransparency);
$size = 256;
$draw_x = $fixed_x/2;
$draw_y = $fixed_y/2;
imagefilledarc($new_image, $draw_x, $draw_y, $size, $size, 0, 360, $blk_color, IMG_ARC_PIE);
imagefilledarc($new_image, $draw_x, $draw_y, $size-20, $size-20, 0, 360, $white_color, IMG_ARC_PIE);
imagefilledarc($new_image, $draw_x, $draw_y, $size-100, $size-100, 0, 360, $iris_color, IMG_ARC_PIE);
imagefilledarc($new_image, $draw_x, $draw_y, $size-170, $size-170, 0, 360, $blk_color, IMG_ARC_PIE);
imagefilledellipse($new_image, $draw_x, $draw_y-50, $size-180, $size-220, $white_color);
imagefilledellipse($new_image, $draw_x, $draw_y+50, $size-200, $size-225, $white_color);
// Cut top part of Eye off
$partial_eye = partialEye($new_image, 192, $fixed_x, $fixed_y, 0);
// Cut top part of eye and add lid
$left_eye_add_lid_eye = partialEye($new_image, 192, $fixed_x, $fixed_y, 1);
// Rotate and Preserve Size
$left_eye = rotatePreserve($left_eye_add_lid_eye, $fixed_x, $fixed_y, -13);
imagesavealpha($left_eye, true);
imagepng($left_eye, $output_dir.'/left_eye.png', 0, 9);
$first_rotate = rotatePreserve($new_image, $fixed_x, $fixed_y, 26);
// FAILS!!
$right_eye_add_lid_eye = partialEye($first_rotate, 192, $fixed_x, $fixed_y, 2);
//Testing shows the fail with any color choice becoming transparent
imagesavealpha($right_eye_add_lid_eye, true);
imagepng($right_eye_add_lid_eye, $output_dir.'/'.$output_filename, 0, 9);
// Then rotate and flip for final position this would finish the right eye in the correct position
// this would give the expected result if the "eye lid" wasn't transparent
// $right_eye_add_lid_eye = rotatePreserve($right_eye_add_lid_eye, $fixed_x, $fixed_y, -13);
// imageflip($right_eye_add_lid_eye, IMG_FLIP_HORIZONTAL);
// imagesavealpha($right_eye_add_lid_eye, true);
// imagepng($right_eye_add_lid_eye, $output_dir.'/right_eye.png', 0, 9);
results
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
I input this code to show text convert into image.
<?php
// Set the content-type
header('Content-Type:image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font="arial.ttf";
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
And after this the OUTPUT is::
Output in mozilla
I'm using WAMP in windows and also GD installed... My gd_info() :
array (size=12)
'GD Version' => string 'bundled (2.1.0 compatible)' (length=26)
'FreeType Support' => boolean true
'FreeType Linkage' => string 'with freetype' (length=13)
'T1Lib Support' => boolean false
'GIF Read Support' => boolean true
'GIF Create Support' => boolean true
'JPEG Support' => boolean true
'PNG Support' => boolean true
'WBMP Support' => boolean true
'XPM Support' => boolean true
'XBM Support' => boolean true
'JIS-mapped Japanese Font Support' => boolean false
So finally what I have to do... Please help me......
you need to specify the full path to the font file.
and give 755 permission to the font file
class CaptchaSecurityImages {
var $font = 'monofont.ttf';
function getFontPath(){
return $_SERVER['DOCUMENT_ROOT'].'/captcha/monofont.ttf';
}
function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '012345678';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}
function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.75;
$image = #imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->getFontPath(), $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->getFontPath() , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code'] = $code;
}
}
$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';
$captcha = new CaptchaSecurityImages($width,$height,$characters);
Try my code... create one php file->put this code and just run you
got result surely... happy codding....
Don't Forgot To add "arial.ttf" file where you run your php file same directory...
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
If it worked after you comment to both call to imagettftext(), make sure file arial.ttf exists in same directory of script. Also comment out call to header() to be able to view any error message returned.
I am trying to create a PHP image, although I have come across some problems.
I am trying to add a URL to the php file. The url (global.php) contains information to grab the Username of a specific user and input certain image files into the php image.
But whenever I add the global.php link, the image won't show up.
My main question is, am I able to add links to files, plus add things like echo ' ' ; to the php image file?
<?php
// MySQL Connect & Select
mysql_connect('localhost', 'root', 'password') or die('Error connecting to mysql');
mysql_select_db('database');
$result = mysql_query("SELECT * FROM users WHERE username = '".stripslashes(trim(htmlspecialchars(mysql_real_escape_string(mysql_escape_string($_GET['user'])))))."'");
$row = mysql_fetch_array($result);
$result_stats = mysql_query("SELECT * FROM user_stats WHERE id = '".$row['id']."'") or die("USER_STATS: ".mysql_error());
$row_stats = mysql_fetch_array($result_stats);
$result_info = mysql_query("SELECT * FROM user_info WHERE user_id = '".$row['id']."'") or die("USER_INFO: ".mysql_error());
$row_info = mysql_fetch_array($result_info);
$text[1] = "Hiya, ".$row['username']."!";
$text[2] = "Your last login was ".#date("d-M-Y\n H:i",$row['last_online']).". We missed you :)";
$text[3] = "Do you know the Rare! Way";
$text[4] = "".$row['username']."?";
$text[5] = "I do!";
$text[6] = "Be sure to follow the Rare! Way. \n \n When you follow the Way, you're giving \n yourself the best experience on Rare! that you \n can ever get! Stay tuned for more.";
// Colors
$image = imagecreatefrompng('userbanner/hotel.png');
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 250, 250, 250);
$grey = imagecolorallocate($image, 77, 77, 77);
// Font
$volterB = 'userbanner/Ubuntu-B.ttf';
$volder = 'userbanner/Ubuntu-M.ttf';
$conden = 'userbanner/Ubuntu-L.ttf';
$light = 'userbanner/Ubuntu-Regular.ttf';
$userimage1 = imagecreatefrompng("http://www.habbo.com/habbo-imaging/avatarimage?figure=".$row['look']."&direction=2&head_direction=2&gesture=spk&img_format=png");
imagecopy($image, $userimage1, 420, 288, 0, 0, 64, 110);
$userimage = imagecreatefrompng("http://www.habbo.com/habbo-imaging/avatarimage?figure=".$row['look']."&direction=2&head_direction=2&gesture=sml&img_format=png");
imagecopy($image, $userimage, 220, 628, 0, 0, 64, 110);
// Create image from text
imagettftext($image, 15, 0, 20, 465, $black, $volterB, $text[1]);
imagettftext($image, 8, 0, 20, 483, $black, $conden, $text[2]);
imagettftext($image, 18, 0, 510, 300, $black, $volterB, $text[3]);
imagettftext($image, 15, 0, 510, 330, $black, $volterB, $text[4]);
imagettftext($image, 15, 0, 345, 333, $black, $volder, $text[5]);
imagettftext($image, 10, 0, 520, 360, $grey, $light, $text[6]);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
If I've been unclear in any way, please let me know. Thanks :)
I have the following code validation php script:
if(empty($_POST['captcha_code'])) {
$error = 1;
$code[3] = 'color:#FF0000;';
} else {
include_once "formfiles/captcha.php";
$randomnr = new Securimage();
$valid = $randomnr->check($_POST['captcha_code']);
if(!$valid) {
$error = 1;
$code[3] = 'color:#FF0000;';
$code[4] = '<strong><span style="color:#FF0000;">Incorrect code</span></strong>';
}
}
and this captcha php code:
<?php
Securimage();
exit();
function Securimage()
{
$randomnr = rand(1000, 9999);
$_SESSION['randomnr2'] = md5($randomnr);
$im = imagecreatetruecolor(100, 38);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 150, 150, 150);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 200, 35, $black);
//path to font - this is just an example you can use any font you like:
$font = dirName(__FILE__).'/calvin.ttf';
imagettftext($im, 20, 4, 22, 30, $grey, $font, $randomnr);
imagettftext($im, 20, 4, 15, 32, $white, $font, $randomnr);
imagegif($im);
imagedestroy($im);
}
?>
After submitting my form I always get an awkward bit of code starting with gif. Where is my fault? Can somebody please help me?
You need to decalare header about the content type to browser recognize it as image. Try this
header('Content-Type: image/gif');
Securimage();
exit();