PDA

View Full Version : سوال: نوشتن متن بر روی عکس



shahinshyd
پنج شنبه 03 شهریور 1390, 11:02 صبح
با سلام
من یک کد گالری تصاویر دارم که به این صورت هست


// check we have the appropriate variable data
// variables are button-text and color

$button_text = $_REQUEST['button_text'];
$color = $_REQUEST['color'];

if (empty($button_text) || empty($color))
{
echo 'Could not create image - form not filled out correctly';
exit;
}

// create an image of the right background and check size
$im = ImageCreateFromPNG ($color.'-button.png');

$width_image = ImageSX($im);
$height_image = ImageSY($im);

// Our images need an 18 pixel margin in from the edge of the image
$width_image_wo_margins = $width_image - (2 * 18);
$height_image_wo_margins = $height_image - (2 * 18);

// Work out if the font size will fit and make it smaller until it does
// Start out with the biggest size that will reasonably fit on our buttons
$font_size = 33;

// you need to tell GD2 where your fonts reside
putenv('GDFONTPATH=C:\WINDOWS\Fonts');
$fontname = 'arial';

do
{
$font_size--;

// find out the size of the text at that font size
$bbox=ImageTTFBBox ($font_size, 0, $fontname, $button_text);

$right_text = $bbox[2]; // right co-ordinate
$left_text = $bbox[0]; // left co-ordinate
$width_text = $right_text - $left_text; // how wide is it?
$height_text = abs($bbox[7] - $bbox[1]); // how tall is it?

}
while ( $font_size>8 &&
( $height_text>$height_image_wo_margins ||
$width_text>$width_image_wo_margins )
);

if ( $height_text>$height_image_wo_margins ||
$width_text>$width_image_wo_margins )
{
// no readable font size will fit on button
echo 'Text given will not fit on button.<br />';
}
else
{
// We have found a font size that will fit
// Now work out where to put it

$text_x = $width_image/2.0 - $width_text/2.0;
$text_y = $height_image/2.0 - $height_text/2.0 ;

if ($left_text < 0)
$text_x += abs($left_text); // add factor for left overhang

$above_line_text = abs($bbox[7]); // how far above the baseline?
$text_y += $above_line_text; // add baseline factor

$text_y -= 2; // adjustment factor for shape of our template

$white = ImageColorAllocate ($im, 255, 255, 255);

ImageTTFText ($im, $font_size, 0, $text_x, $text_y, $white, $fontname,
$button_text);

Header ('Content-type: image/png');
ImagePNG ($im);
}

ImageDestroy ($im);

این سورس بوسیله یک فرم متی که باید روی تصویر نوشته بشه رو میگیره ولی مشکل این جاست که متن روی تصویر نمیاد و پیغام خطا میده

http://1.almas.host56.com/images/1ada9947e0d6.gif
از نام فونت ایراد میگره ولی من چک کردم .

رضا قربانی
پنج شنبه 03 شهریور 1390, 15:46 عصر
سلام شاهین جان
خستخ نباشید
آقا می تونی با فتوشاپ یه چیزی بنویسی و با فونت دلخواهت به مثلا png تبدیل کنی و توسط توابع GD اون رو روی عکس قرار بدی.

آماده پایین دریافت کن
موفق باشید

amin1softco
پنج شنبه 03 شهریور 1390, 16:17 عصر
نمی دونم مشکل چیه ولی دوباره برای من کار داد؟؟!؟:متفکر:
فقط تا فارسی می نویسی بهم میریزه که با فارسی ساز درست می شه ولی فونتایی مثل هما Bhoma روش کار نمی ده یکمی ضایعه؟؟؟؟

shahinshyd
جمعه 04 شهریور 1390, 11:26 صبح
با تشکر از اقا رضا واقا امین
من با این تابع ImageTTFText یک مشکل دیگه هم دارم من یک نظر سنجی تو سایتم گذاشتم که برای نمایش نتایج از توابع gd استفاده میکنه برای رسم مستطیل و نوشتن متن روی عکسی که درست شده


<?php
/*******************************************
Database query to get poll info
*******************************************/

// get vote from form
$vote=$_REQUEST['vote'];

// log in to database
if (!$db_conn = mysql_connect('localhost', 'poll', 'poll'))
{
echo 'Could not connect to db<br />';
exit;
};
@mysql_select_db('poll');

if (!empty($vote)) // if they filled the form out, add their vote
{
$vote = addslashes($vote);
$query = "update poll_results
set num_votes = num_votes + 1
where candidate = '$vote'";
if(!($result = @mysql_query($query, $db_conn)))
{
echo 'Could not connect to db<br />';
exit;
}
};

// get current results of poll, regardless of whether they voted
$query = 'select * from poll_results';
if(!($result = @mysql_query($query, $db_conn)))
{
echo 'Could not connect to db<br />';
exit;
}
$num_candidates = mysql_num_rows($result);

// calculate total number of votes so far
$total_votes=0;
while ($row = mysql_fetch_object ($result))
{
$total_votes += $row->num_votes;
}
mysql_data_seek($result, 0); // reset result pointer


/*******************************************
Initial calculations for graph
*******************************************/
// set up constants
putenv('GDFONTPATH=C:\WINDOWS\Fonts');
$width=500; // width of image in pixels - this will fit in 640x480
$left_margin = 50; // space to leave on left of graph
$right_margin= 50; // ditto right
$bar_height = 40;
$bar_spacing = $bar_height/2;
$font = 'arial.ttf';
$title_size= 16; // point
$main_size= 12; // point
$small_size= 12; // point
$text_indent = 10; // position for text labels from edge of image

// set up initial point to draw from
$x = $left_margin + 60; // place to draw baseline of the graph
$y = 50; // ditto
$bar_unit = ($width-($x+$right_margin)) / 100; // one "point" on the graph

// calculate height of graph - bars plus gaps plus some margin
$height = $num_candidates * ($bar_height + $bar_spacing) + 50;

/*******************************************
Set up base image
*******************************************/
// create a blank canvas
$im = ImageCreateTrueColor($width,$height);

// Allocate colors
$white=ImageColorAllocate($im,255,255,255);
$blue=ImageColorAllocate($im,0,64,128);
$black=ImageColorAllocate($im,0,0,0);
$pink = ImageColorAllocate($im,255,78,243);

$text_color = $black;
$percent_color = $black;
$bg_color = $white;
$line_color = $black;
$bar_color = $blue;
$number_color = $pink;

// Create "canvas" to draw on
ImageFilledRectangle($im,0,0,$width,$height,$bg_co lor);

// Draw outline around canvas
ImageRectangle($im,0,0,$width-1,$height-1,$line_color);

// Add title
$title = 'Poll Results';
$title_dimensions = ImageTTFBBox($title_size, 0, $font, $title);
$title_length = $title_dimensions[2] - $title_dimensions[0];
$title_height = abs($title_dimensions[7] - $title_dimensions[1]);
$title_above_line = abs($title_dimensions[7]);
$title_x = ($width-$title_length)/2; // center it in x
$title_y = ($y - $title_height)/2 + $title_above_line; // center in y gap
ImageTTFText($im, $title_size, 0, $title_x, $title_y,
$text_color, $font, $title);

// Draw a base line from a little above first bar location
// to a little below last
ImageLine($im, $x, $y-5, $x, $height-15, $line_color);

/*******************************************
Draw data into graph
*******************************************/
// Get each line of db data and draw corresponding bars
while ($row = mysql_fetch_object ($result))
{
if ($total_votes > 0)
$percent = intval(round(($row->num_votes/$total_votes)*100));
else
$percent = 0;

// display percent for this value
$percent_dimensions = ImageTTFBBox($main_size, 0, $font, $percent.'%');
$percent_length = $percent_dimensions[2] - $percent_dimensions[0];
ImageTTFText($im, $main_size, 0, $width-$percent_length-$text_indent,
$y+($bar_height/2), $percent_color, $font, $percent.'%');

if ($total_votes > 0)
$right_value = intval(round(($row->num_votes/$total_votes)*100));
else
$right_value = 0;

// length of bar for this value
$bar_length = $x + ($right_value * $bar_unit);

// draw bar for this value
ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);

// draw title for this value
ImageTTFText($im, $main_size, 0, $text_indent, $y+($bar_height/2),
$text_color, $font, "$row->candidate");

// draw outline showing 100%
ImageRectangle($im, $bar_length+1, $y-2,
($x+(100*$bar_unit)), $y+$bar_height, $line_color);

// display numbers
ImageTTFText($im, $small_size, 0, $x+(100*$bar_unit)-50, $y+($bar_height/2),
$number_color, $font, $row->num_votes.'/'.$total_votes);

// move down to next bar
$y=$y+($bar_height+$bar_spacing);
}

/*******************************************
Display image
*******************************************/
Header('Content-type: image/png');
ImagePNG($im);

/*******************************************
Clean up
*******************************************/
ImageDestroy($im);
?>



خیلی عجیبه من این کد رو روی یک پوشه روی لوکال تست کردم جواب داد

http://1.almas.comlu.com/images/e591403ff232.gif

ولی همین کد رو جای دیگه استفاده میکنم پیغام میده!!!؟
http://1.almas.comlu.com/images/40c945bb7298.gif

amin1softco
جمعه 04 شهریور 1390, 12:08 عصر
حتماً اونجا ویندوز رو در یک درایو دیگه نصب کردند فونت ها رو بزارید کنار فایل های پی اچ پی فک کنم مشکل حل بشه....
مثل اون فایلی که ضمیمه کرده بودم روی هاست تست کردم کار داد!!!!

shahinshyd
جمعه 04 شهریور 1390, 13:05 عصر
با تشکر مشکل حل شد
-روی هاست اینترنت اگه فونت تو پوشه سایت باشه مشکلی نداره؟
-یه سوال دیگه من میخوام این کد رو تو سایت بذارم ولی از هدر ایراد میگیره


Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\d\func.php:6) in C:\xampp\htdocs\d\show_poll.php on line 159و برای نمایش هم که حتما باید این خط باشه


header('Content-type:image/png');

ایا راهی هست که بگیم تو این صفحه از این هدر استفاده کنه؟و به بقیه هدر ها کاری نداشته باشه؟

amin1softco
جمعه 04 شهریور 1390, 13:16 عصر
-نه مشکلی نداره
- شما باید قبل از هر چیزی کد header رو قرار بدین یعنی قبل از نوشتن هر گونه کد html تا صحیح کار بده

shahinshyd
جمعه 04 شهریور 1390, 13:28 عصر
خوب من کد پست 4 رو تغییر دادم ولی نتیجه نمایش داده نمیشه و از فایل توابع که require کردم ایراد میگیره


header('Content-type:image/png');
require_once("func.php");

/*******************************************
Database query to get poll info
*******************************************/

// get vote from form
$vote=$_REQUEST['vote'];

// log in to database
if (!$db_conn = mysql_connect('localhost','root', ''))
{
echo 'Could not connect to db<br />';
exit;
};
@mysql_select_db('db_news');

if (!empty($vote)) // if they filled the form out, add their vote
{
$vote = addslashes($vote);
$query = "update poll_results
set num_votes = num_votes + 1
where candidate = '$vote'";
if(!($result = @mysql_query($query, $db_conn)))
{
echo 'Could not connect to db<br />';
exit;
}
};

// get current results of poll, regardless of whether they voted
$query = 'select * from poll_results';
if(!($result = @mysql_query($query, $db_conn)))
{
echo 'Could not connect to db<br />';
exit;
}
$num_candidates = mysql_num_rows($result);

// calculate total number of votes so far
$total_votes=0;
while ($row = mysql_fetch_object ($result))
{
$total_votes += $row->num_votes;
}
mysql_data_seek($result, 0); // reset result pointer


/*******************************************
Initial calculations for graph
*******************************************/
// set up constants
putenv('GDFONTPATH=C:\WINDOWS\Fonts');
$width=500; // width of image in pixels - this will fit in 640x480
$left_margin = 50; // space to leave on left of graph
$right_margin= 50; // ditto right
$bar_height = 40;
$bar_spacing = $bar_height/2;
$font = 'arial.ttf';
$title_size= 16; // point
$main_size= 12; // point
$small_size= 12; // point
$text_indent = 10; // position for text labels from edge of image

// set up initial point to draw from
$x = $left_margin + 60; // place to draw baseline of the graph
$y = 50; // ditto
$bar_unit = ($width-($x+$right_margin)) / 100; // one "point" on the graph

// calculate height of graph - bars plus gaps plus some margin
$height = $num_candidates * ($bar_height + $bar_spacing) + 50;

/*******************************************
Set up base image
*******************************************/
// create a blank canvas
$im = ImageCreateTrueColor($width,$height);

// Allocate colors
$white=ImageColorAllocate($im,255,255,255);
$blue=ImageColorAllocate($im,0,64,128);
$black=ImageColorAllocate($im,0,0,0);
$pink = ImageColorAllocate($im,255,78,243);

$text_color = $black;
$percent_color = $black;
$bg_color = $white;
$line_color = $black;
$bar_color = $blue;
$number_color = $pink;

// Create "canvas" to draw on
ImageFilledRectangle($im,0,0,$width,$height,$bg_co lor);

// Draw outline around canvas
ImageRectangle($im,0,0,$width-1,$height-1,$line_color);

// Add title
$title = 'Poll Results';
$title_dimensions = ImageTTFBBox($title_size, 0, $font, $title);
$title_length = $title_dimensions[2] - $title_dimensions[0];
$title_height = abs($title_dimensions[7] - $title_dimensions[1]);
$title_above_line = abs($title_dimensions[7]);
$title_x = ($width-$title_length)/2; // center it in x
$title_y = ($y - $title_height)/2 + $title_above_line; // center in y gap
ImageTTFText($im, $title_size, 0, $title_x, $title_y,
$text_color, $font, $title);

// Draw a base line from a little above first bar location
// to a little below last
ImageLine($im, $x, $y-5, $x, $height-15, $line_color);

/*******************************************
Draw data into graph
*******************************************/
// Get each line of db data and draw corresponding bars
while ($row = mysql_fetch_object ($result))
{
if ($total_votes > 0)
$percent = intval(round(($row->num_votes/$total_votes)*100));
else
$percent = 0;

// display percent for this value
$percent_dimensions = ImageTTFBBox($main_size, 0, $font, $percent.'%');
$percent_length = $percent_dimensions[2] - $percent_dimensions[0];
ImageTTFText($im, $main_size, 0, $width-$percent_length-$text_indent,
$y+($bar_height/2), $percent_color, $font, $percent.'%');

if ($total_votes > 0)
$right_value = intval(round(($row->num_votes/$total_votes)*100));
else
$right_value = 0;

// length of bar for this value
$bar_length = $x + ($right_value * $bar_unit);

// draw bar for this value
ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);

// draw title for this value
ImageTTFText($im, $main_size, 0, $text_indent, $y+($bar_height/2),
$text_color, $font, "$row->candidate");

// draw outline showing 100%
ImageRectangle($im, $bar_length+1, $y-2,
($x+(100*$bar_unit)), $y+$bar_height, $line_color);

// display numbers
ImageTTFText($im, $small_size, 0, $x+(100*$bar_unit)-50, $y+($bar_height/2),
$number_color, $font, $row->num_votes.'/'.$total_votes);

// move down to next bar
$y=$y+($bar_height+$bar_spacing);
}

/*******************************************
Display image
*******************************************/

ImagePNG($im);

/*******************************************
Clean up
*******************************************/
ImageDestroy($im);

shahinshyd
جمعه 04 شهریور 1390, 13:29 عصر
این هم پیغام خطاش

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\d\func.php:6) in C:\xampp\htdocs\d\show_poll.php on line 2

amin1softco
جمعه 04 شهریور 1390, 14:08 عصر
این مشکل ذخیره سازی فایل های utf-8 که اول فایل یک چیزی می نویسه اگه notpad++ دارید طبق عکس عمل کنید و فایلتون رو سیو کنید ببنید چی می شه....74398

shahinshyd
جمعه 04 شهریور 1390, 16:32 عصر
کاری که گفتید رو انجام دادم ولی باز همون پیغام بالا میاد و از فایل func.php خطا میگیره و اگر

require_once("func.php")
این خط رو بردارم درست میشه ولی مشکل اینجاست که توابع فایل func رو لازم دارم

رضا قربانی
شنبه 05 شهریور 1390, 15:09 عصر
نیاز نیست فایل رو همون اول فراخوانی کنی - جایی که احتیاج به اون فایل داری فراخوانی کن و اگر بازم مشکل داشتی بدون که مشکل کد های داخل فایل func.php هست.